From 9123665bed7f527bb5de10d9abc3d0ec069a0f55 Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Tue, 29 Mar 2022 14:16:24 -0400 Subject: [PATCH] fix(core): normalize file path for node-hasher (#9585) Node hasher paths need to be normalized, since on windows they return with '\' instead of '/' and don't match keys in workspace configuration. Fixes #9584 Fixes #9581 --- .../rules/enforce-module-boundaries.spec.ts | 2 ++ packages/nx/src/command-line/report.spec.ts | 1 + .../affected-project-graph.spec.ts | 1 + .../nx/src/core/hasher/file-hasher-base.ts | 14 ++++++++------ packages/nx/src/core/hasher/hasher.spec.ts | 1 + .../src/core/hasher/node-based-file-hasher.ts | 18 +++++++++++------- .../explicit-package-json-dependencies.spec.ts | 1 + .../explicit-project-dependencies.spec.ts | 1 + .../implict-project-dependencies.spec.ts | 1 + .../project-graph/build-project-graph.spec.ts | 1 + .../nx/src/core/target-project-locator.spec.ts | 1 + .../nxEnforceModuleBoundariesRule.spec.ts | 5 ++++- 12 files changed, 33 insertions(+), 14 deletions(-) diff --git a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts index 6386ab45a34d0..7852ec9ecb1b8 100644 --- a/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts +++ b/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.spec.ts @@ -14,10 +14,12 @@ jest.mock('fs', () => require('memfs').fs); jest.mock('@nrwl/devkit', () => ({ ...jest.requireActual('@nrwl/devkit'), appRootPath: '/root', + workspaceRoot: '/root', })); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); const tsconfig = { diff --git a/packages/nx/src/command-line/report.spec.ts b/packages/nx/src/command-line/report.spec.ts index 2130989c0ed26..4de781ea65f9c 100644 --- a/packages/nx/src/command-line/report.spec.ts +++ b/packages/nx/src/command-line/report.spec.ts @@ -5,6 +5,7 @@ import { join } from 'path'; jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '', + workspaceRoot: '', })); jest.mock('../utils/fileutils', () => ({ diff --git a/packages/nx/src/core/affected-project-graph/affected-project-graph.spec.ts b/packages/nx/src/core/affected-project-graph/affected-project-graph.spec.ts index 9eb03646f2d13..7d34048e537cb 100644 --- a/packages/nx/src/core/affected-project-graph/affected-project-graph.spec.ts +++ b/packages/nx/src/core/affected-project-graph/affected-project-graph.spec.ts @@ -11,6 +11,7 @@ import { stripIndents } from '../../utils/strip-indents'; jest.mock('fs', () => require('memfs').fs); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); describe('project graph', () => { diff --git a/packages/nx/src/core/hasher/file-hasher-base.ts b/packages/nx/src/core/hasher/file-hasher-base.ts index 59609de064aeb..0c38a6788595f 100644 --- a/packages/nx/src/core/hasher/file-hasher-base.ts +++ b/packages/nx/src/core/hasher/file-hasher-base.ts @@ -1,8 +1,8 @@ -import { appRootPath } from 'nx/src/utils/app-root'; +import { workspaceRoot } from 'nx/src/utils/app-root'; import { performance } from 'perf_hooks'; import { defaultHashing } from './hashing-impl'; import { FileData } from 'nx/src/shared/project-graph'; -import { joinPathFragments } from 'nx/src/utils/path'; +import { joinPathFragments, normalizePath } from 'nx/src/utils/path'; export abstract class FileHasherBase { protected fileHashes: Map; @@ -61,16 +61,18 @@ export abstract class FileHasherBase { if (!this.fileHashes) { throw new Error('FileHasher is invoked before being initialized'); } - const relativePath = path.startsWith(appRootPath) - ? path.substr(appRootPath.length + 1) - : path; + const relativePath = normalizePath( + path.startsWith(workspaceRoot) + ? path.substring(workspaceRoot.length + 1) + : path + ); if (this.fileHashes.has(relativePath)) { return this.fileHashes.get(relativePath); } else { try { // this has to be absolute to avoid issues with cwd return defaultHashing.hashFile( - joinPathFragments(appRootPath, relativePath) + joinPathFragments(workspaceRoot, relativePath) ); } catch { return ''; diff --git a/packages/nx/src/core/hasher/hasher.spec.ts b/packages/nx/src/core/hasher/hasher.spec.ts index 085a186b22cb1..62755310cf7cf 100644 --- a/packages/nx/src/core/hasher/hasher.spec.ts +++ b/packages/nx/src/core/hasher/hasher.spec.ts @@ -4,6 +4,7 @@ import { DependencyType } from 'nx/src/shared/project-graph'; jest.doMock('../../utils/app-root', () => { return { appRootPath: '', + workspaceRoot: '', }; }); diff --git a/packages/nx/src/core/hasher/node-based-file-hasher.ts b/packages/nx/src/core/hasher/node-based-file-hasher.ts index c8cea4161ecdc..5053f9a6299c1 100644 --- a/packages/nx/src/core/hasher/node-based-file-hasher.ts +++ b/packages/nx/src/core/hasher/node-based-file-hasher.ts @@ -1,4 +1,4 @@ -import { appRootPath } from 'nx/src/utils/app-root'; +import { workspaceRoot } from 'nx/src/utils/app-root'; import { performance } from 'perf_hooks'; import { FileData } from 'nx/src/shared/project-graph'; import { join, relative } from 'path'; @@ -6,6 +6,7 @@ import { existsSync, readdirSync, readFileSync, statSync } from 'fs'; import { FileHasherBase } from './file-hasher-base'; import { stripIndents } from '../../utils/strip-indents'; import ignore from 'ignore'; +import { normalizePath } from 'nx/src/utils/path'; export class NodeBasedFileHasher extends FileHasherBase { ignoredGlobs = getIgnoredGlobs(); @@ -14,7 +15,7 @@ export class NodeBasedFileHasher extends FileHasherBase { performance.mark('init hashing:start'); this.clear(); - this.allFilesInDir(appRootPath, true); + this.allFilesInDir(workspaceRoot, true); performance.mark('init hashing:end'); performance.measure( @@ -36,21 +37,24 @@ export class NodeBasedFileHasher extends FileHasherBase { absoluteDirName: string, recurse: boolean = true ): FileData[] { - const relDirName = relative(appRootPath, absoluteDirName); + const relDirName = relative(workspaceRoot, absoluteDirName); if (relDirName && this.ignoredGlobs.ignores(relDirName)) { return; } try { readdirSync(absoluteDirName).forEach((c) => { const absoluteChild = join(absoluteDirName, c); - const relChild = relative(appRootPath, absoluteChild); + const relChild = relative(workspaceRoot, absoluteChild); if (this.ignoredGlobs.ignores(relChild)) { return; } try { const s = statSync(absoluteChild); if (!s.isDirectory()) { - this.fileHashes.set(relChild, this.hashFile(relChild)); + this.fileHashes.set( + normalizePath(relChild), + this.hashFile(relChild) + ); } else if (s.isDirectory() && recurse) { this.allFilesInDir(absoluteChild, true); } @@ -62,8 +66,8 @@ export class NodeBasedFileHasher extends FileHasherBase { function getIgnoredGlobs() { const ig = ignore(); - ig.add(readFileIfExisting(`${appRootPath}/.gitignore`)); - ig.add(readFileIfExisting(`${appRootPath}/.nxignore`)); + ig.add(readFileIfExisting(`${workspaceRoot}/.gitignore`)); + ig.add(readFileIfExisting(`${workspaceRoot}/.nxignore`)); ig.add(stripIndents` node_modules tmp diff --git a/packages/nx/src/core/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts b/packages/nx/src/core/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts index fa09dbe8f192c..35407dbdbf307 100644 --- a/packages/nx/src/core/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts +++ b/packages/nx/src/core/project-graph/build-dependencies/explicit-package-json-dependencies.spec.ts @@ -11,6 +11,7 @@ import { ProjectGraphBuilder } from '../project-graph-builder'; jest.mock('fs', () => require('memfs').fs); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); describe('explicit package json dependencies', () => { diff --git a/packages/nx/src/core/project-graph/build-dependencies/explicit-project-dependencies.spec.ts b/packages/nx/src/core/project-graph/build-dependencies/explicit-project-dependencies.spec.ts index d87d8a9204f8c..869605995d5a9 100644 --- a/packages/nx/src/core/project-graph/build-dependencies/explicit-project-dependencies.spec.ts +++ b/packages/nx/src/core/project-graph/build-dependencies/explicit-project-dependencies.spec.ts @@ -3,6 +3,7 @@ import { createProjectFileMap } from 'nx/src/core/file-map-utils'; jest.mock('fs', () => require('memfs').fs); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); import { vol } from 'memfs'; diff --git a/packages/nx/src/core/project-graph/build-dependencies/implict-project-dependencies.spec.ts b/packages/nx/src/core/project-graph/build-dependencies/implict-project-dependencies.spec.ts index 71400a5edcc5f..4efebc4764119 100644 --- a/packages/nx/src/core/project-graph/build-dependencies/implict-project-dependencies.spec.ts +++ b/packages/nx/src/core/project-graph/build-dependencies/implict-project-dependencies.spec.ts @@ -5,6 +5,7 @@ import { buildImplicitProjectDependencies } from './implicit-project-dependencie jest.mock('fs', () => require('memfs').fs); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); describe('explicit project dependencies', () => { diff --git a/packages/nx/src/core/project-graph/build-project-graph.spec.ts b/packages/nx/src/core/project-graph/build-project-graph.spec.ts index 95f1fd13531e6..f719cf7e3f65c 100644 --- a/packages/nx/src/core/project-graph/build-project-graph.spec.ts +++ b/packages/nx/src/core/project-graph/build-project-graph.spec.ts @@ -3,6 +3,7 @@ import { vol, fs } from 'memfs'; jest.mock('fs', () => require('memfs').fs); jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); import { buildProjectGraph } from './build-project-graph'; import { defaultFileHasher } from '../hasher/file-hasher'; diff --git a/packages/nx/src/core/target-project-locator.spec.ts b/packages/nx/src/core/target-project-locator.spec.ts index e869dfaa74f34..8b2f37098a34e 100644 --- a/packages/nx/src/core/target-project-locator.spec.ts +++ b/packages/nx/src/core/target-project-locator.spec.ts @@ -8,6 +8,7 @@ import { jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root', + workspaceRoot: '/root', })); jest.mock('fs', () => require('memfs').fs); diff --git a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.spec.ts b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.spec.ts index 6b3610f43fec7..e28a6f8bfd19d 100644 --- a/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.spec.ts +++ b/packages/workspace/src/tslint/nxEnforceModuleBoundariesRule.spec.ts @@ -7,7 +7,10 @@ import { TargetProjectLocator } from 'nx/src/core/target-project-locator'; import { mapProjectGraphFiles } from '@nrwl/workspace/src/utils/runtime-lint-utils'; jest.mock('fs', () => require('memfs').fs); -jest.mock('nx/src/utils/app-root', () => ({ appRootPath: '/root' })); +jest.mock('nx/src/utils/app-root', () => ({ + appRootPath: '/root', + workspaceRoot: '/root', +})); const tsconfig = { compilerOptions: {