nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.75k stars 2.37k forks source link

[sync] typescript-sync generator adds project references for implicit dependencies #28997

Open cogwirrel opened 2 days ago

cogwirrel commented 2 days ago

Current Behavior

The typescript sync generator adds references for all projects in the dependency graph if they have a tsconfig (with composite: true).

This currently includes implicit dependencies, which are not explicitly referenced in code and therefore don't require project references.

See #28839 - where batch builds also skip implicit dependencies.

For a tangible example, if I have a monorepo with some infrastructure project (eg CDK), and a website project (eg a React app) - I might have my infrastructure project deploy my website but not directly reference any of its code, and so use an implicit dependency to ensure build order. The sync generator will still add a reference as both of these are TypeScript projects, but I can get an error such as this when moduleResolution is is different for the two projects:

      ../website/tsconfig.app.json:3:3 - error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.

      3   "compilerOptions": {
          ~~~~~~~~~~~~~~~~~

      ../website/tsconfig.app.json:3:3 - error TS5109: Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.

      3   "compilerOptions": {
          ~~~~~~~~~~~~~~~~~

Expected Behavior

Implicit dependency tsconfigs should not be referenced by the projects which depend on them. Only explicit dependencies should be referenced in project tsconfigs.

GitHub Repo

https://github.com/cogwirrel/nx-sync-generator-implicit-deps-example

Steps to Reproduce

  1. Clone the example repo
  2. pnpm i
  3. pnpm nx run-many --target build --all and observe error

If you then delete the references to the website in packages/infra/tsconfig.json and packages/infra/tsconfig.lib.json and rebuild, Nx prompts to run the sync generator. If we skip running the sync generator, the build succeeds, otherwise if we run the sync generator the build fails.

Nx Report

NX   Report complete - copy this into the issue template

Node           : 20.18.0
OS             : darwin-arm64
Native Target  : aarch64-macos
pnpm           : 9.13.0

nx             : 20.1.2
@nx/js         : 20.1.2
@nx/eslint     : 20.1.1
@nx/workspace  : 20.1.2
@nx/devkit     : 20.1.2
@nx/react      : 20.1.1
@nx/vite       : 20.1.2
@nx/web        : 20.1.1
typescript     : 5.5.4
---------------------------------------
Registered Plugins:
@nx/js/typescript
@nx/vite/plugin
---------------------------------------
The following packages should match the installed version of nx
  - @nx/eslint@20.1.1
  - @nx/react@20.1.1
  - @nx/web@20.1.1

To fix this, run `nx migrate nx@20.1.2`

Failure Logs

Failure logs will vary depending on the use case, but in the example code they are as follows:

      ../website/tsconfig.app.json:3:3 - error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.

      3   "compilerOptions": {
          ~~~~~~~~~~~~~~~~~

      ../website/tsconfig.app.json:3:3 - error TS5109: Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'.

      3   "compilerOptions": {
          ~~~~~~~~~~~~~~~~~

Package Manager Version

pnpm 9.13.0

Operating System

Additional Information

No response