Skip to content

Commit

Permalink
feat(core): include node_modules into the computation hash key (#2993)
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 13, 2020
1 parent 418fbd5 commit 10282bb
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions packages/workspace/src/tasks-runner/hasher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ProjectGraph } from '../core/project-graph';
import { NxJson } from '../core/shared-interfaces';
import { Task } from './tasks-runner';
import { statSync } from 'fs';
import { statSync, readFileSync } from 'fs';
import { rootWorkspaceFileNames } from '../core/file-utils';
import { execSync } from 'child_process';

const resolve = require('resolve');
const hasha = require('hasha');

export interface Hash {
Expand Down Expand Up @@ -32,9 +32,14 @@ interface RuntimeHashResult {
runtime: { [input: string]: string };
}

interface NodeModulesResult {
value: string;
}

export class Hasher {
static version = '1.0';
implicitDependencies: Promise<ImplicitHashResult>;
nodeModules: Promise<NodeModulesResult>;
runtimeInputs: Promise<RuntimeHashResult>;
fileHashes = new FileHashes();
projectHashes = new ProjectHashes(this.projectGraph, this.fileHashes);
Expand All @@ -56,13 +61,19 @@ export class Hasher {
{ algorithm: 'sha256' }
);

const values = await Promise.all([
const values = (await Promise.all([
this.projectHashes.hashProject(task.target.project, [
task.target.project,
]),
this.implicitDepsHash(),
this.runtimeInputsHash(),
]);
this.nodeModulesHash(),
])) as [
ProjectHashResult,
ImplicitHashResult,
RuntimeHashResult,
NodeModulesResult
];

const value = hasha(
[Hasher.version, command, ...values.map((v) => v.value)],
Expand Down Expand Up @@ -152,6 +163,37 @@ export class Hasher {
});
return this.implicitDependencies;
}

private async nodeModulesHash() {
if (this.nodeModules) return this.nodeModules;

this.nodeModules = Promise.resolve().then(async () => {
try {
const j = JSON.parse(readFileSync('package.json').toString());
const allPackages = [
...Object.keys(j.dependencies),
...Object.keys(j.devDependencies),
];
const packageJsonHashes = await Promise.all(
allPackages.map((d) => {
try {
const path = resolve.sync(`${d}/package.json`, {
basedir: process.cwd(),
});
return this.fileHashes.hashFile(path).catch(() => '');
} catch (e) {
return '';
}
})
);
return { value: await hasha(packageJsonHashes) };
} catch (e) {
return { value: '' };
}
});

return this.nodeModules;
}
}

export class ProjectHashes {
Expand Down

0 comments on commit 10282bb

Please sign in to comment.