diff --git a/packages/compiler-cli/src/ngtsc/path/src/logical.ts b/packages/compiler-cli/src/ngtsc/path/src/logical.ts index 40b2502c13..e00a74f4ed 100644 --- a/packages/compiler-cli/src/ngtsc/path/src/logical.ts +++ b/packages/compiler-cli/src/ngtsc/path/src/logical.ts @@ -81,7 +81,7 @@ export class LogicalFileSystem { let logicalFile: LogicalProjectPath|null = null; for (const rootDir of this.rootDirs) { if (physicalFile.startsWith(rootDir)) { - logicalFile = stripExtension(physicalFile.substr(rootDir.length)) as LogicalProjectPath; + logicalFile = this.createLogicalProjectPath(physicalFile, rootDir); // The logical project does not include any special "node_modules" nested directories. if (logicalFile.indexOf('/node_modules/') !== -1) { logicalFile = null; @@ -94,4 +94,10 @@ export class LogicalFileSystem { } return this.cache.get(physicalFile) !; } + + private createLogicalProjectPath(file: AbsoluteFsPath, rootDir: AbsoluteFsPath): + LogicalProjectPath { + const logicalPath = stripExtension(file.substr(rootDir.length)); + return (logicalPath.startsWith('/') ? logicalPath : '/' + logicalPath) as LogicalProjectPath; + } } diff --git a/packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts b/packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts index f48713e39d..b2a8711cdb 100644 --- a/packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts +++ b/packages/compiler-cli/src/ngtsc/path/test/logical_spec.ts @@ -31,6 +31,16 @@ describe('logical paths', () => { expect(fs.logicalPathOfFile(abs('/test/foo.ts'))).toEqual('/foo' as LogicalProjectPath); expect(fs.logicalPathOfFile(abs('/test/dist/foo.ts'))).toEqual('/foo' as LogicalProjectPath); }); + + it('should always return `/` prefixed logical paths', () => { + const rootFs = new LogicalFileSystem([abs('/')]); + expect(rootFs.logicalPathOfFile(abs('/foo/foo.ts'))) + .toEqual('/foo/foo' as LogicalProjectPath); + + const nonRootFs = new LogicalFileSystem([abs('/test/')]); + expect(nonRootFs.logicalPathOfFile(abs('/test/foo/foo.ts'))) + .toEqual('/foo/foo' as LogicalProjectPath); + }); }); describe('utilities', () => {