3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-12-24 22:06:54 +00:00

Add support for running rust-cache commands from within a Nix shell (#290)

This commit is contained in:
marc0246 2025-12-01 19:56:12 +01:00 committed by GitHub
parent 779680da71
commit 151eeee51b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 167 additions and 43 deletions

View file

@ -18,6 +18,9 @@ const STATE_CONFIG = "RUST_CACHE_CONFIG";
const HASH_LENGTH = 8;
export class CacheConfig {
/** A format string for running commands */
public cmdFormat: string = "";
/** All the paths we want to cache */
public cachePaths: Array<string> = [];
/** The primary cache key */
@ -53,6 +56,17 @@ export class CacheConfig {
static async new(): Promise<CacheConfig> {
const self = new CacheConfig();
let cmdFormat = core.getInput("cmd-format");
if (cmdFormat) {
const placeholderMatches = cmdFormat.match(/\{0\}/g);
if (!placeholderMatches || placeholderMatches.length !== 1) {
cmdFormat = "{0}";
}
} else {
cmdFormat = "{0}";
}
self.cmdFormat = cmdFormat;
// Construct key prefix:
// This uses either the `shared-key` input,
// or the `key` input combined with the `job` key.
@ -89,7 +103,7 @@ export class CacheConfig {
// resulting environment hash.
let hasher = crypto.createHash("sha1");
const rustVersion = await getRustVersion();
const rustVersion = await getRustVersion(cmdFormat);
let keyRust = `${rustVersion.release} ${rustVersion.host}`;
hasher.update(keyRust);
@ -158,7 +172,7 @@ export class CacheConfig {
)),
);
const workspaceMembers = await workspace.getWorkspaceMembers();
const workspaceMembers = await workspace.getWorkspaceMembers(cmdFormat);
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => path.join(member.path, "Cargo.toml")));
@ -366,8 +380,8 @@ interface RustVersion {
"commit-hash": string;
}
async function getRustVersion(): Promise<RustVersion> {
const stdout = await getCmdOutput("rustc", ["-vV"]);
async function getRustVersion(cmdFormat: string): Promise<RustVersion> {
const stdout = await getCmdOutput(cmdFormat, "rustc -vV");
let splits = stdout
.split(/[\n\r]+/)
.filter(Boolean)

View file

@ -39,9 +39,9 @@ async function run() {
const workspaceCrates = core.getInput("cache-workspace-crates").toLowerCase() || "false";
const allPackages = [];
for (const workspace of config.workspaces) {
const packages = await workspace.getPackagesOutsideWorkspaceRoot();
const packages = await workspace.getPackagesOutsideWorkspaceRoot(config.cmdFormat);
if (workspaceCrates === "true") {
const wsMembers = await workspace.getWorkspaceMembers();
const wsMembers = await workspace.getWorkspaceMembers(config.cmdFormat);
packages.push(...wsMembers);
}
allPackages.push(...packages);

View file

@ -16,14 +16,15 @@ export function reportError(e: any) {
}
export async function getCmdOutput(
cmdFormat: string,
cmd: string,
args: Array<string> = [],
options: exec.ExecOptions = {},
): Promise<string> {
cmd = cmdFormat.replace("{0}", cmd);
let stdout = "";
let stderr = "";
try {
await exec.exec(cmd, args, {
await exec.exec(cmd, [], {
silent: true,
listeners: {
stdout(data) {
@ -37,7 +38,7 @@ export async function getCmdOutput(
});
} catch (e) {
(e as any).commandFailed = {
command: `${cmd} ${args.join(" ")}`,
command: cmd,
stderr,
};
throw e;

View file

@ -8,12 +8,13 @@ const SAVE_TARGETS = new Set(["lib", "proc-macro"]);
export class Workspace {
constructor(public root: string, public target: string) {}
async getPackages(filter: (p: Meta["packages"][0]) => boolean, ...extraArgs: string[]): Promise<Packages> {
async getPackages(cmdFormat: string, filter: (p: Meta["packages"][0]) => boolean, extraArgs?: string): Promise<Packages> {
const cmd = "cargo metadata --all-features --format-version 1" + (extraArgs ? ` ${extraArgs}` : "");
let packages: Packages = [];
try {
core.debug(`collecting metadata for "${this.root}"`);
const meta: Meta = JSON.parse(
await getCmdOutput("cargo", ["metadata", "--all-features", "--format-version", "1", ...extraArgs], {
await getCmdOutput(cmdFormat, cmd, {
cwd: this.root,
env: { ...process.env, "CARGO_ENCODED_RUSTFLAGS": "" },
}),
@ -29,12 +30,12 @@ export class Workspace {
return packages;
}
public async getPackagesOutsideWorkspaceRoot(): Promise<Packages> {
return await this.getPackages((pkg) => !pkg.manifest_path.startsWith(this.root));
public async getPackagesOutsideWorkspaceRoot(cmdFormat: string): Promise<Packages> {
return await this.getPackages(cmdFormat, (pkg) => !pkg.manifest_path.startsWith(this.root));
}
public async getWorkspaceMembers(): Promise<Packages> {
return await this.getPackages((_) => true, "--no-deps");
public async getWorkspaceMembers(cmdFormat: string): Promise<Packages> {
return await this.getPackages(cmdFormat, (_) => true, "--no-deps");
}
}