nrwl / nx

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

[@nx/esbuild:esbuild] Build not working when generatePackageJson is true and local libraries are referenced #26434

Open lucasvieirasilva opened 1 month ago

lucasvieirasilva commented 1 month ago

Current Behavior

After upgrading to Nx 19.x when a node.js project has a build target configured to @nx/esbuild:esbuild with generatePackageJson: true, thirdParty: true, and references a local library (e.g. lib1) it fails to generate the package-lock, when removes the local library reference (run the lint --fix to remove from the package.json) it generates the package-lock successfully.

Expected Behavior

when the thirdParty: true only the external dependencies should be in the package.json.

As mentioned here: https://github.com/nrwl/nx/blob/9eebe4980a41715f9993ea72782d8280f5a721c3/packages/esbuild/src/executors/esbuild/esbuild.impl.ts#L96-L101

GitHub Repo

https://github.com/lucasvieirasilva/nx19-generate-lock-issue

Steps to Reproduce

In the https://github.com/lucasvieirasilva/nx19-generate-lock-issue repo, the project was created as follows:

app1 uses a function exported by lib1, therefore, the app1's package.json has the lib1 listed as dependency (automatically added by the eslint @nx/dependency-checks rule)

lib1 exports a simple function that uses lodash to capitalize a string value.

app2 uses the lodash directly instead of referencing the lib1.

When the build target is executed for the app1 project it fails with Original error: Pruned lock file creation failed. The following package was not found in the root lock file: @example/lib1@0.0.1

When the build target is executed for the app2 project it runs successfully and generates both package.json and lock file.

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.13.1
OS     : darwin-x64
pnpm   : 8.15.8

nx                 : 19.2.0
@nx/js             : 19.2.0
@nx/linter         : 19.2.0
@nx/eslint         : 19.2.0
@nx/workspace      : 19.2.0
@nx/devkit         : 19.2.0
@nx/esbuild        : 19.2.0
@nx/eslint-plugin  : 19.2.0
@nrwl/tao          : 19.2.0
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/eslint/plugin

Failure Logs

✔  1/1 dependent project tasks succeeded [0 read from cache]

   Hint: you can run the command with --verbose to see the full dependent project outputs

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

> nx run app1:build

 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: Pruned lock file creation failed. The following package was not found in the root lock file: @example/lib1@0.0.1

Error: Pruned lock file creation failed. The following package was not found in the root lock file: @example/lib1@0.0.1
    at /Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/nx@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:45:19
    at Array.forEach (<anonymous>)
    at normalizeDependencies (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/nx@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:31:42)
    at pruneProjectGraph (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/nx@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25/node_modules/nx/src/plugins/js/lock-file/project-graph-pruning.js:13:34)
    at createLockFile (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/nx@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25/node_modules/nx/src/plugins/js/lock-file/lock-file.js:166:79)
    at updatePackageJson (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/@nx+js@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25_@types+node@18.16.9_nx@19.2.0_typescript@5.4.5/node_modules/@nx/js/src/utils/package-json/update-package-json.js:48:61)
    at copyPackageJson (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/@nx+js@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25_@types+node@18.16.9_nx@19.2.0_typescript@5.4.5/node_modules/@nx/js/src/utils/package-json/index.js:26:53)
    at esbuildExecutor (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/@nx+esbuild@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25_@types+node@18.16.9_esbuild@0.19_wrczqmr3apewhx4aoqc7oc6dii/node_modules/@nx/esbuild/src/executors/esbuild/esbuild.impl.js:65:60)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getLastValueFromAsyncIterableIterator (/Users/lucasvieira/Projects/nx-workspaces/nx19-generate-lock/node_modules/.pnpm/nx@19.2.0_@swc-node+register@1.9.1_@swc+core@1.5.25/node_modules/nx/src/utils/async-iterator.js:15:19)

——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Successfully ran target build for project app1 and 1 task it depends on (3s)

Package Manager Version

pnpm 8.15.8

Operating System

Additional Information

No response

arthurgubaidullin commented 4 weeks ago

Do you have any updates on this issue?