Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Last migration update wroke the import via ts (problem with vite.config.ts and new nxViteTsPaths method) #18492

Closed
1 of 4 tasks
fsgreco opened this issue Aug 4, 2023 · 7 comments
Assignees
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug

Comments

@fsgreco
Copy link

fsgreco commented Aug 4, 2023

Current Behavior

My libs where created with @nx/react:lib generators, they result in libs with this kind of package.json:

{
  "name": "@myself/lib-name",
  "version": "0.0.1",
  "main": "./index.js",
  "types": "./index.d.ts",
  "exports": {
    ".": {
      "import": "./index.mjs",
      "require": "./index.js"
    }
  }
}

Now, after the last update (made via nx migrate latest and nx migrate --run-migrations)
I notice that the vite.config.ts files changed the way they get my viteTsConfigPaths.
From this:

  plugins: [
    react(),
    viteTsConfigPaths({
      root: '../../',
    }),
  ],

To this:

  plugins: [
    react(),
        nxViteTsPaths(),
  ],

The problem is that now I have this error:

[plugin:vite:import-analysis] Failed to resolve entry for package "@myself/lib-name". The package may have incorrect main/module/exports specified in its package.json.

Expected Behavior

It should simply import the packages by reading the base ts config file. Vite is not reading it.

GitHub Repo

No response

Steps to Reproduce

This problem appears after a migration from "nx": "16.1.3" to the latest 16.6.0 (idem for @nx/vite from v16.1.3 to v16.6.0)

  1. Generate a library with @nx/react:lib
npx nx g @nx/react:lib ui--importPath=@yourname/ui
  1. Generate an app with @nx/react:app and import some component from @yourname/ui.
  2. Test if it works with nx s your-react-app
  3. Now perform migration with nx migrate latest and nx migrate --run-migrations
  4. Re-test the frontend app. It should give you the error.

Nx Report

15:56:49 [vite] Internal server error: Failed to resolve entry for package "@myself/lib-name". The package may have incorrect main/module/exports specified in its package.json.
  Plugin: vite:import-analysis
  File: /.../packages/the-react-app/src/main.tsx

Failure Logs

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

I manage to "solve" this by changing the package.json of my library, eliminating "exports" and changing "main" to the .ts file, but I'm feeling this is not an ideal solution:

{
  "name": "@myself/lib-name",
  "version": "0.0.1",
  "main": "./src/index.ts",
  "types": "./index.d.ts",
}
@mandarini
Copy link
Member

@fsgreco I see you found the related PR! @barbados-clemens tagging you here just for reference.

@barbados-clemens
Copy link
Contributor

barbados-clemens commented Aug 8, 2023

@fsgreco I just tried this on with nx latest and didn't see the issue.
Do you have a repo I can check with to make sure the PR (#18477) is going to fix the issue?

The steps I took is to make a new react integrated workspace and then generated a react library with vite/vitest.
I am able to build and serve the application without seeing those errors you mentioned.

@fsgreco
Copy link
Author

fsgreco commented Aug 9, 2023

Sorry for not specifying this, but my project is package-based.
I just re-tried, this is the minimal PoC I've just done that reproduce the error:

  1. npx create-nx-workspace@latest myproject --preset=npm
  2. npm install @nx/react -W (this will create a tsconfig.base.json at the root of the project).
  3. npx nx g @nx/react:app frontend --routing --style styled-components --bundler vite --e2eTestRunner none
  4. npx nx g @nx/react:lib ui-demo --importPath=@myorg/ui-demo
  5. Now go to packages/frontend/src/app/app.tsx and import a component from @myorg/ui-demo:
import { UiDemo } from '@myorg/ui-demo'
//...
 <UiDemo />
  1. nx s frontend

It should give you the error:
immagine

@barbados-clemens
Copy link
Contributor

barbados-clemens commented Aug 15, 2023

Okay this looks to be related to buildable libs, and something about the returned path vite doesn't like.

i.e. when resolving to the path defined in the tsconfig.base.json, vite throws the error that it can't understand the package.json export fields, what I imagine is bc those files don't exist as those export fields are for when the project is built as a final artifact.

When running serve, I can see the file is correctly resolved.

side note: I also get error from vite about styled-components as well. (see full error log below)

I will need to look more into how to control this behavior with vite.

[Nx Vite TsPaths] Resolved @myorg/ui-demo to /Users/caleb/Work/sandbox/vite-plugin-issues/packages/ui-demo/src/index.ts
Error:   Failed to scan for dependencies from entries:
  /Users/caleb/Work/sandbox/vite-plugin-issues/packages/frontend/index.html

  ✘ [ERROR] Failed to resolve entry for package "@myorg/ui-demo-buildable". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-scan]

Full Debug Output ❯ nx serve frontend

nx run frontend:serve:development

[Nx Vite TsPaths] Found tsconfig at /Users/caleb/Work/sandbox/vite-plugin-issues/tsconfig.base.json

[Nx Vite TsPaths] first parsed tsconfig: {
resultType: 'success',
configFileAbsolutePath: '/Users/caleb/Work/sandbox/vite-plugin-issues/tsconfig.base.json',
baseUrl: '.',
absoluteBaseUrl: '/Users/caleb/Work/sandbox/vite-plugin-issues',
paths: {
'@myorg/ui-demo': [ 'packages/ui-demo/src/index.ts' ],
'@myorg/ui-demo-buildable': [ 'packages/ui-demo-buildable/src/index.ts' ]
},
addMatchAll: true
}

[Nx Vite TsPaths] Found tsconfig at /Users/caleb/Work/sandbox/vite-plugin-issues/tsconfig.base.json

[Nx Vite TsPaths] fallback parsed tsconfig: {
resultType: 'success',
configFileAbsolutePath: '/Users/caleb/Work/sandbox/vite-plugin-issues/tsconfig.base.json',
baseUrl: '.',
absoluteBaseUrl: '/Users/caleb/Work/sandbox/vite-plugin-issues',
paths: {
'@myorg/ui-demo': [ 'packages/ui-demo/src/index.ts' ],
'@myorg/ui-demo-buildable': [ 'packages/ui-demo-buildable/src/index.ts' ]
},
addMatchAll: true
}
➜ Local: http://localhost:4200/

[Nx Vite TsPaths] Resolved @myorg/ui-demo to /Users/caleb/Work/sandbox/vite-plugin-issues/packages/ui-demo/src/index.ts
Error: Failed to scan for dependencies from entries:
/Users/caleb/Work/sandbox/vite-plugin-issues/packages/frontend/index.html

✘ [ERROR] Failed to resolve entry for package "@myorg/ui-demo-buildable". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-scan]

node_modules/esbuild/lib/main.js:1360:21:
  1360 │         let result = await callback({
       ╵                      ^

at packageEntryFailure (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23382:11)
at resolvePackageEntry (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23379:5)
at tryNodeResolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23113:20)
at Context.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:22874:28)
at Object.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42847:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43145:26)
at async file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43322:34
at async requestCallbacks.on-resolve (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1360:22)
at async handleRequest (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:723:13)

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

node_modules/esbuild/lib/main.js:1279:20:
  1279 │       let promise = setup({
       ╵                     ^

at setup (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43312:19)
at handlePlugins (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1279:21)
at buildOrContextImpl (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:968:5)
at Object.buildOrContext (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:776:5)
at /Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:68
at new Promise (<anonymous>)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:27)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2012:58)
at prepareEsbuildScanner (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43097:26)

The plugin "vite:dep-scan" was triggered by this import

packages/frontend/src/app/app.tsx:7:32:
  7 │ import { UiDemoBuildable } from '@myorg/ui-demo-buildable';
    ╵                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Failed to resolve entry for package "styled-components". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-scan]

node_modules/esbuild/lib/main.js:1360:21:
  1360 │         let result = await callback({
       ╵                      ^

at packageEntryFailure (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23382:11)
at resolvePackageEntry (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23379:5)
at tryNodeResolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23113:20)
at Context.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:22874:28)
at Object.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42847:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43145:26)
at async file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43322:34
at async requestCallbacks.on-resolve (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1360:22)
at async handleRequest (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:723:13)

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

node_modules/esbuild/lib/main.js:1279:20:
  1279 │       let promise = setup({
       ╵                     ^

at setup (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43312:19)
at handlePlugins (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1279:21)
at buildOrContextImpl (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:968:5)
at Object.buildOrContext (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:776:5)
at /Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:68
at new Promise (<anonymous>)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:27)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2012:58)
at prepareEsbuildScanner (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43097:26)

The plugin "vite:dep-scan" was triggered by this import

packages/frontend/src/app/app.tsx:1:19:
  1 │ import styled from 'styled-components';
    ╵                    ~~~~~~~~~~~~~~~~~~~

✘ [ERROR] Failed to resolve entry for package "styled-components". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-scan]

node_modules/esbuild/lib/main.js:1360:21:
  1360 │         let result = await callback({
       ╵                      ^

at packageEntryFailure (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23382:11)
at resolvePackageEntry (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23379:5)
at tryNodeResolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:23113:20)
at Context.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:22874:28)
at Object.resolveId (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:42847:46)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async resolve (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43145:26)
at async file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43322:34
at async requestCallbacks.on-resolve (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1360:22)
at async handleRequest (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:723:13)

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

node_modules/esbuild/lib/main.js:1279:20:
  1279 │       let promise = setup({
       ╵                     ^

at setup (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43312:19)
at handlePlugins (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1279:21)
at buildOrContextImpl (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:968:5)
at Object.buildOrContext (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:776:5)
at /Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:68
at new Promise (<anonymous>)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2172:27)
at Object.context (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:2012:58)
at prepareEsbuildScanner (file:///Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/vite/dist/node/chunks/dep-e8f070e8.js:43097:26)

The plugin "vite:dep-scan" was triggered by this import

packages/ui-demo/src/lib/ui-demo.tsx:1:19:
  1 │ import styled from 'styled-components';
    ╵                    ~~~~~~~~~~~~~~~~~~~


at failureErrorWithLog (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1636:15)
at /Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1048:25
at runOnEndCallbacks (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1471:45)
at buildResponseToResult (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1046:7)
at /Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1058:9
at new Promise (<anonymous>)
at requestCallbacks.on-end (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:1057:54)
at handleRequest (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:723:19)
at handleIncomingPacket (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:745:7)
at Socket.readFromStdout (/Users/caleb/Work/sandbox/vite-plugin-issues/node_modules/esbuild/lib/main.js:673:7)

@barbados-clemens
Copy link
Contributor

@fsgreco okay so some new info. I noticed if I remove the 'workspaces' entry from the package.json and reinstall deps. it starts working.

Something about the npm workspace setup is whats causing the issue, almost like vite doesn't use the plugin to resolve the file but instead does it own resolution first and errors instead of passing it off to the plugin? forcing the plugin to resolve first with 'enforce: pre' doesn't seem to work either.

The plugin "vite:dep-scan" was triggered by this import

    packages/frontend/src/app/app.tsx:2:23:
      2 │ import { UiDemo } from '@org/ui';
        ╵                        ~~~~~~~~~

@barbados-clemens
Copy link
Contributor

Hi @fsgreco, so figured out what was going on here.

mainly using --preset=npm when making a workspace setups npm workspaces.

so when you setup to use the @myorg/ui-demo project inside the frontend application. npm ends up hoisting the @myorg/ui-demo package in the node_modules. Once that package is in the node_modules vite sees it when resolving, but throws an error because the default package.json exports are setup for when that project is built vs when it's just source code.

This all happens before the nx vite tspaths plugin can run so it can't resolve that dep first.

So there are 2 work arounds here

  1. don't use npm workspaces (you can still keep the same 'packages' structure). You can remove workspaces via removing the workspaces key in the top level package.json and running npm i again. That should get things working as expected.
  2. Setup @myorg/ui-demo (and any subsequent packages) to work with npm workspaces and what vite expects. There are 2 steps in solving this.
    • 2a. set the package to output the dist in the project root instead of the top level dist folder. Update the build target's outputPath option to be packages/ui-demo/dist. This is in the project.json file for that project.
"targets": {
  "build": {
      "options": {
        "outputPath": "packages/ui-demo/dist"
      }
  }
}

2b. Set package.json of the @myorg/ui-demo package to export from that dist directory.

{
  "name": "@myorg/ui-demo",
  "version": "0.0.1",
  "main": "./dist/index.js",
  "types": "./dist/index.d.ts",
  "exports": {
    ".": {
      "import": "./dist/index.mjs",
      "require": "./dist/index.js"
    }
  }
}

so now when vite loads your project, it will correctly find the built code. This will require your project is built fist. So you'll want to make sure the nx.json has the correct targetDefaults setup so you don't have to remember to build first.

{
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"]
    },
    "serve": {
      "dependsOn": ["^build"]
    }
}

You can learn more about (nx.json#targetDefaults in the docs](https://nx.dev/reference/nx-json#target-defaults)

Hopefully that helps and explains more as to what is happening here.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated scope: bundlers Issues related to webpack, rollup type: bug
Projects
None yet
Development

No branches or pull requests

3 participants