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

vite-node's import.meta.hot.accept does not work #3817

Closed
6 tasks done
lukadev-0 opened this issue Jul 26, 2023 · 0 comments · Fixed by #3834
Closed
6 tasks done

vite-node's import.meta.hot.accept does not work #3817

lukadev-0 opened this issue Jul 26, 2023 · 0 comments · Fixed by #3834
Milestone

Comments

@lukadev-0
Copy link

lukadev-0 commented Jul 26, 2023

Describe the bug

Any import.meta.hot.accept function will break HMR and the function passed to accept is never called.

I've figured out the cause of this bug by modifying the module in node_modules and adding some console logs, here is what I've found.

async function fetchUpdate(runner: ViteNodeRunner, { path, acceptedPath }: Update) {
const maps = getCache(runner)
const mod = maps.hotModulesMap.get(path)
if (!mod) {
// In a code-splitting project,
// it is common that the hot-updating module is not loaded yet.
// https://github.com/vitejs/vite/issues/721
return
}

The problem is within this function, the mod variable will always return undefined. This is because the path within the update is relative to the project root, so if your project is within /home/me/my-project and you had a file named main.js within that directory, the update's path property would be /main.js.

However, the hotModulesMap's keys are added here:

maps.hotModulesMap.set(ownerPath, mod)

The ownerPath parameter is given in the runner:

hotContext ||= this.options.createHotContext?.(this, `/@fs/${fsPath}`)

The fsPath is an absolute path to the file, prepended by /@fs/, which is different from the format given in the Update object, meaning that the mod variable will always be undefined, causing it to do an early return.

Reproduction

https://github.com/lukadev-0/vite-node-hmr-bug

System Info

  System:
    OS: Linux 6.2 Pop!_OS 22.04 LTS
    CPU: (4) x64 AMD Athlon 300GE with Radeon Vega Graphics
    Memory: 913.02 MB / 5.71 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 20.3.1 - ~/.nvm/versions/node/v20.3.1/bin/node
    npm: 9.6.7 - ~/.nvm/versions/node/v20.3.1/bin/npm
    pnpm: 8.6.5 - ~/.local/share/pnpm/pnpm
  Browsers:
    Brave Browser: 115.1.56.1

Used Package Manager

pnpm

Validations

@sheremet-va sheremet-va added this to the 0.34.0 milestone Jul 28, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Aug 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants