nrwl / nx

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

Creating a workspace with npm preset doesn't analyze dependencies #11073

Closed yjaaidi closed 2 years ago

yjaaidi commented 2 years ago

Current Behavior

Creating a new workspace with npm preset doesn't analyze dependencies so the dependency graph is always "flat" no matter how apps/libraries import each other.

Apparently, this seems to happen because @nrwl/workspace/presets/npm.json is replaced by nx/presets/npm.json where source files are not analyzed due to the following configuration: https://github.com/nrwl/nx/blob/16ffa482b5cdd8e2b35732a40fdd26f333f8ecc8/packages/nx/presets/npm.json#L17-L21 I guess this was done to simplify nx integration with existing monorepos.

Since 14.2.0, a migration script fixes this for existing nx workspaces using the following schematic: https://github.com/nrwl/nx/blob/master/packages/workspace/src/migrations/update-14-2-0/enable-source-analysis.ts but it is not enabled by default for new workspaces.

Expected Behavior

This can be done here https://github.com/nrwl/nx/blob/16ffa482b5cdd8e2b35732a40fdd26f333f8ecc8/packages/workspace/src/generators/workspace/workspace.ts#L34-L45 just like it's done in the migration script: https://github.com/nrwl/nx/blob/master/packages/workspace/src/migrations/update-14-2-0/enable-source-analysis.ts#L20-L22

Steps to Reproduce

yarn create nx-workspace repro --preset npm
cd repro
nx g lib a
nx g lib b

then import something from a into b and run nx dep-graph.

Notice that the dep graph doesn't detect a as a dependency.

Environment

   Node : 18.3.0
   OS   : darwin x64
   yarn : 3.2.1

   nx : 14.4.2
   @nrwl/angular : 14.4.2
   @nrwl/cypress : 14.4.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.4.2
   @nrwl/eslint-plugin-nx : 14.4.2
   @nrwl/express : Not Found
   @nrwl/jest : 14.4.2
   @nrwl/js : 14.4.2
   @nrwl/linter : 14.4.2
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 14.2.0
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.4.2
   @nrwl/web : 14.4.2
   @nrwl/workspace : 14.4.2
   typescript : 4.7.4
   ---------------------------------------
   Community plugins:
FrozenPandaz commented 2 years ago

This was done intentionally.

For the npm preset, we decided to keep things closer to how npm sees dependencies (via the package.json files). This preset doesn't have typescript setup which under the hood is responsible for the source code analysis. Nx will still analyze package.json files where if you have dependencies between projects in your workspace.

The ts preset may suit your needs better. It is very similar to the npm preset but it utilizes typescript to analyze dependencies.

Of course, you can choose to enable source code analysis in a npm preset generate workspace via https://nx.dev/migration/adding-to-monorepo#enabling-js-analysis

Hopefully this clears up any confusion.

yjaaidi commented 2 years ago

Thx @FrozenPandaz, this makes it clear. Is it documented somewhere so other users don't get confused?

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.