Skip to content
Permalink
Browse files

fix(compiler-cli): ensure LogicalProjectPaths always start with a sla…

…sh (#29627)

Previously, if a matching rootDir ended with a slash then the path
returned from `logicalPathOfFile()` would not start with a slash,
which is inconsistent.

PR Close #29627
  • Loading branch information...
petebacondarwin authored and IgorMinar committed Apr 1, 2019
1 parent ec56354 commit e02684e609f128396f9e77cd777d4f12c8f566e9
@@ -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;
}
}
@@ -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', () => {

0 comments on commit e02684e

Please sign in to comment.
You can’t perform that action at this time.