Skip to content

Commit 0b64bee

Browse files
alan-agius4clydin
authored andcommittedMay 12, 2020
fix(@ngtools/webpack): getCanonicalFileName should return FS compatible paths
Unlike TSC which has it's own mechanism the resolve and join paths in POSIX format, NGTSC heavily relies onNode.JS `fs` and `path` modules. This prevents `Path` usage because in Windows `path.resolve` will causes an absolute path to be resolved or joined incorrectly. Example: `/D/MyPath/MyProject` -> `D:/d/mypath/myproject`. With this change we change the `getCanonicalFileName` method to return FS compatible paths. (cherry picked from commit e8e832e)
1 parent 901fdc5 commit 0b64bee

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed
 

‎packages/ngtools/webpack/src/compiler_host.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ export class WebpackCompilerHost implements ts.CompilerHost {
359359
}
360360

361361
getCanonicalFileName(fileName: string): string {
362-
const path = this.resolve(fileName);
362+
const path = workaroundResolve(this.resolve(fileName));
363363

364364
return this.useCaseSensitiveFileNames() ? path : path.toLowerCase();
365365
}

‎packages/ngtools/webpack/src/utils.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import { Path, getSystemPath, normalize } from '@angular-devkit/core';
99

1010
// `TsCompilerAotCompilerTypeCheckHostAdapter` in @angular/compiler-cli seems to resolve module
1111
// names directly via `resolveModuleName`, which prevents full Path usage.
12+
// NSTSC also uses Node.JS `path.resolve` which will result in incorrect paths in Windows
13+
// Example: `/D/MyPath/MyProject` -> `D:/d/mypath/myproject`
1214
// To work around this we must provide the same path format as TS internally uses in
1315
// the SourceFile paths.
14-
export function workaroundResolve(path: Path | string) {
16+
export function workaroundResolve(path: Path | string): string {
1517
return forwardSlashPath(getSystemPath(normalize(path)));
1618
}
1719

@@ -20,6 +22,6 @@ export function flattenArray<T>(value: Array<T | T[]>): T[] {
2022
}
2123

2224
// TS represents paths internally with '/' and expects paths to be in this format.
23-
export function forwardSlashPath(path: string) {
25+
export function forwardSlashPath(path: string): string {
2426
return path.replace(/\\/g, '/');
2527
}

0 commit comments

Comments
 (0)
Please sign in to comment.