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

Monorepo module resolution fails when requested via HTTP using the dev server #12106

Closed
7 tasks done
hex2f opened this issue Feb 18, 2023 · 1 comment
Closed
7 tasks done
Labels
inconsistency Inconsistency between dev & build pending triage

Comments

@hex2f
Copy link

hex2f commented Feb 18, 2023

Describe the bug

I'm trying to use vite in a monorepo where i have some packages that need to be imported in <head> (that shouldnt be bundled in my main app ts file):

<script type="module" src="@vitereprod/foo/src/index.ts"></script>

Building works perfectly as expected, but not when using the dev server (just running npx vite).

It correctly resolves the package during initial boot-up

vite:resolve 11.24ms @vitereprod/foo/src/index.ts -> C:/Users/leah/versatile/vitereprod/packages/foo/src/index.ts +0ms

But resolution fails when loaded via HTTP

vite:resolve 0.76ms /@vitereprod/foo/src/index.ts -> null +7ms

The problem seems to be that the leading / from the url is included when resolving.

I managed to fix the issue by doing a quick and dirty patch to resolveId() that removes the leading slash if rawId starts with /@vitereprod. Everything works as expected after this change, including hot reloading. No clue how to generalize it though 🤷‍♀️

async resolveId(_rawId, importer = join(root, 'index.html'), options) {
  const rawId = rawId.startsWith('/@vitereprod') ? _rawId.slice(1) : _rawId
  ...

First time using Vite so this might just be a deep misunderstanding of how to do this and i just managed to find a way to force this behavior :p

Reproduction

https://github.com/hex2f/vite-bug-reprod

Steps to reproduce

npm i
cd apps/web
npm run dev

try to open the dev server in browser or curl http://localhost:port/@vitereprod/foo/src/index.ts

System Info

System:
    OS: Windows 10 10.0.22621
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 10.22 GB / 31.93 GB
  Binaries:
    Node: 17.1.0 - C:\Program Files\nodejs\node.EXE
    npm: 8.1.4 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.22621.1265.0), Chromium (110.0.1587.49)
    Internet Explorer: 11.0.22621.1

Used Package Manager

npm

Logs

Click to expand!
 VITE v4.1.2  ready in 253 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
  vite:deps Crawling dependencies using entries:
  vite:deps   C:/Users/leah/versatile/vitereprod/apps/web/src/index.html +0ms
  vite:resolve 11.24ms @vitereprod/foo/src/index.ts -> C:/Users/leah/versatile/vitereprod/packages/foo/src/index.ts +0ms
  vite:deps Scan completed in 56.13ms: {} +43ms
  vite:deps no dependencies found by scanner +57ms
  vite:html-fallback Rewriting GET / to /index.html +0ms
  vite:time 19.51ms /index.html +0ms
  vite:html-fallback Rewriting GET / to /index.html +79ms
  vite:time 3.62ms /index.html +64ms
  vite:resolve 4.55ms C:\Users\leah\versatile\vitereprod\node_modules\vite\dist\client\client.mjs -> C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/client.mjs +0ms
  vite:resolve 5.29ms /@vite/client -> C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/client.mjs +1ms
  vite:resolve 0.76ms /@vitereprod/foo/src/index.ts -> null +7ms
  vite:html-fallback Not rewriting GET /@vitereprod/foo/src/index.ts because the path includes a dot (.) character. +77ms
  vite:time 3.05ms /@vitereprod/foo/src/index.ts +74ms
  vite:load 7.48ms [fs] /@vite/client +0ms
  vite:resolve 1.00ms C:\Users\leah\versatile\vitereprod\node_modules\vite\dist\client\env.mjs -> C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/env.mjs +26ms
  vite:resolve 1.22ms @vite/env -> C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/env.mjs +1ms
  vite:import-analysis 6.95ms [1 imports rewritten] C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/client.mjs +0ms
  vite:transform 25.35ms /@vite/client +0ms
  vite:time 44.59ms /@vite/client +29ms
  vite:load 1.20ms [fs] ..\..\..\node_modules\vite\dist\client\env.mjs +30ms
  vite:import-analysis 0.10ms [no imports] C:/Users/leah/versatile/vitereprod/node_modules/vite/dist/client/env.mjs +6ms
  vite:transform 0.81ms ..\..\..\node_modules\vite\dist\client\env.mjs +5ms
  vite:deps ✨ static imports crawl ended +472ms
  vite:deps ✨ no dependencies found by the scanner or crawling static imports +0ms
  vite:cache [304] ..\..\..\node_modules\vite\dist\client\env.mjs +0ms
  vite:time 1.45ms ..\..\..\node_modules\vite\dist\client\env.mjs +35ms

Validations

@sapphi-red sapphi-red added the inconsistency Inconsistency between dev & build label Jun 15, 2023
@bluwy
Copy link
Member

bluwy commented Oct 29, 2023

Duplicate of #12260. Preferring it as it has a linked PR and the issue is not monorepo-related.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 29, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
inconsistency Inconsistency between dev & build pending triage
Projects
None yet
Development

No branches or pull requests

3 participants