Skip to content

Commit

Permalink
fix(js): add default baseUrl when extracting tsconfig.base.json or bu…
Browse files Browse the repository at this point in the history
…ilding 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
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
jaysoo authored Jun 6, 2024
1 parent b2e6662 commit cc023c9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/js/src/utils/typescript/create-ts-config.spec.ts
Original file line number Diff line number Diff line change
@@ -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: '.',
},
});
});
});
4 changes: 4 additions & 0 deletions packages/js/src/utils/typescript/create-ts-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit cc023c9

Please sign in to comment.