fix(compiler-cli): resolve `rootDirs` to absolute (#41359)

Ensure that `rootDirs` are absolute by resolving them against the current working directory.

Fixes #36290

PR Close #41359
This commit is contained in:
Benjamin Kindle 2021-04-10 16:27:17 -04:00 committed by Zach Arend
parent 6ba67c6fff
commit 3e0fda96b8
2 changed files with 35 additions and 3 deletions

View File

@ -10,7 +10,7 @@ const TS = /\.tsx?$/i;
const D_TS = /\.d\.ts$/i;
import * as ts from 'typescript';
import {AbsoluteFsPath, absoluteFrom} from '../../file_system';
import {AbsoluteFsPath, getFileSystem} from '../../file_system';
import {DeclarationNode} from '../../reflection';
export function isDtsPath(filePath: string): boolean {
@ -96,19 +96,21 @@ export function getRootDirs(
host: Pick<ts.CompilerHost, 'getCurrentDirectory'|'getCanonicalFileName'>,
options: ts.CompilerOptions): AbsoluteFsPath[] {
const rootDirs: string[] = [];
const cwd = host.getCurrentDirectory();
const fs = getFileSystem();
if (options.rootDirs !== undefined) {
rootDirs.push(...options.rootDirs);
} else if (options.rootDir !== undefined) {
rootDirs.push(options.rootDir);
} else {
rootDirs.push(host.getCurrentDirectory());
rootDirs.push(cwd);
}
// In Windows the above might not always return posix separated paths
// See:
// https://github.com/Microsoft/TypeScript/blob/3f7357d37f66c842d70d835bc925ec2a873ecfec/src/compiler/sys.ts#L650
// Also compiler options might be set via an API which doesn't normalize paths
return rootDirs.map(rootDir => absoluteFrom(host.getCanonicalFileName(rootDir)));
return rootDirs.map(rootDir => fs.resolve(cwd, host.getCanonicalFileName(rootDir)));
}
export function nodeDebugInfo(node: ts.Node): string {

View File

@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {FileSystem, getFileSystem} from '../../file_system';
import {runInEachFileSystem} from '../../file_system/testing';
import {getRootDirs} from '../src/typescript';
runInEachFileSystem(() => {
let fs: FileSystem;
beforeEach(() => {
fs = getFileSystem();
});
describe('typescript', () => {
it('should allow relative root directories', () => {
const mockCompilerHost = {
getCanonicalFileName: (val: string) => val,
getCurrentDirectory: () => '/fs-root/projects'
};
const result = getRootDirs(mockCompilerHost, {rootDir: './test-project-root'});
expect(result).toEqual([fs.resolve('/fs-root/projects/test-project-root')]);
});
});
});