From 1d1063fb1a4d5b47c20ddf6098e51a1d8c5eca43 Mon Sep 17 00:00:00 2001 From: Jason Jean Date: Sat, 15 Feb 2020 18:18:47 -0500 Subject: [PATCH] fix(core): recalculate dep-graph when root files are touched --- packages/workspace/src/core/file-utils.ts | 30 ++++++++++++++----- .../src/core/project-graph/project-graph.ts | 2 +- .../workspace/src/tasks-runner/hasher.spec.ts | 3 ++ packages/workspace/src/tasks-runner/hasher.ts | 2 ++ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/packages/workspace/src/core/file-utils.ts b/packages/workspace/src/core/file-utils.ts index 3b47a6e029..9820a6dd6b 100644 --- a/packages/workspace/src/core/file-utils.ts +++ b/packages/workspace/src/core/file-utils.ts @@ -97,6 +97,18 @@ function defaultReadFileAtRevision( } } +function getFileData(filePath: string): FileData { + const stat = fs.statSync(filePath); + return { + file: path + .relative(appRootPath, filePath) + .split(path.sep) + .join('/'), + ext: path.extname(filePath), + mtime: stat.mtimeMs + }; +} + export function allFilesInDir( dirName: string, recurse: boolean = true @@ -118,14 +130,7 @@ export function allFilesInDir( const s = fs.statSync(child); if (!s.isDirectory()) { // add starting with "apps/myapp/..." or "libs/mylib/..." - res.push({ - file: path - .relative(appRootPath, child) - .split(path.sep) - .join('/'), - ext: path.extname(child), - mtime: s.mtimeMs - }); + res.push(getFileData(child)); } else if (s.isDirectory() && recurse) { res = [...res, ...allFilesInDir(child)]; } @@ -182,10 +187,19 @@ export function readNxJson(): NxJson { return config; } +// TODO: Make this list extensible +export function rootWorkspaceFileNames(): string[] { + return [`package.json`, workspaceFileName(), `nx.json`, `tsconfig.json`]; +} + export function readWorkspaceFiles(): FileData[] { const workspaceJson = readWorkspaceJson(); const files = []; + files.push( + ...rootWorkspaceFileNames().map(f => getFileData(`${appRootPath}/${f}`)) + ); + // Add known workspace files and directories files.push(...allFilesInDir(appRootPath, false)); files.push(...allFilesInDir(`${appRootPath}/tools`)); diff --git a/packages/workspace/src/core/project-graph/project-graph.ts b/packages/workspace/src/core/project-graph/project-graph.ts index d95334547e..b4f95f3fbc 100644 --- a/packages/workspace/src/core/project-graph/project-graph.ts +++ b/packages/workspace/src/core/project-graph/project-graph.ts @@ -1,4 +1,4 @@ -import { mkdirSync, readFileSync } from 'fs'; +import { mkdirSync } from 'fs'; import { ProjectGraph } from './project-graph-models'; import { ProjectGraphBuilder } from './project-graph-builder'; import { appRootPath } from '../../utils/app-root'; diff --git a/packages/workspace/src/tasks-runner/hasher.spec.ts b/packages/workspace/src/tasks-runner/hasher.spec.ts index d72de7b183..ffc07c1537 100644 --- a/packages/workspace/src/tasks-runner/hasher.spec.ts +++ b/packages/workspace/src/tasks-runner/hasher.spec.ts @@ -10,6 +10,9 @@ describe('Hasher', () => { hasha.mockImplementation(values => values.join('|')); hasha.fromFile.mockImplementation(path => Promise.resolve(hashes[path])); fs.statSync.mockReturnValue({ size: 100 }); + fs.readFileSync.mockImplementation(() => + JSON.stringify({ dependencies: {}, devDependencies: {} }) + ); }); it('should create project hash', async done => { diff --git a/packages/workspace/src/tasks-runner/hasher.ts b/packages/workspace/src/tasks-runner/hasher.ts index 3190a988a8..ddc71ab617 100644 --- a/packages/workspace/src/tasks-runner/hasher.ts +++ b/packages/workspace/src/tasks-runner/hasher.ts @@ -2,6 +2,7 @@ import { ProjectGraph } from '../core/project-graph'; import { NxJson } from '../core/shared-interfaces'; import { Task } from './tasks-runner'; import { statSync } from 'fs'; +import { rootWorkspaceFileNames } from '../core/file-utils'; const hasha = require('hasha'); @@ -42,6 +43,7 @@ export class Hasher { ...Object.keys(this.nxJson.implicitDependencies || {}).map(r => this.fileHashes.hashFile(r) ), + ...rootWorkspaceFileNames().map(r => this.fileHashes.hashFile(r)), this.fileHashes.hashFile('package-lock.json'), this.fileHashes.hashFile('yarn.lock') ]);