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 c7a5651
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
15 changes: 9 additions & 6 deletions 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<string, string>;
Expand Down Expand Up @@ -61,16 +61,19 @@ 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 normalizedWorkspaceRoot = normalizePath(workspaceRoot);
const relativePath = normalizePath(
path.startsWith(normalizedWorkspaceRoot)
? path.substring(normalizedWorkspaceRoot.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(normalizedWorkspaceRoot, relativePath)
);
} catch {
return '';
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/core/hasher/node-based-file-hasher.ts
Expand Up @@ -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();
Expand Down Expand Up @@ -43,7 +44,7 @@ export class NodeBasedFileHasher extends FileHasherBase {
try {
readdirSync(absoluteDirName).forEach((c) => {
const absoluteChild = join(absoluteDirName, c);
const relChild = relative(appRootPath, absoluteChild);
const relChild = normalizePath(relative(appRootPath, absoluteChild));
if (this.ignoredGlobs.ignores(relChild)) {
return;
}
Expand Down

0 comments on commit c7a5651

Please sign in to comment.