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

fix: correctly resolve hmr dep ids and fallback to url #18840

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

patricklx
Copy link
Contributor

@patricklx patricklx commented Nov 29, 2024

Description

fixes #12912
fixes #16375

this is the most basic to get it working. There are more issues regarding this, like having multiple modules in the module graph. one for 0virtual:file and another for /@id/__00__/virtual:file

@patricklx patricklx changed the title fix hot accept virtual module fix: hot accept virtual module Nov 29, 2024
@sapphi-red
Copy link
Member

Thank you for the PR!
I don't have much context for the linked issue and PRs, but my understandings are:

  • import.meta.hot.accept currently expects the deps argument to be a URL.
  • for a module with an id starting with \0, the url property of the module node for that module starts with \0
  • writing import.meta.hot.accept(urlForTheVirtualModule) doesn't work, because urlForTheVirtualModule starts with \0 instead of / and thus toAbsolutePath appends some string

I think there are two ways to fix the issue.

The first is to make import.meta.hot.accept to expect the deps argument to be an ID instead of a URL. I think this is more intuitive. But given that we have been expecting URLs, it's a breaking change. Maybe simply falling back to treating as a URL when resolving as an id fails would be fine.

The second is to make the url property of the module node to always start with /. Since it's a "URL", I expect it to always be a (non-relative) path part of the URL, and thus start with /. This fix would probably be more simple than the first one.

IIUC this PR makes import.meta.hot.accept to expect the deps arugument to be a URL for relative specifiers and to be an id for non-relative specifiers. I think that is confusing and better to expect only URLs or only ids.

@sapphi-red sapphi-red added feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) labels Dec 6, 2024
@patricklx
Copy link
Contributor Author

patricklx commented Dec 6, 2024

hi, alright, I think it should accept Ids, since the imports can be transformed based on imported and specifier. i think meta.hot.accept should do the same.

is it necessary a breaking change? I did not find that its documented to use url for deps.

@patricklx patricklx changed the title fix: hot accept virtual module fix: hot accept dep ids and fallback to url Dec 6, 2024
@patricklx
Copy link
Contributor Author

patricklx commented Dec 6, 2024

@sapphi-red this is now passing and still uses urls, but tries first to resolve the module via resolveId and then uses the resolved module.url.

on meta.hot.accept:
to be more precise:

  • import.meta.hot.accept currently expects the deps argument to be a URL at runtime and id at build time.

@patricklx patricklx changed the title fix: hot accept dep ids and fallback to url fix: correctly resolve dep ids and fallback to url Dec 6, 2024
@patricklx patricklx changed the title fix: correctly resolve dep ids and fallback to url fix: correctly resolve hmr dep ids and fallback to url Dec 6, 2024
@patricklx
Copy link
Contributor Author

@sapphi-red does this look okay now?

Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the change!

packages/vite/src/node/plugins/importAnalysis.ts Outdated Show resolved Hide resolved
Co-authored-by: 翠 / green <[email protected]>
@sapphi-red

This comment was marked as outdated.

@vite-ecosystem-ci

This comment was marked as outdated.

@vite-ecosystem-ci

This comment was marked as outdated.

@patricklx
Copy link
Contributor Author

I looked at vitest failure. vitest is currently failing on main

hi-ogawa
hi-ogawa previously approved these changes Dec 10, 2024
@vite-ecosystem-ci

This comment was marked as outdated.

@sapphi-red sapphi-red added this to the 6.1 milestone Dec 11, 2024
@sapphi-red
Copy link
Member

@patricklx Would you also describe where you needed to use import.meta.hot.accept(virtualDeps)?

Co-authored-by: 翠 / green <[email protected]>
@patricklx
Copy link
Contributor Author

@patricklx Would you also describe where you needed to use import.meta.hot.accept(virtualDeps)?

I have a vite plugin that generates virtual files which can also change depending on other files on the filesystem.
currently it's impossible to hot accept them.

@sapphi-red
Copy link
Member

@patricklx Would you also describe where you needed to use import.meta.hot.accept(virtualDeps)?

I have a vite plugin that generates virtual files which can also change depending on other files on the filesystem. currently it's impossible to hot accept them.

Is self-accepting on the virtual module side (i.e. call import.meta.accept(cb) in the virtual module) not an option?

@patricklx
Copy link
Contributor Author

no, the o

@patricklx Would you also describe where you needed to use import.meta.hot.accept(virtualDeps)?

I have a vite plugin that generates virtual files which can also change depending on other files on the filesystem. currently it's impossible to hot accept them.

Is self-accepting on the virtual module side (i.e. call import.meta.accept(cb) in the virtual module) not an option?

no, the files having the virtual file dependency need to update their values that come from the virtual file

@@ -753,9 +753,36 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(toAbsoluteUrl(url))
let [normalized, resolvedId] = await normalizeUrl(url, start)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sapphi-red I changed this to use normalizeUrl which already does the resolve and also calls _ensureEntryFromUrl.
I had to move _ensureEntryFromUrl out of if (environment.config.consumer === 'client') { check so its also called in ssr

sapphi-red
sapphi-red previously approved these changes Dec 12, 2024
@sapphi-red
Copy link
Member

/ecosystem-ci run

Copy link

pkg-pr-new bot commented Dec 12, 2024

Open in Stackblitz

npm i https://pkg.pr.new/vite@18840

commit: d5d7b94

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on ff09f8b: Open

suite result latest scheduled
histoire failure failure
nuxt success failure
rakkas failure success
redwoodjs failure failure
remix failure failure
storybook failure success
vike failure failure
vite-plugin-svelte failure success
vite-plugin-vue failure success
vitepress failure success
waku failure failure

analogjs, astro, ladle, laravel, marko, previewjs, quasar, qwik, sveltekit, unocss, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-setup-catalogue, vitest, vuepress

@sapphi-red
Copy link
Member

Hmm, it seems rakkas and vitepress is passing a URL. cc @patak-dev

@patricklx
Copy link
Contributor Author

vue is also failing in their hmr tests

@sapphi-red
Copy link
Member

plugin-vue's fail is a flaky fail, so that one is fine (vitejs/vite-plugin-vue#485).

@patak-dev
Copy link
Member

Hmm, it seems rakkas and vitepress is passing a URL

Maybe we could add back the URL fallback in the next minor if we're clear that this is just for backward compat and we'll move to a warning on Vite v7.

@patricklx
Copy link
Contributor Author

So, should i add back the fallback then?

@sapphi-red
Copy link
Member

Yeah. Would you add back the fallback with a comment that this is just for backward compat and will remove it in Vite 7?

@patricklx
Copy link
Contributor Author

Yeah. Would you add back the fallback with a comment that this is just for backward compat and will remove it in Vite 7?

I added the change

sapphi-red
sapphi-red previously approved these changes Dec 18, 2024
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@sapphi-red
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci

This comment was marked as duplicate.

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on d5d7b94: Open

suite result latest scheduled
histoire failure failure
laravel failure success
marko failure success
rakkas failure success
redwoodjs failure failure
remix failure failure
storybook failure success
vike failure failure
vite-plugin-svelte failure failure
vitepress failure success
waku failure failure

analogjs, astro, ladle, nuxt, previewjs, quasar, qwik, sveltekit, unocss, vite-environment-examples, vite-plugin-pwa, vite-plugin-react, vite-plugin-react-swc, vite-plugin-vue, vite-setup-catalogue, vitest, vuepress

@sapphi-red sapphi-red dismissed their stale review December 18, 2024 03:58

The fallback does not seem to be working

@@ -753,9 +756,38 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
// normalize and rewrite accepted urls
const normalizedAcceptedUrls = new Set<string>()
for (const { url, start, end } of acceptedUrls) {
const [normalized] = await moduleGraph.resolveUrl(toAbsoluteUrl(url))
let [normalized, resolvedId] = await normalizeUrl(url, start)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not taken a deeper look, but my guess is that this normalizeUrl throws an error when it fails to resolve, which is why the fallback isn't working.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) trigger: preview
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Support resolve import.meta.hot.accept dep Fails to hmr accept virtual modules
4 participants