Skip to content

Commit

Permalink
fix(js): do not set project references to non-existing tsconfig files…
Browse files Browse the repository at this point in the history
… 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 #
  • Loading branch information
leosvelperez authored Jan 8, 2025
1 parent 97e1e3b commit 160800e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
47 changes: 47 additions & 0 deletions packages/js/src/generators/typescript-sync/typescript-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,53 @@ describe('syncGenerator()', () => {
`);
});

it('should not add a reference if dependent project does not have a tsconfig files', async () => {
addProject('foo', ['bar'], ['tsconfig.lib.json']);
addProject('bar');
tree.delete('packages/bar/tsconfig.json');

await syncGenerator(tree);

expect(tree.read('tsconfig.json').toString('utf-8'))
.toMatchInlineSnapshot(`
"{
"compilerOptions": {
"composite": true
},
"references": [
{
"path": "./packages/a"
},
{
"path": "./packages/b"
},
{
"path": "./packages/foo"
}
]
}
"
`);
expect(tree.read('packages/foo/tsconfig.json').toString('utf-8'))
.toMatchInlineSnapshot(`
"{
"compilerOptions": {
"composite": true
}
}
"
`);
expect(tree.read('packages/foo/tsconfig.lib.json').toString('utf-8'))
.toMatchInlineSnapshot(`
"{
"compilerOptions": {
"composite": true
}
}
"
`);
});

describe('without custom sync generator options', () => {
it.each`
runtimeTsConfigFileName
Expand Down
18 changes: 16 additions & 2 deletions packages/js/src/generators/typescript-sync/typescript-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,9 @@ function updateTsConfigReferences(

let hasChanges = false;
for (const dep of dependencies) {
// Ensure the project reference for the target is set
let referencePath = dep.data.root;
// Ensure the project reference for the target is set if we can find the
// relevant tsconfig file
let referencePath: string;
if (runtimeTsConfigFileName) {
const runtimeTsConfigPath = joinPathFragments(
dep.data.root,
Expand Down Expand Up @@ -434,6 +435,19 @@ function updateTsConfigReferences(
continue;
}
}
if (!referencePath) {
if (
tsconfigExists(
tree,
tsconfigInfoCaches,
joinPathFragments(dep.data.root, 'tsconfig.json')
)
) {
referencePath = dep.data.root;
} else {
continue;
}
}
const relativePathToTargetRoot = relative(projectRoot, referencePath);
if (!newReferencesSet.has(relativePathToTargetRoot)) {
newReferencesSet.add(relativePathToTargetRoot);
Expand Down

0 comments on commit 160800e

Please sign in to comment.