Skip to content

Commit ccdddcc

Browse files
authored
only hash Cargo.toml/Cargo.lock that belong to a configured workspace (#90)
1 parent b5ec9ed commit ccdddcc

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

src/config.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,54 @@ export class CacheConfig {
118118
let lockHash = core.getState(STATE_LOCKFILE_HASH);
119119
let keyFiles: Array<string> = JSON.parse(core.getState(STATE_LOCKFILES) || "[]");
120120

121+
// Constructs the workspace config and paths to restore:
122+
// The workspaces are given using a `$workspace -> $target` syntax.
123+
124+
const workspaces: Array<Workspace> = [];
125+
const workspacesInput = core.getInput("workspaces") || ".";
126+
for (const workspace of workspacesInput.trim().split("\n")) {
127+
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
128+
root = path.resolve(root);
129+
target = path.join(root, target);
130+
workspaces.push(new Workspace(root, target));
131+
}
132+
self.workspaces = workspaces;
133+
121134
if (!lockHash) {
122-
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", {
123-
followSymbolicLinks: false,
124-
});
125-
keyFiles = await globber.glob();
135+
hasher = crypto.createHash("sha1");
136+
137+
async function globHash(pattern: string): Promise<string[]> {
138+
const globber = await glob.create(pattern, {
139+
followSymbolicLinks: false,
140+
});
141+
return await globber.glob();
142+
}
143+
144+
keyFiles = keyFiles.concat(await globHash("rust-toolchain\nrust-toolchain.toml"));
145+
for (const workspace of workspaces) {
146+
const root = workspace.root;
147+
keyFiles = keyFiles.concat(await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock\n${root}/**/rust-toolchain\n${root}/**/rust-toolchain.toml`));
148+
}
149+
126150
keyFiles.sort((a, b) => a.localeCompare(b));
127151

128-
hasher = crypto.createHash("sha1");
129152
for (const file of keyFiles) {
130153
for await (const chunk of fs.createReadStream(file)) {
131154
hasher.update(chunk);
132155
}
133156
}
157+
134158
lockHash = hasher.digest("hex");
135159

136160
core.saveState(STATE_LOCKFILE_HASH, lockHash);
137161
core.saveState(STATE_LOCKFILES, JSON.stringify(keyFiles));
138162
}
139163

140164
self.keyFiles = keyFiles;
165+
141166
key += `-${lockHash}`;
142167
self.cacheKey = key;
143168

144-
// Constructs the workspace config and paths to restore:
145-
// The workspaces are given using a `$workspace -> $target` syntax.
146-
147-
const workspaces: Array<Workspace> = [];
148-
const workspacesInput = core.getInput("workspaces") || ".";
149-
for (const workspace of workspacesInput.trim().split("\n")) {
150-
let [root, target = "target"] = workspace.split("->").map((s) => s.trim());
151-
root = path.resolve(root);
152-
target = path.join(root, target);
153-
workspaces.push(new Workspace(root, target));
154-
}
155-
self.workspaces = workspaces;
156-
157169
self.cachePaths = [CARGO_HOME];
158170
const cacheTargets = core.getInput("cache-targets").toLowerCase();
159171
if (cacheTargets === "true") {

0 commit comments

Comments
 (0)