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

"HMR dead end" false positive due to /@id/ prefix #2380

Closed
3 tasks done
aleclarson opened this issue Mar 5, 2021 · 4 comments
Closed
3 tasks done

"HMR dead end" false positive due to /@id/ prefix #2380

aleclarson opened this issue Mar 5, 2021 · 4 comments

Comments

@aleclarson
Copy link
Member

aleclarson commented Mar 5, 2021

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

Describe the bug

Note: This seems to affect virtual modules only.

if (!url.startsWith('.') && !url.startsWith('/')) {
url =
VALID_ID_PREFIX + resolved.id.replace('\0', NULL_BYTE_PLACEHOLDER)
}

In the snippet above, /@id/ is prefixed to bare imports. This "normalized URL" is added to the module's importedUrls set (here), which is then passed to moduleGraph.updateModuleInfo (here). Then a module is created for the /@id/-prefixed bare import (here).

But then in transformRequest, the same bare import is not prefixed with /@id/, which leads to a duplicate module being created (here). This duplicate module won't know which modules it's imported by, which leads to "HMR dead end" false positives (here).

System Info

  • vite version: 2.0.5
@sunkehappy
Copy link

bare imports like React will be resolved to relative path to root with / prefixed. Check the log in the image:

bare import log

But still maybe we are not talking the same imports. Could you upload a sample project that could reproduce this bug to GitHub?

@aleclarson
Copy link
Member Author

Here's the repro with instructions
https://github.com/aleclarson/repro/tree/vite-2380

aleclarson added a commit to aleclarson/vite-plugin-react-pages that referenced this issue Mar 11, 2021
This avoids the issue described in:
  vitejs/vite#2380
aleclarson added a commit to aleclarson/vite-plugin-react-pages that referenced this issue Mar 11, 2021
This avoids the issue described in:
  vitejs/vite#2380
@aleclarson
Copy link
Member Author

aleclarson commented Mar 11, 2021

By taking the same approach as @vitejs/plugin-react-refresh does with virtual module IDs, I can avoid this issue.

const runtimePublicPath = '/@react-refresh'

aleclarson added a commit to aleclarson/vite-plugin-react-pages that referenced this issue Mar 11, 2021
This avoids the issue described in:
  vitejs/vite#2380
@aleclarson
Copy link
Member Author

One problem with the work around is TypeScript won't let you declare module when the path starts with /.

Ambient module declaration cannot specify relative module name. ts(2436)

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

No branches or pull requests

2 participants