Skip to content

Commit

Permalink
fix(js): only add typescript project references for explicit dependen…
Browse files Browse the repository at this point in the history
…cies in sync generator (#28998)

This change omits references to implicit dependency tsconfigs for
typescript projects in the sync generator, since given that they are not
referenced directly in code there is no need for project references.

## Current Behavior
TypeScript sync generator adds references to any dependency project
which has a tsconfig (where composite is true), including implicit
dependencies where these references are unnecessary and can potentially
cause build failures.

See [example
repo](https://github.com/cogwirrel/nx-sync-generator-implicit-deps-example).

## Expected Behavior
Only explicit dependencies should be referenced in tsconfigs.

## Related Issue(s)
Fixes #28997

---------

Co-authored-by: Leosvel Pérez Espinosa <[email protected]>
  • Loading branch information
cogwirrel and leosvelperez authored Jan 7, 2025
1 parent 68b293f commit 9e78142
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 33 deletions.
25 changes: 25 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 @@ -58,6 +58,14 @@ describe('syncGenerator()', () => {
});
}

function addProjectWithImplicitDependencies(
name: string,
implicitDependencies: string[]
) {
addProject(name);
projectGraph.nodes[name].data.implicitDependencies = implicitDependencies;
}

beforeEach(async () => {
tree = createTreeWithEmptyWorkspace();
projectGraph = {
Expand Down Expand Up @@ -634,6 +642,23 @@ describe('syncGenerator()', () => {
`);
});

it('should not add a reference if dependent project is an implicit dependency', async () => {
addProject('implicit-dep');
addProjectWithImplicitDependencies('foo', ['implicit-dep']);

await syncGenerator(tree);

expect(tree.read('packages/foo/tsconfig.json').toString('utf-8'))
.toMatchInlineSnapshot(`
"{
"compilerOptions": {
"composite": true
}
}
"
`);
});

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

for (const dep of projectGraph.dependencies[projectName]) {
const targetProjectNode = projectGraph.nodes[dep.target];
if (!targetProjectNode) {
// It's an npm dependency
if (!targetProjectNode || dep.type === 'implicit') {
// It's an npm or an implicit dependency
continue;
}

Expand Down
11 changes: 0 additions & 11 deletions packages/js/src/utils/typescript/ts-solution-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,6 @@ export function updateTsconfigFiles(
});
}

if (tree.exists(tsconfigE2E)) {
// tsconfig.json for e2e projects need to have references array
updateJson(tree, tsconfigE2E, (json) => {
json.references ??= [];
const projectPath = relative(e2eRoot, projectRoot);
if (!json.references.some((x) => x.path === projectPath))
json.references.push({ path: projectPath });
return json;
});
}

if (tree.exists('tsconfig.json')) {
updateJson(tree, 'tsconfig.json', (json) => {
const projectPath = './' + projectRoot;
Expand Down
5 changes: 0 additions & 5 deletions packages/next/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1038,11 +1038,6 @@ describe('app (legacy)', () => {
"**/*.cy.jsx",
"**/*.d.ts",
],
"references": [
{
"path": "../myapp",
},
],
}
`);
});
Expand Down
5 changes: 0 additions & 5 deletions packages/nuxt/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,6 @@ describe('app', () => {
"src/**/*.test.js",
"src/**/*.d.ts",
],
"references": [
{
"path": "../myapp",
},
],
}
`);
});
Expand Down
5 changes: 0 additions & 5 deletions packages/react/src/generators/application/application.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1447,11 +1447,6 @@ describe('app', () => {
"src/**/*.test.js",
"src/**/*.d.ts",
],
"references": [
{
"path": "../myapp",
},
],
}
`);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,6 @@ describe('Remix Application', () => {
"src/**/*.test.js",
"src/**/*.d.ts",
],
"references": [
{
"path": "../myapp",
},
],
}
`);
});
Expand Down

0 comments on commit 9e78142

Please sign in to comment.