Skip to content

Commit 3e0fda9

Browse files
literalpiezarend
authored andcommitted
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
1 parent 6ba67c6 commit 3e0fda9

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

packages/compiler-cli/src/ngtsc/util/src/typescript.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const TS = /\.tsx?$/i;
1010
const D_TS = /\.d\.ts$/i;
1111

1212
import * as ts from 'typescript';
13-
import {AbsoluteFsPath, absoluteFrom} from '../../file_system';
13+
import {AbsoluteFsPath, getFileSystem} from '../../file_system';
1414
import {DeclarationNode} from '../../reflection';
1515

1616
export function isDtsPath(filePath: string): boolean {
@@ -96,19 +96,21 @@ export function getRootDirs(
9696
host: Pick<ts.CompilerHost, 'getCurrentDirectory'|'getCanonicalFileName'>,
9797
options: ts.CompilerOptions): AbsoluteFsPath[] {
9898
const rootDirs: string[] = [];
99+
const cwd = host.getCurrentDirectory();
100+
const fs = getFileSystem();
99101
if (options.rootDirs !== undefined) {
100102
rootDirs.push(...options.rootDirs);
101103
} else if (options.rootDir !== undefined) {
102104
rootDirs.push(options.rootDir);
103105
} else {
104-
rootDirs.push(host.getCurrentDirectory());
106+
rootDirs.push(cwd);
105107
}
106108

107109
// In Windows the above might not always return posix separated paths
108110
// See:
109111
// https://github.com/Microsoft/TypeScript/blob/3f7357d37f66c842d70d835bc925ec2a873ecfec/src/compiler/sys.ts#L650
110112
// Also compiler options might be set via an API which doesn't normalize paths
111-
return rootDirs.map(rootDir => absoluteFrom(host.getCanonicalFileName(rootDir)));
113+
return rootDirs.map(rootDir => fs.resolve(cwd, host.getCanonicalFileName(rootDir)));
112114
}
113115

114116
export function nodeDebugInfo(node: ts.Node): string {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {FileSystem, getFileSystem} from '../../file_system';
10+
import {runInEachFileSystem} from '../../file_system/testing';
11+
import {getRootDirs} from '../src/typescript';
12+
13+
runInEachFileSystem(() => {
14+
let fs: FileSystem;
15+
16+
beforeEach(() => {
17+
fs = getFileSystem();
18+
});
19+
20+
describe('typescript', () => {
21+
it('should allow relative root directories', () => {
22+
const mockCompilerHost = {
23+
getCanonicalFileName: (val: string) => val,
24+
getCurrentDirectory: () => '/fs-root/projects'
25+
};
26+
const result = getRootDirs(mockCompilerHost, {rootDir: './test-project-root'});
27+
expect(result).toEqual([fs.resolve('/fs-root/projects/test-project-root')]);
28+
});
29+
});
30+
});

0 commit comments

Comments
 (0)