Skip to content

Commit

Permalink
fix(core): normalize file path for node-hasher
Browse files Browse the repository at this point in the history
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
  • Loading branch information
AgentEnder committed Mar 29, 2022
1 parent 0ad3eb3 commit 842c9c9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions packages/nx/src/core/hasher/file-hasher-base.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 { defaultHashing } from './hashing-impl';
import { FileData } from 'nx/src/shared/project-graph';
Expand Down Expand Up @@ -61,16 +61,16 @@ 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)
const relativePath = path.startsWith(workspaceRoot)
? path.substr(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 '';
Expand Down
15 changes: 8 additions & 7 deletions packages/nx/src/core/hasher/node-based-file-hasher.ts
@@ -1,11 +1,12 @@
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';
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();
Expand All @@ -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(
Expand All @@ -36,21 +37,21 @@ 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);
}
Expand All @@ -62,8 +63,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
Expand Down

0 comments on commit 842c9c9

Please sign in to comment.