Skip to content

Commit

Permalink
fix(js): handle ts project refs pointing to non-existing files and pr…
Browse files Browse the repository at this point in the history
…une ts refs for projects with no deps (#28130)

<!-- 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 Sep 27, 2024
1 parent 56eabff commit 5d6b357
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 7 deletions.
13 changes: 11 additions & 2 deletions packages/js/src/generators/typescript-sync/typescript-sync.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,12 +371,19 @@ describe('syncGenerator()', () => {
{ path: '../c' }, // this is not a dependency, should be pruned
],
});
// add a project with no dependencies
addProject('c');
updateJson(tree, 'packages/c/tsconfig.json', (json) => {
// this is not a dependency, should be pruned
json.references = [{ path: '../d' }];
return json;
});

await syncGenerator(tree);

const rootTsconfig = readJson(tree, 'packages/b/tsconfig.json');
const bTsconfigJson = readJson(tree, 'packages/b/tsconfig.json');
// The dependency reference on "a" is added to the start of the array
expect(rootTsconfig.references).toMatchInlineSnapshot(`
expect(bTsconfigJson.references).toMatchInlineSnapshot(`
[
{
"path": "../a",
Expand All @@ -389,6 +396,8 @@ describe('syncGenerator()', () => {
},
]
`);
const cTsconfigJson = readJson(tree, 'packages/c/tsconfig.json');
expect(cTsconfigJson.references).toStrictEqual([]);
});

it('should not prune existing external project references that are not dependencies but are git ignored', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ export async function syncGenerator(tree: Tree): Promise<SyncGeneratorResult> {
for (const [projectName, data] of Object.entries(projectGraph.dependencies)) {
if (
!projectGraph.nodes[projectName] ||
projectGraph.nodes[projectName].data.root === '.' ||
!data.length
projectGraph.nodes[projectName].data.root === '.'
) {
continue;
}
Expand Down Expand Up @@ -208,9 +207,6 @@ export async function syncGenerator(tree: Tree): Promise<SyncGeneratorResult> {
projectGraph,
collectedDependencies
);
if (!dependencies.length) {
continue;
}

for (const runtimeTsConfigFileName of runtimeTsConfigFileNames) {
const runtimeTsConfigPath = joinPathFragments(
Expand Down
30 changes: 30 additions & 0 deletions packages/js/src/plugins/typescript/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,19 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
`);
});

it('should handle ts project references pointing to non-existing files and not throw', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': JSON.stringify({
references: [{ path: '../my-lib-2' }],
}),
'libs/my-lib/package.json': `{}`,
});

await expect(
invokeCreateNodesOnMatchingFiles(context, {})
).resolves.not.toThrow();
});

describe('inputs', () => {
it('should add the config file and the `include` and `exclude` patterns', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
Expand Down Expand Up @@ -1821,6 +1834,23 @@ describe(`Plugin: ${PLUGIN_NAME}`, () => {
`);
});

it('should handle ts project references pointing to non-existing files and not throw', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
'libs/my-lib/tsconfig.json': JSON.stringify({
references: [{ path: '../my-lib-2' }],
}),
'libs/my-lib/tsconfig.lib.json': `{}`,
'libs/my-lib/package.json': `{}`,
});

await expect(
invokeCreateNodesOnMatchingFiles(context, {
typecheck: false,
build: true,
})
).resolves.not.toThrow();
});

describe('inputs', () => {
it('should add the config file and the `include` and `exclude` patterns', async () => {
await applyFilesToTempFsAndContext(tempFs, context, {
Expand Down
10 changes: 10 additions & 0 deletions packages/js/src/plugins/typescript/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,11 @@ function resolveInternalProjectReferences(
continue;
}

if (!existsSync(refConfigPath)) {
// the referenced tsconfig doesn't exist, ignore it
continue;
}

if (isExternalProjectReference(refConfigPath, workspaceRoot, projectRoot)) {
continue;
}
Expand Down Expand Up @@ -585,6 +590,11 @@ function hasExternalProjectReferences(
continue;
}

if (!existsSync(refConfigPath)) {
// the referenced tsconfig doesn't exist, ignore it
continue;
}

if (isExternalProjectReference(refConfigPath, workspaceRoot, projectRoot)) {
return true;
}
Expand Down

0 comments on commit 5d6b357

Please sign in to comment.