diff --git a/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts b/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts index a3e07268cd..a01f3ca306 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts @@ -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", @@ -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 () => { diff --git a/packages/js/src/generators/typescript-sync/typescript-sync.ts b/packages/js/src/generators/typescript-sync/typescript-sync.ts index 2c54033d5a..75469689a4 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.ts @@ -176,8 +176,7 @@ export async function syncGenerator(tree: Tree): Promise { 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; } @@ -208,9 +207,6 @@ export async function syncGenerator(tree: Tree): Promise { projectGraph, collectedDependencies ); - if (!dependencies.length) { - continue; - } for (const runtimeTsConfigFileName of runtimeTsConfigFileNames) { const runtimeTsConfigPath = joinPathFragments( diff --git a/packages/js/src/plugins/typescript/plugin.spec.ts b/packages/js/src/plugins/typescript/plugin.spec.ts index afe458817b..0e97fb30d8 100644 --- a/packages/js/src/plugins/typescript/plugin.spec.ts +++ b/packages/js/src/plugins/typescript/plugin.spec.ts @@ -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, { @@ -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, { diff --git a/packages/js/src/plugins/typescript/plugin.ts b/packages/js/src/plugins/typescript/plugin.ts index 09bbe98822..2ec1ba459e 100644 --- a/packages/js/src/plugins/typescript/plugin.ts +++ b/packages/js/src/plugins/typescript/plugin.ts @@ -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; } @@ -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; }