nrwl / nx

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

Nx Vite plugin errors when Vite config imports monorepo dependency #28719

Open privatenumber opened 1 month ago

privatenumber commented 1 month ago

Current Behavior

When using the Nx Vite plugin in a monorepo, Nx throws an error if a Vite config file imports from a dependency within the monorepo that hasn't been built yet.

For example, attempting to build a Vite library (lib-a) that imports an unbuilt Node package dependency (util-a) within the same monorepo results in an error during the project graph processing:

$ pnpm nx build lib-a --verbose

failed to load config from /path/to/lib-a/vite.config.ts

 NX   Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it. See errors below.

Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.
      An error occurred while processing files for the @nx/vite/plugin plugin.

Expected Behavior

For Nx to build the dependency tree without loading the Vite config, and to build util-a first before lib-a.

GitHub Repo

https://github.com/privatenumber/nx-plugin-issue

Steps to Reproduce

Run pnpm nx build lib-a --verbose

Nx Report

N/A

Failure Logs

$ pnpm nx build lib-a --verbose
Error: R] Failed to resolve entry for package "@squareup/util-a". The package may have incorrect main/module/exports specified in its package.json. [plugin externalize-deps]

    node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27:
      1225 │         let result = await callback({
           ╵                            ^

    at packageEntryFailure (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:46575:15)
    at resolvePackageEntry (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:46572:3)
    at tryNodeResolve (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:46388:16)
    at resolveByViteResolver (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66528:20)
    at file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66561:28
    at requestCallbacks.on-resolve (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:28)
    at handleRequest (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:647:17)
    at handleIncomingPacket (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:672:7)
    at Socket.readFromStdout (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7)
    at Socket.emit (node:events:517:28)

  This error came from the "onResolve" callback registered here:

    node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:20:
      1150 │       let promise = setup({
           ╵                     ^

    at setup (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66549:18)
    at handlePlugins (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1150:21)
    at buildOrContextImpl (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:873:5)
    at Object.buildOrContext (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:699:5)
    at /home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2023:15
    at new Promise (<anonymous>)
    at Object.build (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:2022:25)
    at build (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1873:51)
    at bundleConfigFile (file:///home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/vite@5.4.8_@types+node@22.7.5_terser@5.34.1/node_modules/vite/dist/node/chunks/dep-CDnG8rE7.js:66504:24)

  The plugin "externalize-deps" was triggered by this import

    libs/lib-a/vite.config.ts:3:25:
      3 │ import { sayHello } from '@squareup/util-a';
        ╵                          ~~~~~~~~~~~~~~~~~~

failed to load config from /home/runner/work/nx-plugin-issue/nx-plugin-issue/libs/lib-a/vite.config.ts

 NX   Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it. See errors below.

Failed to process project graph. Run "nx reset" to fix this. Please report the issue if you keep seeing it.
      An error occurred while processing files for the @nx/vite/plugin plugin.
    - libs/lib-a/vite.config.ts: Build failed with 1 error:
  node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27: ERROR: [plugin: externalize-deps] Failed to resolve entry for package "@squareup/util-a". The package may have incorrect main/module/exports specified in its package.json.
      Error: Build failed with 1 error:
      node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1225:27: ERROR: [plugin: externalize-deps] Failed to resolve entry for package "@squareup/util-a". The package may have incorrect main/module/exports specified in its package.json.
          at failureErrorWithLog (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1472:15)
          at /home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:945:25
          at runOnEndCallbacks (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:1315:45)
          at buildResponseToResult (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:943:7)
          at /home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:970:16
          at responseCallbacks.<computed> (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:622:9)
          at handleIncomingPacket (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:677:12)
          at Socket.readFromStdout (/home/runner/work/nx-plugin-issue/nx-plugin-issue/node_modules/.pnpm/esbuild@0.21.5/node_modules/esbuild/lib/main.js:600:7)
          at Socket.emit (node:events:517:28)
          at addChunk (node:internal/streams/readable:368:12)

Full logs: https://github.com/privatenumber/nx-plugin-issue/actions/runs/11608109855/job/32322726820

Package Manager Version

pnpm v9.11.0

Operating System

Additional Information

I empathize that this is a challenging problem as the Vite config must be loaded to infer the target configuration.

But I figured I'll report this as this introduces a significant limitation to basic monorepo development: no internal monorepo package such as a utility library or Vite plugin can be imported by the Vite config.

I'm thinking the user-end solution is to opt out of using the Vite plugin and manually configure the target instead, but I'm curious what functionality we'll miss out on: https://github.com/privatenumber/nx-plugin-issue/pull/3

privatenumber commented 3 days ago

This same problem happens with @nx/eslint/plugin when eslint.config.js references an internal ESLint config workspace package that requires a build step (written in TypeScript).