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

feat(hmr): improve circular import updates #14867

Merged
merged 9 commits into from
Nov 8, 2023
Merged

feat(hmr): improve circular import updates #14867

merged 9 commits into from
Nov 8, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Nov 3, 2023

Description

fix #10208
fix #7893
fix #3033
fix #10118 (not quite an exact fix but I think close enough, the issue has a lot of circular imports and fast refresh warnings that's causing things to update slowly)

When propagating HMR updates, we only propagate until we reach HMR-accepted modules, or the root (which we do a full reload.

A HMR-accepted module doesn't need a full reload as it can update itself. However, if the accepted module is within an import loop, updating itself can break the execution order of the import loop. This PR detects this and forces a full reload instead.


Any other import loops without a HMR-accepted module are fine. For example,

App.vue -> a.js -> b.js -> c.js -> a.js

If we update b.js and App.vue accepts HMR, App.vue will refetch a.js?t=123 and since a.js -> b.js -> c.js -> a.js is its own self-contained loop, the execution order and state should be fine.

If b.js (or any within the loop) accepts HMR, we can't guarantee the execution order anymore, e.g. it will refetch b.js?t=123, causing b.js -> c.js -> a.js (import order matters)

Additional context

Tests ported from #1477. The commit that fixes that issue introduces the full reload, but another the issue was that the leaf of circular imports wasn't properly invalidated. It is now properly invalidated, but I don't think we always need a full reload.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority) labels Nov 3, 2023
Copy link

stackblitz bot commented Nov 3, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@bluwy bluwy marked this pull request as ready for review November 6, 2023 06:12
@bluwy bluwy marked this pull request as draft November 6, 2023 06:27
@bluwy

This comment was marked as outdated.

@bluwy bluwy marked this pull request as ready for review November 7, 2023 09:01
@patak-dev
Copy link
Member

/ecosystem-ci run

@vite-ecosystem-ci
Copy link

📝 Ran ecosystem CI on 1bc383d: Open

suite result latest scheduled
analogjs failure failure
astro success failure
histoire failure failure
ladle success success
laravel failure failure
marko failure failure
nuxt failure failure
nx failure failure
previewjs success success
qwik failure failure
rakkas success success
sveltekit failure failure
unocss success success
vike failure failure
vite-plugin-pwa success success
vite-plugin-react failure success
vite-plugin-react-pages failure failure
vite-plugin-react-swc success success
vite-plugin-svelte success success
vite-plugin-vue success failure
vite-setup-catalogue success success
vitepress success success
vitest failure failure

@patak-dev
Copy link
Member

Amazing work on HMR all around for Vite 5! It looks good to me, but there is a new fail in plugin react that may be related: https://github.com/vitejs/vite-ecosystem-ci/actions/runs/6783049879/job/18436527990. It would be good if @ArnaudBarre gets some time to review this one, and then maybe we could ping Evan to speed up merging it as part of the major.

@bluwy
Copy link
Member Author

bluwy commented Nov 7, 2023

I ran the plugin-react's test locally and it's working fine. Could be a fluke 🤔

patak-dev
patak-dev previously approved these changes Nov 7, 2023
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.

Great!

docs/guide/troubleshooting.md Outdated Show resolved Hide resolved
playground/hmr/__tests__/hmr.spec.ts Show resolved Hide resolved
@@ -0,0 +1,3 @@
import { value as _value } from './mod-c'

export const value = `mod-b -> ${_value}`
Copy link
Member

Choose a reason for hiding this comment

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

If I add import.meta.hot?.accept(() => {}) here, the full refresh starts happening.

index.js (boundary) -> mod-a.js -> mod-b.js (boundary) -> mod-c.js -> mod-a.js

It might be good to improve this case as well in future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah this one is tricky currently since the two boundary could start fetching at the same time, causing potential execution order issues. We could detect and only refresh index.js as the boundary, but I think it's also nice that this triggers a page reload + HMR debug logs so they can fix this and get fine-grained HMR in the first place.

Copy link
Member

Choose a reason for hiding this comment

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

I think it's also nice that this triggers a page reload + HMR debug logs so they can fix this and get fine-grained HMR in the first place.

Ah, that's a good point.

Co-authored-by: 翠 / green <green@sapphi.red>
@sapphi-red sapphi-red merged commit b479055 into main Nov 8, 2023
10 checks passed
@sapphi-red sapphi-red deleted the hmr-circle branch November 8, 2023 08:01
@ArnaudBarre
Copy link
Member

For the code, great, very clear thanks!

For the feature, I will just say that I don't think we're helping people in the long term to add a patch for circular imports.
This is too easy to have code working one day and not the other because you change one export or the chain start from another point because of another link. And as Evan W. pointed out once, the spec can be ambiguous for runtime on the order execution for complex graph.

People using circular imports are also adding really strong requirement on bundlers, and they will miss perf improvement on that side because new bundlers will not always have the edge cases right (ex. esbuild).

@sapphi-red
Copy link
Member

Oops, I forgot about this comment and merged this one. 😅

It would be good if @ArnaudBarre gets some time to review this one, and then maybe we could ping Evan to speed up merging it as part of the major.

@patak-dev Do you think we should wait for Evan? or was it because of the plugin-react's (flaky?) fail?

@patak-dev
Copy link
Member

@ArnaudBarre I think we should still add more nudges in the docs for people to avoid circular imports (maybe in the performance or troubleshooting guide?). The PR doesn't feel like a patch though complexity-wise, so I think it was good merging it.

@sapphi-red we are good 👍🏼

@bluwy
Copy link
Member Author

bluwy commented Nov 8, 2023

Yeah I think the PR is leaning towards the real fix for the problem. The previous reload and circular detection was more of a patch that made HMR not ideal for those case. I agree though that circular imports is not great in general, and hopefully the explicit reloads compared to the silent fails before will bring more attention to the issue.

@skovhus
Copy link
Contributor

skovhus commented Nov 14, 2023

Thanks for improving this. Is there a canary release where we can test this out?

@bluwy
Copy link
Member Author

bluwy commented Nov 15, 2023

The latest Vite 5 beta should already include this

@skovhus
Copy link
Contributor

skovhus commented Nov 18, 2023

@bluwy I just tried 5.0 out an a large codebase with a lot of circular imports. Unfortunately we can end up in an infinite loop when changing certain files – vite becomes irresponsive and the only solution is to kill vite. I'll try to reproduce this on a smaller example that I can share.

But maybe this can already now guide us on the right track to fix this, by adding a log line for one of our files src/vite/index.tsx:

function isNodeWithinCircularImports(node, nodeChain, currentChain = [node], recursion=0) {
  if (node.url === '/src/vite/index.tsx') {
     console.log("isNodeWithinCircularImports 0", node.url, {nodeChainLength: nodeChain.length, currentChainLength: currentChain.length, recursion});
  }

Then I basically see the same file being analyzed with varying currentChain length but a stable nodeChain length. I also instrumented the recursion level but that doesn't explode – we just seem to be cycling around in the same nodes for some reason.

Let me know how I can help to fix this.

@ArnaudBarre
Copy link
Member

ArnaudBarre commented Nov 18, 2023

This is expected that the nodeChain is stable for a given node and then all the rest of the graph is explored. There is probably an issue with barrel exports where a long part of the graph is explored repetitively.
I think adding something similar to #12658 for caching could help.

Can you try to update your patch with this?

function isNodeWithinCircularImports(node, nodeChain, currentChain = [node], traversedModules = new Set()) {
  if (traversedModules.has(node)) return false
  traversedModules.add(node)
  // [...]
  isNodeWithinCircularImports(importer, nodeChain, currentChain.concat(importer), traversedModules)

@skovhus
Copy link
Contributor

skovhus commented Nov 19, 2023

@ArnaudBarre that works as expected, thanks!

PR up to fix this: #15034

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: hmr p2-nice-to-have Not breaking anything but nice to have (priority)
Projects
None yet
5 participants