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 06fad1035d94e..97ea3b33f5720 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts @@ -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 diff --git a/packages/js/src/generators/typescript-sync/typescript-sync.ts b/packages/js/src/generators/typescript-sync/typescript-sync.ts index 751751ae63a1d..b3fbd9a5ebdb8 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.ts @@ -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, @@ -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);