nrwl / nx

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

`Target project does not exist` error on app restart #19199

Open artursudnik opened 1 year ago

artursudnik commented 1 year ago

Current Behavior

after setting generatePackageJson: true for a nodejs NestJS application I noticed that when it is started in watch mode, on every restart I am getting the following error:

 >  NX   An error occured while creating pruned lockfile

   Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
   To prevent the build from breaking we are returning the root lock file.
   If you run `npm install --package-lock-only` in your output folder it will regenerate the correct pruned lockfile.

Original error: Target project does not exist: npm:debug@2.6.9

   Error: Target project does not exist: npm:debug@2.6.9
    at validateCommonDependencyRules (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/project-graph/project-graph-builder.js:288:15)
    at validateDependency (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/project-graph/project-graph-builder.js:278:5)
    at ProjectGraphBuilder.addDependency (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/project-graph/project-graph-builder.js:165:9)
    at ProjectGraphBuilder.addStaticDependency (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/project-graph/project-graph-builder.js:82:14)
    at /Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:81:17
    at Array.forEach (<anonymous>)
    at traverseNode (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:78:36)
    at /Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:80:9
    at Array.forEach (<anonymous>)
    at traverseNode (/Users/artur/WebstormProjects/example-apps/nx-isolating-issue/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:78:36)

After that, it restarts correctly.

When building code nothing like this happens.

Expected Behavior

No error messages on restart

GitHub Repo

https://github.com/artursudnik/nx-isolated-issue

Steps to Reproduce

  1. generate a new integrated NX monorepository with the NestJS app
  2. set generatePackageJson: true for the app
  3. start the app in develop (watch) mode
  4. make modifications to trigger a restart

Nx Report

Node   : 20.6.1
   OS     : darwin-arm64
   npm    : 9.8.1

   nx                 : 16.8.1
   @nx/js             : 16.8.1
   @nx/jest           : 16.8.1
   @nx/linter         : 16.8.1
   @nx/workspace      : 16.8.1
   @nx/devkit         : 16.8.1
   @nx/eslint-plugin  : 16.8.1
   @nx/nest           : 16.8.1
   @nx/node           : 16.8.1
   @nrwl/tao          : 16.8.1
   @nx/webpack        : 16.8.1
   typescript         : 5.1.6

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

fantoine commented 1 year ago

Got the same issue too while using yarn as a package manager.

jasonmacdonald commented 1 year ago

Same issue; only mine is looking for node type Original error: Target project does not exist: npm:@types/node@20.5.7

patrickmichalina commented 1 year ago

Same issue but I see:

 >  NX   An error occured while creating pruned lockfile

   Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
   To prevent the build from breaking we are returning the root lock file.
   If you run `npm install --package-lock-only` in your output folder it will regenerate the correct pruned lockfile.

Original error: Target project does not exist: npm:brace-expansion@2.0.1

   Error: Target project does not exist: npm:brace-expansion@2.0.1
dustintownsend commented 1 year ago

I am getting a similar issue after upgrading to 16.9.1 and 16.10.0. Issue is not on 16.4.0.

edit: also seeing this with 17.0.1

 >  NX   An error occured while creating pruned lockfile

   Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
   To prevent the build from breaking we are returning the root lock file.
   If you run `pnpm install --lockfile-only` in your output folder it will regenerate the correct pruned lockfile.

Original error: Target project does not exist: npm:is-stream@2.0.1

   Error: Target project does not exist: npm:is-stream@2.0.1
    at validateCommonDependencyRules (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/project-graph/project-graph-builder.js:323:15)
    at validateDependency (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/project-graph/project-graph-builder.js:313:5)
    at ProjectGraphBuilder.addDependency (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/project-graph/project-graph-builder.js:190:9)
    at ProjectGraphBuilder.addStaticDependency (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/project-graph/project-graph-builder.js:89:14)
    at /workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:81:17
    at Array.forEach (<anonymous>)
    at traverseNode (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:78:36)
    at /workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:80:9
    at Array.forEach (<anonymous>)
    at traverseNode (/workspaces/project-name/node_modules/.pnpm/nx@16.10.0_@swc-node+register@1.6.8_@swc+core@1.3.94/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:78:36)
noticeeverything commented 1 year ago

Still present in 17.0.2. This error causes build failures in watch mode after some unpredictable number of restarts, and it also prevents a successful build when running nx docker-build from the app directory (e.g., apps/my-app) without a lockfile already present. I'm working around this currently by copying the root lockfile into the app directory temporarily. Obviously not an ideal solution.

jasonmacdonald commented 1 year ago

I can confirm I'm still seeing this in 17.0.2

dustintownsend commented 11 months ago

This issue is also present on 17.1.2.

I can confirm that if I set generatePackageJson to false then the issue is gone. However that generated package json is needed for the docker image. I appear to be getting a successful build with the generatePackageJson set to true but I haven't done a full build and deploy on nx 17+.

dustintownsend commented 11 months ago

@AgentEnder I think tracked down why this error is happening. It is because some dependencies append an @{version} to the end of the name, but this is not always reflected in the graph. My theory is it happens when the root lockfile has a dependency with different versions but the pruned lockfile only has 1 of those versions. I have not tracked down where nx determines to add the @{version} or not.

I was able to "fix" this in nx/src/project-graph/project-graph-builder - validateCommonDependencyRules by parsing the name and removing @{version} if it exists.

I just did this in the node_modules folder so its the js output instead of original ts source.

// nx/src/project-graph/project-graph-builder.js

function parseName(value) {
  const lastAtIndex = value.lastIndexOf('@');
  const colonIndex = value.indexOf(':');
  if (lastAtIndex === -1 || lastAtIndex - colonIndex === 1) {
    return value;
  }
  const name = value.substring(0, lastAtIndex);
  return name
}

function validateCommonDependencyRules(d, { externalNodes, projects, fileMap }) {
  if (!projects[d.source] &&
      !externalNodes[d.source] &&
      !projects[parseName(d.source)] &&
      !externalNodes[parseName(d.source)]) {
    throw new Error(`Source project does not exist: ${d.source}`);
  }
  if (!projects[d.target] &&
    !externalNodes[d.target] &&
    !('sourceFile' in d && d.sourceFile) && 
    !externalNodes[parseName(d.target)]) {
    throw new Error(`Target project does not exist: ${d.target}`);
  }
.....

I would open a PR but this doesn't feel like the correct fix.

artursudnik commented 11 months ago

I have updated the isolated case to nx@17.1.2

fantoine commented 10 months ago

@artursudnik Have you find any workaround around this issue ?

artursudnik commented 10 months ago

@fantoine Unfortunately not. I am just ignoring these annoying messages.

artursudnik commented 10 months ago

Same after upgrading NX to v17.2.4

dustintownsend commented 9 months ago

I came across a workaround in another issue. I tried it out using @artursudnik nx-isolated-issue and it appears to fix it. See this comment: https://github.com/nrwl/nx/issues/20421#issuecomment-1851882236 Basically just need to move generatePackageJson: true to the production config. This should work unless you have a need to generatedPackageJson in development.

"build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        ......
        "generatePackageJson": false
      },
      "configurations": {
        "development": {},
        "production": {
          "generatePackageJson": true
        }
      }
    },
......
shyagamzo commented 8 months ago

Still a problem in Nx v18.0.5.

The suggested workaround technically works but isn't relevant in my case. I actually need my package.json to be generated in development mode as well.

Use case: I'm developing a vscode extension and vscode reads feature configuration from package.json. As I develop or modify features, I need to make the relevant changes in my package.json and I would like those to be reflected immediately, without relaunching the extension host.

Anyone managed to come up with a fix?

NX   An error occurred while creating pruned lockfile

Please open an issue at `https://github.com/nrwl/nx/issues/new?template=1-bug.yml` and provide a reproduction.
To prevent the build from breaking we are returning the root lock file.
If you run `npm install --package-lock-only` in your output folder it will regenerate the correct pruned lockfile.

Original error: Target project does not exist: npm:debug@2.6.9

Error: Target project does not exist: npm:debug@2.6.9
    at validateCommonDependencyRules (C:\Path\To\Project\node_modules\nx\src\project-graph\project-graph-builder.js:323:15)
    at validateDependency (C:\Path\To\Project\node_modules\nx\src\project-graph\project-graph-builder.js:313:5)
    at ProjectGraphBuilder.addDependency (C:\Path\To\Project\node_modules\nx\src\project-graph\project-graph-builder.js:190:9)
    at ProjectGraphBuilder.addStaticDependency (C:\Path\To\Project\node_modules\nx\src\project-graph\project-graph-builder.js:89:14)
    at C:\Path\To\Project\node_modules\nx\src\plugins\js\lock-file\project-graph-pruning.js:81:17      
    at Array.forEach (<anonymous>)
    at traverseNode (C:\Path\To\Project\node_modules\nx\src\plugins\js\lock-file\project-graph-pruning.js:78:36)
    at C:\Path\To\Project\node_modules\nx\src\plugins\js\lock-file\project-graph-pruning.js:80:9       
    at Array.forEach (<anonymous>)
    at traverseNode (C:\Path\To\Project\node_modules\nx\src\plugins\js\lock-file\project-graph-pruning.js:78:36)

chunk (runtime: main) main.js (main) 18.8 KiB (javascript) 670 bytes (runtime) [entry] [rendered]
webpack compiled successfully (0e3e969e157c6987)
shyagamzo commented 7 months ago

Upgraded Nx to v18.2.1. Problem persists.

PatrikValkovic commented 5 months ago

With the latest NX v19.0.3 the error is still there.

zeejay09 commented 3 months ago

Im at nx v18.3.1 and it is still an issue, this workaround solves my issue

I came across a workaround in another issue. I tried it out using @artursudnik nx-isolated-issue and it appears to fix it. See this comment: #20421 (comment) Basically just need to move generatePackageJson: true to the production config. This should work unless you have a need to generatedPackageJson in development.

"build": {
      "executor": "@nx/webpack:webpack",
      "outputs": ["{options.outputPath}"],
      "defaultConfiguration": "production",
      "options": {
        ......
        "generatePackageJson": false
      },
      "configurations": {
        "development": {},
        "production": {
          "generatePackageJson": true
        }
      }
    },
......