From 68840e2b16408f14329f288224aa95068d057eac Mon Sep 17 00:00:00 2001 From: Benjamin Kindle Date: Sat, 10 Apr 2021 16:27:17 -0400 Subject: [PATCH] fix(compiler-cli): resolve `rootDirs` to absolute Ensure that `rootDirs` are absolute by resolving them against the current working directory. Fixes #36290 --- .../src/ngtsc/util/src/typescript.ts | 8 +++-- .../src/ngtsc/util/test/typescript_spec.ts | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 packages/compiler-cli/src/ngtsc/util/test/typescript_spec.ts diff --git a/packages/compiler-cli/src/ngtsc/util/src/typescript.ts b/packages/compiler-cli/src/ngtsc/util/src/typescript.ts index 63af2e8206efa..6063442aad39b 100644 --- a/packages/compiler-cli/src/ngtsc/util/src/typescript.ts +++ b/packages/compiler-cli/src/ngtsc/util/src/typescript.ts @@ -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, 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 { diff --git a/packages/compiler-cli/src/ngtsc/util/test/typescript_spec.ts b/packages/compiler-cli/src/ngtsc/util/test/typescript_spec.ts new file mode 100644 index 0000000000000..31aa679625d7c --- /dev/null +++ b/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')]); + }); + }); +});