Skip to content

Commit 160800e

Browse files
authored
fix(js): do not set project references to non-existing tsconfig files in sync generator (#29536)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## 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 #
1 parent 97e1e3b commit 160800e

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

packages/js/src/generators/typescript-sync/typescript-sync.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,53 @@ describe('syncGenerator()', () => {
659659
`);
660660
});
661661

662+
it('should not add a reference if dependent project does not have a tsconfig files', async () => {
663+
addProject('foo', ['bar'], ['tsconfig.lib.json']);
664+
addProject('bar');
665+
tree.delete('packages/bar/tsconfig.json');
666+
667+
await syncGenerator(tree);
668+
669+
expect(tree.read('tsconfig.json').toString('utf-8'))
670+
.toMatchInlineSnapshot(`
671+
"{
672+
"compilerOptions": {
673+
"composite": true
674+
},
675+
"references": [
676+
{
677+
"path": "./packages/a"
678+
},
679+
{
680+
"path": "./packages/b"
681+
},
682+
{
683+
"path": "./packages/foo"
684+
}
685+
]
686+
}
687+
"
688+
`);
689+
expect(tree.read('packages/foo/tsconfig.json').toString('utf-8'))
690+
.toMatchInlineSnapshot(`
691+
"{
692+
"compilerOptions": {
693+
"composite": true
694+
}
695+
}
696+
"
697+
`);
698+
expect(tree.read('packages/foo/tsconfig.lib.json').toString('utf-8'))
699+
.toMatchInlineSnapshot(`
700+
"{
701+
"compilerOptions": {
702+
"composite": true
703+
}
704+
}
705+
"
706+
`);
707+
});
708+
662709
describe('without custom sync generator options', () => {
663710
it.each`
664711
runtimeTsConfigFileName

packages/js/src/generators/typescript-sync/typescript-sync.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,9 @@ function updateTsConfigReferences(
372372

373373
let hasChanges = false;
374374
for (const dep of dependencies) {
375-
// Ensure the project reference for the target is set
376-
let referencePath = dep.data.root;
375+
// Ensure the project reference for the target is set if we can find the
376+
// relevant tsconfig file
377+
let referencePath: string;
377378
if (runtimeTsConfigFileName) {
378379
const runtimeTsConfigPath = joinPathFragments(
379380
dep.data.root,
@@ -434,6 +435,19 @@ function updateTsConfigReferences(
434435
continue;
435436
}
436437
}
438+
if (!referencePath) {
439+
if (
440+
tsconfigExists(
441+
tree,
442+
tsconfigInfoCaches,
443+
joinPathFragments(dep.data.root, 'tsconfig.json')
444+
)
445+
) {
446+
referencePath = dep.data.root;
447+
} else {
448+
continue;
449+
}
450+
}
437451
const relativePathToTargetRoot = relative(projectRoot, referencePath);
438452
if (!newReferencesSet.has(relativePathToTargetRoot)) {
439453
newReferencesSet.add(relativePathToTargetRoot);

0 commit comments

Comments
 (0)