3
0
Fork 0
mirror of https://github.com/Swatinem/rust-cache synced 2025-11-05 15:06:02 +00:00

fix: Move 'if' check for 'add-rust-environment-hash-key' input above keyFiles definition to avoid un-needed computation

This commit is contained in:
Ryan-Brice 2025-11-02 18:52:16 +08:00
parent 967280621f
commit 6424dd79ce
No known key found for this signature in database
GPG key ID: 3702BD1113AFDD13
3 changed files with 233 additions and 230 deletions

147
dist/restore/index.js vendored
View file

@ -148844,90 +148844,91 @@ class CacheConfig {
workspaces.push(new Workspace(root, target)); workspaces.push(new Workspace(root, target));
} }
self.workspaces = workspaces; self.workspaces = workspaces;
let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml"); // Add hash suffix of all rust environment lockfiles + manifests if
const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed // 'add-rust-environment-hash-key' is true
hasher = external_crypto_default().createHash("sha1"); if (lib_core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
for (const workspace of workspaces) { let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml");
const root = workspace.root; const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed
keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`))); hasher = external_crypto_default().createHash("sha1");
const workspaceMembers = await workspace.getWorkspaceMembers(); for (const workspace of workspaces) {
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml"))); const root = workspace.root;
for (const cargo_manifest of cargo_manifests) { keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
try { const workspaceMembers = await workspace.getWorkspaceMembers();
const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" }); const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml")));
// Use any since TomlPrimitive is not exposed for (const cargo_manifest of cargo_manifests) {
const parsed = parse(content); try {
if ("package" in parsed) { const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" });
const pack = parsed.package; // Use any since TomlPrimitive is not exposed
if ("version" in pack) { const parsed = parse(content);
pack["version"] = "0.0.0"; if ("package" in parsed) {
} const pack = parsed.package;
} if ("version" in pack) {
for (const prefix of ["", "build-", "dev-"]) { pack["version"] = "0.0.0";
const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue;
}
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
} }
catch (_e) { }
// Not an object, probably a string (version), for (const prefix of ["", "build-", "dev-"]) {
// continue. const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue; continue;
} }
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
}
catch (_e) {
// Not an object, probably a string (version),
// continue.
continue;
}
}
} }
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
}
catch (e) {
// Fallback to caching them as regular file
lib_core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_manifest);
} }
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
} }
catch (e) { const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock");
// Fallback to caching them as regular file if (await utils_exists(cargo_lock)) {
lib_core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`); try {
keyFiles.push(cargo_manifest); const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" });
} const parsed = parse(content);
} if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) {
const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock"); // Fallback to caching them as regular file since this action
if (await utils_exists(cargo_lock)) { // can only handle Cargo.lock format version 3
try { lib_core.warning("Unsupported Cargo.lock format, fallback to caching entire file");
const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" }); keyFiles.push(cargo_lock);
const parsed = parse(content); continue;
if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) { }
// Fallback to caching them as regular file since this action // Package without `[[package]].source` and `[[package]].checksum`
// can only handle Cargo.lock format version 3 // are the one with `path = "..."` to crates within the workspace.
lib_core.warning("Unsupported Cargo.lock format, fallback to caching entire file"); const packages = parsed.package.filter((p) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
}
catch (e) {
// Fallback to caching them as regular file
lib_core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock); keyFiles.push(cargo_lock);
continue;
} }
// Package without `[[package]].source` and `[[package]].checksum`
// are the one with `path = "..."` to crates within the workspace.
const packages = parsed.package.filter((p) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
}
catch (e) {
// Fallback to caching them as regular file
lib_core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock);
} }
} }
} keyFiles = sort_and_uniq(keyFiles);
keyFiles = sort_and_uniq(keyFiles); for (const file of keyFiles) {
for (const file of keyFiles) { for await (const chunk of external_fs_default().createReadStream(file)) {
for await (const chunk of external_fs_default().createReadStream(file)) { hasher.update(chunk);
hasher.update(chunk); }
} }
} keyFiles.push(...parsedKeyFiles);
keyFiles.push(...parsedKeyFiles); self.keyFiles = sort_and_uniq(keyFiles);
self.keyFiles = sort_and_uniq(keyFiles);
// Add lock hash suffix if 'add-rust-environment-hash-key' is true
if (lib_core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
let lockHash = digest(hasher); let lockHash = digest(hasher);
key += `-${lockHash}`; key += `-${lockHash}`;
} }

147
dist/save/index.js vendored
View file

@ -148844,90 +148844,91 @@ class CacheConfig {
workspaces.push(new Workspace(root, target)); workspaces.push(new Workspace(root, target));
} }
self.workspaces = workspaces; self.workspaces = workspaces;
let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml"); // Add hash suffix of all rust environment lockfiles + manifests if
const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed // 'add-rust-environment-hash-key' is true
hasher = external_crypto_default().createHash("sha1"); if (core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
for (const workspace of workspaces) { let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml");
const root = workspace.root; const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed
keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`))); hasher = external_crypto_default().createHash("sha1");
const workspaceMembers = await workspace.getWorkspaceMembers(); for (const workspace of workspaces) {
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml"))); const root = workspace.root;
for (const cargo_manifest of cargo_manifests) { keyFiles.push(...(await globFiles(`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`)));
try { const workspaceMembers = await workspace.getWorkspaceMembers();
const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" }); const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => external_path_default().join(member.path, "Cargo.toml")));
// Use any since TomlPrimitive is not exposed for (const cargo_manifest of cargo_manifests) {
const parsed = parse(content); try {
if ("package" in parsed) { const content = await promises_default().readFile(cargo_manifest, { encoding: "utf8" });
const pack = parsed.package; // Use any since TomlPrimitive is not exposed
if ("version" in pack) { const parsed = parse(content);
pack["version"] = "0.0.0"; if ("package" in parsed) {
} const pack = parsed.package;
} if ("version" in pack) {
for (const prefix of ["", "build-", "dev-"]) { pack["version"] = "0.0.0";
const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue;
}
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
} }
catch (_e) { }
// Not an object, probably a string (version), for (const prefix of ["", "build-", "dev-"]) {
// continue. const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue; continue;
} }
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
}
catch (_e) {
// Not an object, probably a string (version),
// continue.
continue;
}
}
} }
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
}
catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_manifest);
} }
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
} }
catch (e) { const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock");
// Fallback to caching them as regular file if (await exists(cargo_lock)) {
core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`); try {
keyFiles.push(cargo_manifest); const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" });
} const parsed = parse(content);
} if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) {
const cargo_lock = external_path_default().join(workspace.root, "Cargo.lock"); // Fallback to caching them as regular file since this action
if (await exists(cargo_lock)) { // can only handle Cargo.lock format version 3
try { core.warning("Unsupported Cargo.lock format, fallback to caching entire file");
const content = await promises_default().readFile(cargo_lock, { encoding: "utf8" }); keyFiles.push(cargo_lock);
const parsed = parse(content); continue;
if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) { }
// Fallback to caching them as regular file since this action // Package without `[[package]].source` and `[[package]].checksum`
// can only handle Cargo.lock format version 3 // are the one with `path = "..."` to crates within the workspace.
core.warning("Unsupported Cargo.lock format, fallback to caching entire file"); const packages = parsed.package.filter((p) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
}
catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock); keyFiles.push(cargo_lock);
continue;
} }
// Package without `[[package]].source` and `[[package]].checksum`
// are the one with `path = "..."` to crates within the workspace.
const packages = parsed.package.filter((p) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
}
catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock);
} }
} }
} keyFiles = sort_and_uniq(keyFiles);
keyFiles = sort_and_uniq(keyFiles); for (const file of keyFiles) {
for (const file of keyFiles) { for await (const chunk of external_fs_default().createReadStream(file)) {
for await (const chunk of external_fs_default().createReadStream(file)) { hasher.update(chunk);
hasher.update(chunk); }
} }
} keyFiles.push(...parsedKeyFiles);
keyFiles.push(...parsedKeyFiles); self.keyFiles = sort_and_uniq(keyFiles);
self.keyFiles = sort_and_uniq(keyFiles);
// Add lock hash suffix if 'add-rust-environment-hash-key' is true
if (core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
let lockHash = digest(hasher); let lockHash = digest(hasher);
key += `-${lockHash}`; key += `-${lockHash}`;
} }

View file

@ -142,110 +142,111 @@ export class CacheConfig {
} }
self.workspaces = workspaces; self.workspaces = workspaces;
let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml"); // Add hash suffix of all rust environment lockfiles + manifests if
const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed // 'add-rust-environment-hash-key' is true
if (core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
let keyFiles = await globFiles(".cargo/config.toml\nrust-toolchain\nrust-toolchain.toml");
const parsedKeyFiles = []; // keyFiles that are parsed, pre-processed and hashed
hasher = crypto.createHash("sha1"); hasher = crypto.createHash("sha1");
for (const workspace of workspaces) { for (const workspace of workspaces) {
const root = workspace.root; const root = workspace.root;
keyFiles.push( keyFiles.push(
...(await globFiles( ...(await globFiles(
`${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`, `${root}/**/.cargo/config.toml\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`,
)), )),
); );
const workspaceMembers = await workspace.getWorkspaceMembers(); const workspaceMembers = await workspace.getWorkspaceMembers();
const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => path.join(member.path, "Cargo.toml"))); const cargo_manifests = sort_and_uniq(workspaceMembers.map((member) => path.join(member.path, "Cargo.toml")));
for (const cargo_manifest of cargo_manifests) { for (const cargo_manifest of cargo_manifests) {
try { try {
const content = await fs_promises.readFile(cargo_manifest, { encoding: "utf8" }); const content = await fs_promises.readFile(cargo_manifest, { encoding: "utf8" });
// Use any since TomlPrimitive is not exposed // Use any since TomlPrimitive is not exposed
const parsed = toml.parse(content) as { [key: string]: any }; const parsed = toml.parse(content) as { [key: string]: any };
if ("package" in parsed) { if ("package" in parsed) {
const pack = parsed.package; const pack = parsed.package;
if ("version" in pack) { if ("version" in pack) {
pack["version"] = "0.0.0"; pack["version"] = "0.0.0";
}
}
for (const prefix of ["", "build-", "dev-"]) {
const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue;
}
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
} catch (_e) {
// Not an object, probably a string (version),
// continue.
continue;
} }
} }
for (const prefix of ["", "build-", "dev-"]) {
const section_name = `${prefix}dependencies`;
if (!(section_name in parsed)) {
continue;
}
const deps = parsed[section_name];
for (const key of Object.keys(deps)) {
const dep = deps[key];
try {
if ("path" in dep) {
dep.version = "0.0.0";
dep.path = "";
}
} catch (_e) {
// Not an object, probably a string (version),
// continue.
continue;
}
}
}
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
} catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_manifest);
} }
hasher.update(JSON.stringify(parsed));
parsedKeyFiles.push(cargo_manifest);
} catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.toml manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_manifest);
} }
}
const cargo_lock = path.join(workspace.root, "Cargo.lock"); const cargo_lock = path.join(workspace.root, "Cargo.lock");
if (await exists(cargo_lock)) { if (await exists(cargo_lock)) {
try { try {
const content = await fs_promises.readFile(cargo_lock, { encoding: "utf8" }); const content = await fs_promises.readFile(cargo_lock, { encoding: "utf8" });
const parsed = toml.parse(content); const parsed = toml.parse(content);
if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) { if ((parsed.version !== 3 && parsed.version !== 4) || !("package" in parsed)) {
// Fallback to caching them as regular file since this action // Fallback to caching them as regular file since this action
// can only handle Cargo.lock format version 3 // can only handle Cargo.lock format version 3
core.warning("Unsupported Cargo.lock format, fallback to caching entire file"); core.warning("Unsupported Cargo.lock format, fallback to caching entire file");
keyFiles.push(cargo_lock);
continue;
}
// Package without `[[package]].source` and `[[package]].checksum`
// are the one with `path = "..."` to crates within the workspace.
const packages = (parsed.package as any[]).filter((p: any) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
} catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock); keyFiles.push(cargo_lock);
continue;
} }
// Package without `[[package]].source` and `[[package]].checksum`
// are the one with `path = "..."` to crates within the workspace.
const packages = (parsed.package as any[]).filter((p: any) => "source" in p || "checksum" in p);
hasher.update(JSON.stringify(packages));
parsedKeyFiles.push(cargo_lock);
} catch (e) {
// Fallback to caching them as regular file
core.warning(`Error parsing Cargo.lock manifest, fallback to caching entire file: ${e}`);
keyFiles.push(cargo_lock);
} }
} }
} keyFiles = sort_and_uniq(keyFiles);
keyFiles = sort_and_uniq(keyFiles);
for (const file of keyFiles) { for (const file of keyFiles) {
for await (const chunk of fs.createReadStream(file)) { for await (const chunk of fs.createReadStream(file)) {
hasher.update(chunk); hasher.update(chunk);
}
} }
}
keyFiles.push(...parsedKeyFiles); keyFiles.push(...parsedKeyFiles);
self.keyFiles = sort_and_uniq(keyFiles); self.keyFiles = sort_and_uniq(keyFiles);
// Add lock hash suffix if 'add-rust-environment-hash-key' is true
if (core.getInput("add-rust-environment-hash-key").toLowerCase() == "true") {
let lockHash = digest(hasher); let lockHash = digest(hasher);
key += `-${lockHash}`; key += `-${lockHash}`;
} }