Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only hash Cargo.toml/Cargo.lock that belong to a configured workspace #90

Merged
merged 4 commits into from Nov 5, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/config.ts
Expand Up @@ -118,19 +118,41 @@ export class CacheConfig {
let lockHash = core.getState(STATE_LOCKFILE_HASH);
let keyFiles: Array<string> = JSON.parse(core.getState(STATE_LOCKFILES) || "[]");

if (!lockHash) {
const globber = await glob.create("**/Cargo.toml\n**/Cargo.lock\nrust-toolchain\nrust-toolchain.toml", {
followSymbolicLinks: false,
});
keyFiles = await globber.glob();
keyFiles.sort((a, b) => a.localeCompare(b));
// 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) {
hasher = crypto.createHash("sha1");
for (const file of keyFiles) {
for await (const chunk of fs.createReadStream(file)) {
hasher.update(chunk);

async function globHash(pattern: string): Promise<void> {
const globber = await glob.create(pattern, {
followSymbolicLinks: false,
});
keyFiles = keyFiles.concat(await globber.glob());
keyFiles.sort((a, b) => a.localeCompare(b));

for (const file of keyFiles) {
for await (const chunk of fs.createReadStream(file)) {
hasher.update(chunk);
}
}
}

await globHash("rust-toolchain\nrust-toolchain.toml");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m wondering, is the rust-toolchain file something workspace specific? I think so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, cargo uses the closest one but it'll search all the way to the root.

for (const workspace of workspaces) {
const root = workspace.root;
await globHash(`${root}/**/Cargo.toml\n${root}/**/Cargo.lock`);
}
lockHash = hasher.digest("hex");

core.saveState(STATE_LOCKFILE_HASH, lockHash);
Expand All @@ -141,19 +163,6 @@ export class CacheConfig {
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