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

moduleGraph.invalidateModule() softInvalidate can be optimizing #15607

Closed
7 tasks done
YellRes opened this issue Jan 15, 2024 · 4 comments · Fixed by #15663
Closed
7 tasks done

moduleGraph.invalidateModule() softInvalidate can be optimizing #15607

YellRes opened this issue Jan 15, 2024 · 4 comments · Fixed by #15663
Labels
contribution welcome feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@YellRes
Copy link

YellRes commented Jan 15, 2024

Describe the bug

in sourcecode, importer's softInvalidate state are defined by importer.staticImportedUrls or current softInvalidate

mod.importers.forEach((importer) => {
  if (!importer.acceptedHmrDeps.has(mod)) {
    // If the importer statically imports the current module, we can soft-invalidate the importer
    // to only update the import timestamps. If it's not statically imported, e.g. watched/glob file,
    // we can only soft invalidate if the current module was also soft-invalidated. A soft-invalidation
    // doesn't need to trigger a re-load and re-transform of the importer.
    const shouldSoftInvalidateImporter =
      importer.staticImportedUrls?.has(mod.url) || softInvalidate
    this.invalidateModule(
      importer,
      seen,
      timestamp,
      isHmr,
      shouldSoftInvalidateImporter,
    )
  }
})

but it fails sometimes, in next case app.vue imports a.ts

<script setup lang="ts">
import { arr } from './a'
</script>

<template>
  {{ arr }}
  <div>hmr fails</div>
</template>

after first init vite program, app.vue moduleNode's staticImportedUrls containsa.ts(like /src/a.ts), when i change a.ts, it match a.ts moduleNode's url, so app.vue will softInvalidate itself, this is ok.
but after change app.vue, app.vue will load and transform its content, its moduleNode's staticImportedUrls contains a.ts with timestamps(like /src/a.ts?v=xxx), so next time i just change a.ts, importer.staticImportedUrls?.has(mod.url) will be false, app.vue will never softInvalidate itself.

Reproduction

https://github.com/YellRes/soft-invalidate-fail

Steps to reproduce

run pnpm run dev

  • change content in app.vue
  • then change content in a.ts
  • app.vue isn't softInvalidate

System Info

System:
    OS: macOS 13.2
    CPU: (8) arm64 Apple M1
    Memory: 1.18 GB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    npm: 8.3.0 - ~/node_modules/.bin/npm
  Browsers:
    Chrome: 120.0.6099.216
    Edge: 120.0.2210.133
    Safari: 16.3
  npmPackages:
    @vitejs/plugin-vue: ^4.5.2 => 4.6.2 
    vite: ^5.0.8 => 5.0.11

Used Package Manager

pnpm

Logs

No response

Validations

@bluwy bluwy added feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Jan 15, 2024
@bluwy
Copy link
Member

bluwy commented Jan 15, 2024

Thanks for digging into this! Seems clear that this is a bug. I think on this line

const staticImportedUrls = new Set(_orderedImportedUrls)

we can map and run through removeTimestampQuery() to strip the timestamps away. Would you like to contribute a fix for this?

@YellRes
Copy link
Author

YellRes commented Jan 16, 2024

sure, i will try this

@YellRes
Copy link
Author

YellRes commented Jan 16, 2024

@bluwy sry, i never contribute before. after sever times pr failed, maybe other people are better do this😥

@bluwy
Copy link
Member

bluwy commented Jan 16, 2024

No problem! Feel free to hop in to the Vite discord server if you need help setting Vite up locally, otherwise others may also help contribute a fix.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
contribution welcome feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants