From cc023c91f93d279078850328b93dd8965d5587dd Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Thu, 6 Jun 2024 16:10:36 -0400 Subject: [PATCH] fix(js): add default baseUrl when extracting tsconfig.base.json or building libs will fail (#26432) This PR ensures that `"rootDir": "."` is set in `tsconfig.base.json` is set, or else generating libs in a standalone project will fail with and error like this: ``` error TS5090: Non-relative paths are not allowed when 'baseUrl' is not set. Did you forget a leading './'? ``` ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- .../utils/typescript/create-ts-config.spec.ts | 30 +++++++++++++++++++ .../src/utils/typescript/create-ts-config.ts | 4 +++ 2 files changed, 34 insertions(+) create mode 100644 packages/js/src/utils/typescript/create-ts-config.spec.ts diff --git a/packages/js/src/utils/typescript/create-ts-config.spec.ts b/packages/js/src/utils/typescript/create-ts-config.spec.ts new file mode 100644 index 0000000000000..98db533b19d91 --- /dev/null +++ b/packages/js/src/utils/typescript/create-ts-config.spec.ts @@ -0,0 +1,30 @@ +import { readJson, Tree } from '@nx/devkit'; +import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing'; +import { extractTsConfigBase } from './create-ts-config'; + +describe('extractTsConfigBase', () => { + let tree: Tree; + + beforeEach(() => { + tree = createTreeWithEmptyWorkspace(); + }); + + it('should add default rootDir if it was not set', () => { + tree.delete('tsconfig.base.json'); + tree.write( + 'tsconfig.json', + JSON.stringify({ + compilerOptions: {}, + }) + ); + + extractTsConfigBase(tree); + + expect(readJson(tree, 'tsconfig.base.json')).toEqual({ + compileOnSave: false, + compilerOptions: { + baseUrl: '.', + }, + }); + }); +}); diff --git a/packages/js/src/utils/typescript/create-ts-config.ts b/packages/js/src/utils/typescript/create-ts-config.ts index 4bbd3253e9d96..03ab4f894904d 100644 --- a/packages/js/src/utils/typescript/create-ts-config.ts +++ b/packages/js/src/utils/typescript/create-ts-config.ts @@ -31,6 +31,10 @@ export function extractTsConfigBase(host: Tree) { delete tsconfig.compilerOptions[compilerOption]; } } + // If we don't set baseDir then builds will fail when more than one projects exist. + if (typeof baseCompilerOptions.baseUrl === 'undefined') { + baseCompilerOptions.baseUrl = '.'; + } writeJson(host, 'tsconfig.base.json', { compileOnSave: false, compilerOptions: baseCompilerOptions,