diff --git a/packages/compiler-cli/ngcc/src/dependencies/module_resolver.ts b/packages/compiler-cli/ngcc/src/dependencies/module_resolver.ts index e8c12e361d..c558a2bb97 100644 --- a/packages/compiler-cli/ngcc/src/dependencies/module_resolver.ts +++ b/packages/compiler-cli/ngcc/src/dependencies/module_resolver.ts @@ -118,7 +118,7 @@ export class ModuleResolver { */ private resolveAsEntryPoint(moduleName: string, fromPath: AbsoluteFsPath): ResolvedModule|null { let folder = fromPath; - while (folder !== '/') { + while (!AbsoluteFsPath.isRoot(folder)) { folder = AbsoluteFsPath.dirname(folder); if (folder.endsWith('node_modules')) { // Skip up if the folder already ends in node_modules @@ -225,7 +225,7 @@ export class ModuleResolver { */ private findPackagePath(path: AbsoluteFsPath): AbsoluteFsPath|null { let folder = path; - while (folder !== '/') { + while (!AbsoluteFsPath.isRoot(folder)) { folder = AbsoluteFsPath.dirname(folder); if (this.fs.exists(AbsoluteFsPath.join(folder, 'package.json'))) { return folder; diff --git a/packages/compiler-cli/ngcc/src/packages/ngcc_compiler_host.ts b/packages/compiler-cli/ngcc/src/packages/ngcc_compiler_host.ts index 19ddb626dd..d437a51945 100644 --- a/packages/compiler-cli/ngcc/src/packages/ngcc_compiler_host.ts +++ b/packages/compiler-cli/ngcc/src/packages/ngcc_compiler_host.ts @@ -27,7 +27,7 @@ export class NgccCompilerHost implements ts.CompilerHost { } getDefaultLibLocation(): string { - const nodeLibPath = AbsoluteFsPath.fromUnchecked(require.resolve('typescript')); + const nodeLibPath = AbsoluteFsPath.from(require.resolve('typescript')); return AbsoluteFsPath.join(nodeLibPath, '..'); } diff --git a/packages/compiler-cli/ngcc/test/helpers/mock_file_system.ts b/packages/compiler-cli/ngcc/test/helpers/mock_file_system.ts index c47e2f359e..3e8fd219b4 100644 --- a/packages/compiler-cli/ngcc/test/helpers/mock_file_system.ts +++ b/packages/compiler-cli/ngcc/test/helpers/mock_file_system.ts @@ -14,7 +14,7 @@ import {FileStats, FileSystem} from '../../src/file_system/file_system'; export class MockFileSystem implements FileSystem { files: Folder = {}; constructor(...folders: Folder[]) { - folders.forEach(files => this.processFiles(this.files, files)); + folders.forEach(files => this.processFiles(this.files, files, true)); } exists(path: AbsoluteFsPath): boolean { return this.findFromPath(path) !== null; } @@ -67,7 +67,7 @@ export class MockFileSystem implements FileSystem { return new MockFileStats(fileOrFolder); } - pwd(): AbsoluteFsPath { return AbsoluteFsPath.fromUnchecked('/'); } + pwd(): AbsoluteFsPath { return AbsoluteFsPath.from('/'); } copyFile(from: AbsoluteFsPath, to: AbsoluteFsPath): void { this.writeFile(to, this.readFile(from)); @@ -82,9 +82,10 @@ export class MockFileSystem implements FileSystem { ensureDir(path: AbsoluteFsPath): void { this.ensureFolders(this.files, path.split('/')); } - private processFiles(current: Folder, files: Folder): void { + private processFiles(current: Folder, files: Folder, isRootPath = false): void { Object.keys(files).forEach(path => { - const segments = path.split('/'); + const pathResolved = isRootPath ? AbsoluteFsPath.from(path) : path; + const segments = pathResolved.split('/'); const lastSegment = segments.pop() !; const containingFolder = this.ensureFolders(current, segments); const entity = files[path]; diff --git a/packages/compiler-cli/src/ngtsc/path/src/types.ts b/packages/compiler-cli/src/ngtsc/path/src/types.ts index 6561d7e2fe..7286aeaaa7 100644 --- a/packages/compiler-cli/src/ngtsc/path/src/types.ts +++ b/packages/compiler-cli/src/ngtsc/path/src/types.ts @@ -40,6 +40,12 @@ export const AbsoluteFsPath = { * Convert the path `str` to an `AbsoluteFsPath`, throwing an error if it's not an absolute path. */ from: function(str: string): AbsoluteFsPath { + if (str.startsWith('/') && process.platform === 'win32') { + // in Windows if it's absolute path and starts with `/` we shall + // resolve it and return it including the drive. + str = path.resolve(str); + } + const normalized = normalizeSeparators(str); if (!isAbsolutePath(normalized)) { throw new Error(`Internal Error: AbsoluteFsPath.from(${str}): path is not absolute`); @@ -80,6 +86,9 @@ export const AbsoluteFsPath = { */ resolve: function(basePath: string, ...paths: string[]): AbsoluteFsPath { return AbsoluteFsPath.from(path.resolve(basePath, ...paths));}, + + /** Returns true when the path provided is the root path. */ + isRoot: function(path: AbsoluteFsPath): boolean { return AbsoluteFsPath.dirname(path) === path;}, }; /**