Skip to content

Commit

Permalink
fix(compiler-cli): resolve rootDirs to absolute
Browse files Browse the repository at this point in the history
Ensure that `rootDirs` are absolute by resolving them against the current working directory.

Fixes #36290
  • Loading branch information
literalpie committed Apr 10, 2021
1 parent 61bfa3d commit 68840e2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/compiler-cli/src/ngtsc/util/src/typescript.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
30 changes: 30 additions & 0 deletions packages/compiler-cli/src/ngtsc/util/test/typescript_spec.ts
@@ -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')]);
});
});
});

0 comments on commit 68840e2

Please sign in to comment.