Skip to content

Commit

Permalink
fix(core): do not validate remote cache validity (#19059)
Browse files Browse the repository at this point in the history
(cherry picked from commit f71f2ac)
  • Loading branch information
FrozenPandaz committed Sep 7, 2023
1 parent 2194422 commit 1f9dc40
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions packages/nx/src/tasks-runner/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export class Cache {
const res = await this.getFromLocalDir(task);

if (res) {
await this.assertLocalCacheValidity(task);
return { ...res, remote: false };
} else if (this.options.remoteCache) {
// didn't find it locally but we have a remote cache
Expand Down Expand Up @@ -227,31 +228,6 @@ export class Cache {
code = Number(await readFile(join(td, 'code'), 'utf-8'));
} catch {}

let sourceMachineId = null;
try {
sourceMachineId = await readFile(join(td, 'source'), 'utf-8');
} catch {}

if (
sourceMachineId &&
sourceMachineId != (await this.currentMachineId())
) {
if (
process.env.NX_REJECT_UNKNOWN_LOCAL_CACHE != '0' &&
process.env.NX_REJECT_UNKNOWN_LOCAL_CACHE != 'false'
) {
const error = [
`Invalid Cache Directory for Task "${task.id}"`,
`The local cache artifact in "${td}" was not been generated on this machine.`,
`As a result, the cache's content integrity cannot be confirmed, which may make cache restoration potentially unsafe.`,
`If your machine ID has changed since the artifact was cached, run "nx reset" to fix this issue.`,
`Read about the error and how to address it here: https://nx.dev/recipes/troubleshooting/unknown-local-cache`,
``,
].join('\n');
throw new Error(error);
}
}

return {
terminalOutput,
outputsPath: join(td, 'outputs'),
Expand All @@ -262,6 +238,31 @@ export class Cache {
}
}

private async assertLocalCacheValidity(task: Task) {
const td = join(this.cachePath, task.hash);
let sourceMachineId = null;
try {
sourceMachineId = await readFile(join(td, 'source'), 'utf-8');
} catch {}

if (sourceMachineId && sourceMachineId != (await this.currentMachineId())) {
if (
process.env.NX_REJECT_UNKNOWN_LOCAL_CACHE != '0' &&
process.env.NX_REJECT_UNKNOWN_LOCAL_CACHE != 'false'
) {
const error = [
`Invalid Cache Directory for Task "${task.id}"`,
`The local cache artifact in "${td}" was not been generated on this machine.`,
`As a result, the cache's content integrity cannot be confirmed, which may make cache restoration potentially unsafe.`,
`If your machine ID has changed since the artifact was cached, run "nx reset" to fix this issue.`,
`Read about the error and how to address it here: https://nx.dev/recipes/troubleshooting/unknown-local-cache`,
``,
].join('\n');
throw new Error(error);
}
}
}

private createCacheDir() {
mkdirSync(cacheDir, { recursive: true });
return cacheDir;
Expand Down

0 comments on commit 1f9dc40

Please sign in to comment.