Skip to content

Commit

Permalink
fix(@ngtools/webpack): getCanonicalFileName should return FS compatib…
Browse files Browse the repository at this point in the history
…le 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)
  • Loading branch information
alan-agius4 authored and clydin committed May 12, 2020
1 parent 901fdc5 commit 0b64bee
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/ngtools/webpack/src/compiler_host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ export class WebpackCompilerHost implements ts.CompilerHost {
}

getCanonicalFileName(fileName: string): string {
const path = this.resolve(fileName);
const path = workaroundResolve(this.resolve(fileName));

return this.useCaseSensitiveFileNames() ? path : path.toLowerCase();
}
Expand Down
6 changes: 4 additions & 2 deletions packages/ngtools/webpack/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { Path, getSystemPath, normalize } from '@angular-devkit/core';

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

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

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

0 comments on commit 0b64bee

Please sign in to comment.