Skip to content

Commit

Permalink
only hash Cargo.toml/Cargo.lock that belong to a configured workspace (
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Nov 5, 2022
1 parent b5ec9ed commit ccdddcc
Showing 1 changed file with 30 additions and 18 deletions.
48 changes: 30 additions & 18 deletions src/config.ts
Expand Up @@ -118,42 +118,54 @@ export class CacheConfig {
let lockHash = core.getState(STATE_LOCKFILE_HASH);
let keyFiles: Array<string> = JSON.parse(core.getState(STATE_LOCKFILES) || "[]");

// Constructs the workspace config and paths to restore:
// The workspaces are given using a `$workspace -> $target` syntax.

const workspaces: Array<Workspace> = [];
const workspacesInput = core.getInput("workspaces") || ".";
for (const workspace of workspacesInput.trim().split("\n")) {
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
root = path.resolve(root);
target = path.join(root, target);
workspaces.push(new Workspace(root, target));
}
self.workspaces = workspaces;

if (!lockHash) {
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", {
followSymbolicLinks: false,
});
keyFiles = await globber.glob();
hasher = crypto.createHash("sha1");

async function globHash(pattern: string): Promise<string[]> {
const globber = await glob.create(pattern, {
followSymbolicLinks: false,
});
return await globber.glob();
}

keyFiles = keyFiles.concat(await globHash("rust-toolchain\nrust-toolchain.toml"));
for (const workspace of workspaces) {
const root = workspace.root;
keyFiles = keyFiles.concat(await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`));
}

keyFiles.sort((a, b) => a.localeCompare(b));

hasher = crypto.createHash("sha1");
for (const file of keyFiles) {
for await (const chunk of fs.createReadStream(file)) {
hasher.update(chunk);
}
}

lockHash = hasher.digest("hex");

core.saveState(STATE_LOCKFILE_HASH, lockHash);
core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles));
}

self.keyFiles = keyFiles;

key += `-${lockHash}`;
self.cacheKey = key;

// Constructs the workspace config and paths to restore:
// The workspaces are given using a `$workspace -> $target` syntax.

const workspaces: Array<Workspace> = [];
const workspacesInput = core.getInput("workspaces") || ".";
for (const workspace of workspacesInput.trim().split("\n")) {
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
root = path.resolve(root);
target = path.join(root, target);
workspaces.push(new Workspace(root, target));
}
self.workspaces = workspaces;

self.cachePaths = [CARGO_HOME];
const cacheTargets = core.getInput("cache-targets").toLowerCase();
if (cacheTargets === "true") {
Expand Down

0 comments on commit ccdddcc

Please sign in to comment.