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

asset-import-meta-url incorrectly tries to resolve assets #14169

Closed
7 tasks done
magic-akari opened this issue Aug 21, 2023 · 10 comments
Closed
7 tasks done

asset-import-meta-url incorrectly tries to resolve assets #14169

magic-akari opened this issue Aug 21, 2023 · 10 comments

Comments

@magic-akari
Copy link

magic-akari commented Aug 21, 2023

Describe the bug

keywords:

  • asset-import-meta-url
  • new URL
  • import.meta.url
  • Failed to resolve entry for package "[...]/.vite/deps". The package may have incorrect main/module/exports specified in its package.json.

Cannot use import.meta.url to calculate path.
The vite plugin asset-import-meta-url is trying to resolve the asset and throw errors.

This bug only exists in the installed npm package and cannot be reproduced using the local link (pnpm link) method. Therefore, when trying to reproduce the bug, please directly move foo to the node_modules directory.

Reproduction

https://stackblitz.com/edit/vitejs-vite-9ysjmo?file=index.js

Steps to reproduce

You can download this tgz file and reproduce the bug according to the README.

minimal reproducible example: vite-bug-utl-0.0.0.tgz

Background: I am working at https://www.npmjs.com/package/@wasm-fmt/clang-format .

System Info

System:
    OS: macOS 13.5
    CPU: (10) arm64 Apple M1 Pro
    Memory: 394.03 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 16.20.1 - ~/Library/Caches/fnm_multishells/39029_1692629667006/bin/node
    Yarn: 3.3.1 - ~/Library/Caches/fnm_multishells/39029_1692629667006/bin/yarn
    npm: 8.19.4 - ~/Library/Caches/fnm_multishells/39029_1692629667006/bin/npm
    pnpm: 8.1.1 - ~/Library/Caches/fnm_multishells/39029_1692629667006/bin/pnpm
  Browsers:
    Chrome: 116.0.5845.96
    Safari: 16.6
  npmPackages:
    vite: ^4.4.9 => 4.4.9

Used Package Manager

pnpm

Logs

[vite] Internal server error: Failed to resolve entry for package "/Users/akari/Developer/vite-bug-url/node_modules/.vite/deps". The package may have incorrect main/module/exports specified in its package.json.
Plugin: vite:asset-import-meta-url
File: /Users/akari/Developer/vite-bug-url/node_modules/.vite/deps/foo.js?v=7f02c688
at packageEntryFailure (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:28691:11)
at resolvePackageEntry (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:28688:5)
at tryCleanFsResolve (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:28349:28)
at tryFsResolve (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:28296:17)
at TransformContext.transform (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:43166:32)
at Object.transform (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:44283:62)
at async loadAndTransform (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:54950:29)
at async viteTransformMiddleware (file:///Users/akari/Developer/vite-bug-url/node_modules/.pnpm/[email protected]/node_modules/vite/dist/node/chunks/dep-df561101.js:64345:32)

Validations

@stackblitz
Copy link

stackblitz bot commented Aug 21, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@sapphi-red
Copy link
Member

Duplicate of #8427

@sapphi-red sapphi-red marked this as a duplicate of #8427 Aug 22, 2023
@sapphi-red sapphi-red closed this as not planned Won't fix, can't repro, duplicate, stale Aug 22, 2023
@sapphi-red sapphi-red added duplicate This issue or pull request already exists and removed pending triage labels Aug 22, 2023
@magic-akari
Copy link
Author

@sapphi-red I believe this is not the same issue.
The error #8427 occurred during runtime, and this issue's error occurred during the build stage.

@sapphi-red
Copy link
Member

The underlying problem is same: import.meta.url inside is passes as-is by the dep optimizer causing import.meta.url to point to a wrong place.

On second thought, this issue could be a separate one. As I think it won't be solved even if #8427 was solved.
Two features are making new URL('.', import.meta.url) to work impossible.

  1. Dep optimizer bundles the script and passes new URL('.', import.meta.url) as-is.
  2. Then new URL('.', import.meta.url) works like import.meta.resolve('.') (resolve . in the .vite/deps by the node's resolver)

Because .vite/deps/package.json exists but the main field doesn't exist, the error happens.

The workaround is same with #8427 (exclude from dep optimization) but I guess it won't work in some cases even with that.
Because the package will expect new URL('.', import.meta.url) to resolve to a directory.

related: #7837, #13142

@sapphi-red sapphi-red reopened this Aug 22, 2023
@sapphi-red sapphi-red removed the duplicate This issue or pull request already exists label Aug 22, 2023
@pneves001
Copy link

Any resolution for this? I'm having the same problem.

@WolfgangDrescher
Copy link

I'm not sure if this is really a duplicate of #13530. However, the other issue is closed so I ask here.

I get an error since Vite 5:

[vite]  Internal server error: Maximum call stack size exceeded
Plugin: vite:asset-import-meta-url
...
[vite] Pre-transform error: Maximum call stack size exceeded

Reproduction: https://stackblitz.com/edit/github-rjaduf-38mdms?file=src%2Fpages%2FHome.vue

The workaround with optimizeDeps.exclude = ['verovio'] suggested by @vitto32 does not seem to work anymore. Is there another method in Vite 5? How this could be improved?

@sapphi-red
Copy link
Member

@WolfgangDrescher The error reported at #13530 is a duplicate of this issue. But that Maximum call stack size exceeded error is a different one. I made a separate issue for that: #15703

@WolfgangDrescher
Copy link

@danielroe gave an update on nuxt/nuxt#21578. Since this is a duplicate of #13530 probably this issue is already resolved. I did upgrade Vite in my reproduction repo to v5.1.1 and everything seems to work now. Also Maximum call stack size exceeded does not appear now:

https://stackblitz.com/edit/github-rjaduf-caswxa?file=package.json

@magic-akari
Copy link
Author

I upgraded vite to version 5 and retested the case; it appears to have been resolved. Should we close this issue?

@sapphi-red
Copy link
Member

Thanks for the report. It seems to be fixed by #14626.

@github-actions github-actions bot locked and limited conversation to collaborators Jul 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants