Skip to content

feat(rolldown_plugin_vite_import_glob): follow file creation and deletion via hotUpdate - #10550

Merged
shulaoda merged 2 commits into
mainfrom
feat/import-glob-hot-update
Oct 7, 2026
Merged

shulaoda merged 2 commits into
mainfrom
feat/import-glob-hot-update

Conversation

@shulaoda

@shulaoda shulaoda commented Jul 30, 2026 •

Copy link
Copy Markdown
Member

Part of #10059

Problem

Vite's JS plugin keeps the result of a glob fresh from a hotUpdate hook. Under bundled dev the native plugin replaces it, and the native plugin only implemented transform.

#10549 makes the file events arrive, but nothing maps them to the module that owns the glob.

What this PR does

Implements hotUpdate in the native plugin, the way Vite's plugin does it.

  • Each import.meta.glob call gets one matcher in dev mode. It is built with globstar from the absolute globs of the call, with the options Vite passes to picomatch. dot follows exhaustive, case folding follows caseSensitive, and **/node_modules/** is excluded unless exhaustive is set.
  • The glob syntax in the literal part of a path is escaped, like Vite's globSafeResolvedPath.
  • For a created or deleted file, the hook adds every module with a matcher that matches the file to the affected set.
  • A content edit is declined. It cannot change which files a glob matches.

The walk in transform is not changed. It still matches with fast_glob.

Note

The hotUpdate hook is off by default (dev.hotUpdate, #10837), and Vite does not pass the option yet (vitejs/vite#22956). This PR takes effect once the option is on.

The hook is as simple as Vite's, so it inherits what the watcher and the engine do differently from Vite:

  • A file saved by renaming a temporary file over it is reported as created, so the module of the glob is updated with it.
  • A deleted directory is reported without its files, and no glob matches a directory.
  • The matchers of a deleted module stay, like in Vite, but the engine still knows the module.

@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from c8e34c4 to 34e3e53 Compare July 31, 2026 07:46
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch 2 times, most recently from c27791c to 3e4542c Compare July 31, 2026 18:49
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch 2 times, most recently from aa1cf94 to 927d441 Compare July 31, 2026 19:06
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch 3 times, most recently from 28f7168 to 30dc48c Compare July 31, 2026 20:41
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from 30dc48c to b0a23fc Compare July 31, 2026 20:49
@shulaoda shulaoda changed the title feat(vite-import-glob): follow file creation and deletion via hotUpdate feat(rolldown_plugin_vite_import_glob): follow file creation and deletion via hotUpdate Jul 31, 2026
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from b0a23fc to 142fee5 Compare August 3, 2026 00:25
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch 2 times, most recently from 4a795d2 to b1f932f Compare August 3, 2026 04:10
@shulaoda
shulaoda changed the base branch from feat/import-glob-watch-dirs to feat/import-glob-record-matchers August 3, 2026 04:13
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from b1f932f to 7497d3e Compare August 3, 2026 05:14
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from 7497d3e to 94cd3a2 Compare August 3, 2026 05:35
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from 94cd3a2 to 43a3be7 Compare August 4, 2026 06:34
@shulaoda
shulaoda requested a review from h-a-n-a October 6, 2026 07:50
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from 0a3d418 to fb8971b Compare October 6, 2026 07:57
@shulaoda

shulaoda commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

How does this work with rebuilds when incremental build is enabled? Does the this.addWatch(dir) make the module containing import.meta.glob to be invalidated when any file in dir is changed?

It works the same way as Vite's implementation. The only difference is that ours needs to call this.addWatch(dir) on the glob's root (i.e., its parent directory), and then use the hotUpdate hook to filter and match the corresponding modules.

@shulaoda
shulaoda marked this pull request as ready for review October 7, 2026 03:28
@shulaoda
shulaoda requested a review from sapphi-red as a code owner October 7, 2026 03:28
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from 3c645e2 to c4abb66 Compare October 7, 2026 03:29
@sapphi-red

Copy link
Copy Markdown
Member

How does this work with rebuilds when incremental build is enabled? Does the this.addWatch(dir) make the module containing import.meta.glob to be invalidated when any file in dir is changed?

It works the same way as Vite's implementation. The only difference is that ours needs to call this.addWatch(dir) on the glob's root (i.e., its parent directory), and then use the hotUpdate hook to filter and match the corresponding modules.

Isn't that for "hotUpdates"? I'm talking about the "rebuilds" (the one corresponding to reloads in unbundled mode).

@codspeed

codspeed Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing feat/import-glob-hot-update (5cc9db6) with main (701cce0)

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@shulaoda

shulaoda commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Isn't that for "hotUpdates"? I'm talking about the "rebuilds" (the one corresponding to reloads in unbundled mode).

If you're asking whether the dir can act as a dependency of the corresponding module, so that the module gets invalidated when something in the dir changes, then yes.

When this.addWatch(dir) is called, we record a dir -> module dependency. Then, when a file changes, we walk up the file path's ancestors to find the matching watched dir, look up the module(s) associated with it, and rebuild them.

@sapphi-red

Copy link
Copy Markdown
Member

Isn't that for "hotUpdates"? I'm talking about the "rebuilds" (the one corresponding to reloads in unbundled mode).

If you're asking whether the dir can act as a dependency of the corresponding module, so that the module gets invalidated when something in the dir changes, then yes.

When this.addWatch(dir) is called, we record a dir -> module dependency. Then, when a file changes, we walk up the file path's ancestors to find the matching watched dir, look up the module(s) associated with it, and rebuild them.

Wouldn't that cause many false-positives for a glob like **/*.png since we don't have shouldTransformCachedModule hook?

@shulaoda

shulaoda commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Wouldn't that cause many false-positives for a glob like **/*.png since we don't have shouldTransformCachedModule hook?

Oh, you're right. I took a closer look, and it turns out I'm calling add_watch(dir) through the regular plugin context instead of transform plugin context, so no dir -> module association is recorded at all.

And that association isn't actually needed here: all we need is for file changes in the dir to be watched, mimicking Vite's behavior of watching the root. So, somewhat by accident, the current behavior is actually correct.

We just need to do what Vite's import glob plugin does: store a module -> Vec<GlobMatch> map, then in the hotUpdate hook, iterate over the matchers to test the changed file. For example, if **/*.png matches a/b.png, we know which module needs to be updated, and the matched module is then invalidated for the incremental rebuild as well (i think so)

@shulaoda

shulaoda commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

I'll merge it first and fix any issues that come up afterward.

@shulaoda

shulaoda commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

Wouldn't that cause many false-positives for a glob like **/*.png since we don't have shouldTransformCachedModule hook?

That said, it can also lead to some unnecessary file events being propagated, which is essentially the same problem we have with watching the root. At this point, I'm starting to think that watching the root might actually be the better option.

@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from c4abb66 to c0985f3 Compare October 7, 2026 12:56
…tion via hotUpdate

Add the modules whose glob result changes to the affected set:

- for a created file, if the walk would match it and it is not part of
  the result yet
- for a deleted path, if it is a file of the result or a directory above
  one

The hook is off by default (`dev.hotUpdate`), so this takes effect once
the option is on.
…h globstar like vite

Build one matcher per glob call from its absolute globs, the way vite does with picomatch, and ask it for every created or deleted file. The result of the last walk is no longer kept.
@shulaoda
shulaoda force-pushed the feat/import-glob-hot-update branch from c0985f3 to 5cc9db6 Compare October 7, 2026 13:03
@shulaoda
shulaoda merged commit 40a0424 into main Oct 7, 2026
36 checks passed
@shulaoda
shulaoda deleted the feat/import-glob-hot-update branch October 7, 2026 13:10
@rolldown-guard rolldown-guard Bot mentioned this pull request Oct 7, 2026
shulaoda added a commit that referenced this pull request Oct 7, 2026
## [1.2.13] - 2026-10-07

### 🚀 Features

- rolldown_plugin_vite_import_glob: follow file creation and deletion via hotUpdate (#10550) by @shulaoda
- inline-common-chunks: deduplicate shared code along known loading paths (#11093) by @hyfdev
- rolldown_plugin_vite_import_glob: record a matcher per glob call in dev (#10592) by @shulaoda
- rolldown_plugin_vite_import_glob: watch the directory each glob reads (#10549) by @shulaoda
- watch: hand watch.exclude to notify as its ignore filter (#11008) by @shulaoda
- dev: add `experimental.devMode.hotUpdate` option (#11123) by @h-a-n-a

### 🐛 Bug Fixes

- renamer: check the printed name of namespace-alias imports for captures (#11129) by @IWANABETHATGUY
- devtools: honor `devtools.sessionId` by @Brooooooklyn
- renamer: keep a renamed nested binding off its module's top-level bindings (#11128) by @IWANABETHATGUY
- dev: keep `this` undefined when calling an imported function in HMR patches and lazy chunks (#11126) by @h-a-n-a
- dev: read string-named imports correctly in HMR patches and lazy chunks (#11127) by @h-a-n-a
- sanitize preserveModules chunk names after resolving the relative path (#11087) by @waltuov
- keep the `with` clause on bare external imports (#11100) by @IWANABETHATGUY
- generator: filter DISABLE_JS_HOOK bits from generated HookUsage (#10346) by @Brooooooklyn
- dev: make the module factory async for top-level await (#11118) by @h-a-n-a
- node: give each renderChunk call its own meta object (#11115) by @Brooooooklyn
- honor `externalLiveBindings` for imported exports (#11048) by @camc314
- dev: keep a late payload from replacing newer module code (#11030) by @h-a-n-a
- binding: use-after-free in async `transform` with a `TsconfigCache` (#11080) by @shulaoda
- fail on chunk syntax errors and escape export names once (#11051) by @hyfdev
- dev: ship newly imported modules the client never received (#11029) by @h-a-n-a
- code-splitting: avoid cycles from redundant re-export initialization (#11073) by @hyfdev
- link: treat re-exports of the same external binding as one export (#11069) by @IWANABETHATGUY
- escape namespace properties in IIFE and UMD output (#11085) by @hyfdev
- escape dependency paths in CJS and UMD output (#11084) by @hyfdev
- use computed access for arbitrary import names (#11083) by @hyfdev
- generate valid bindings for shimmed exports (#11082) by @hyfdev
- dev: reload the page when an HMR update fails after merging its edit (#11031) by @h-a-n-a
- dev/lazy: ship the deps of a module that a patch shipped but never ran (#11028) by @h-a-n-a
- code-splitting: don't move init calls in front of unwrapped JSON modules (#11062) by @IWANABETHATGUY
- escape export names in `defineProperty` calls (#11047) by @camc314

### 📚 Documentation

- say the CLI bundles multiple configs one after another (#11111) by @Brooooooklyn
- add `renderChunk` hook filter docs (#11065) by @sapphi-red

### ⚡ Performance

- use `concat_string!` instead of `format!` for plain string joins (#11139) by @IWANABETHATGUY
- utils: evaluate each `concat_string!` argument once (#11105) by @IWANABETHATGUY
- json: escape `JSON.parse` string with `json-escape-simd` (#11104) by @IWANABETHATGUY
- unwrap JSON modules with a non-object root (#11066) by @IWANABETHATGUY

### 🧪 Testing

- inline-common-chunks: fix flaky inlineCommonChunks concurrent imports (#11088) by @hyfdev

### ⚙️ Miscellaneous Tasks

- deps: upgrade oxc to 0.153.0 (#11109) by @camc314
- update pnpm to v12 (#10876) by @btea
- deps: update pnpm to v11.28.0 (#11049) by @renovate[bot]
- deps: update rollup submodule for tests to v4.64.0 (#11116) by @rolldown-guard[bot]
- deps: update napi (#11137) by @renovate[bot]
- scripts: spawn the rolldown CLI without a shell in setup-vite (#10875) by @Brooooooklyn
- deps: bump `@napi-rs/wasm-runtime` to `~1.2.5` (#11117) by @shulaoda
- deps: update napi (#11108) by @renovate[bot]
- deps: update taiki-e/install-action action to v2.87.22 (#11096) by @renovate[bot]
- deps: update npm packages (#11095) by @renovate[bot]
- deps: update rust crates (#11097) by @renovate[bot]
- deps: update napi (#11063) by @renovate[bot]
- deps: update dependency rust to v1.99.0 (#11071) by @renovate[bot]
- deps: update dependency vite-plus to v1 (#11056) by @renovate[bot]

Co-authored-by: shulaoda <[email protected]>
@sapphi-red

Copy link
Copy Markdown
Member

Oh, you're right. I took a closer look, and it turns out I'm calling add_watch(dir) through the regular plugin context instead of transform plugin context, so no dir -> module association is recorded at all.

And that association isn't actually needed here: all we need is for file changes in the dir to be watched, mimicking Vite's behavior of watching the root. So, somewhat by accident, the current behavior is actually correct.

We just need to do what Vite's import glob plugin does: store a module -> Vec<GlobMatch> map, then in the hotUpdate hook, iterate over the matchers to test the changed file. For example, if **/*.png matches a/b.png, we know which module needs to be updated, and the matched module is then invalidated for the incremental rebuild as well (i think so)

I thought incremental build won't run the rebuild unless the relationship is setup by this.addWatchFile. That how it used to work for incremental builds.

@sapphi-red

Copy link
Copy Markdown
Member

That said, it can also lead to some unnecessary file events being propagated, which is essentially the same problem we have with watching the root. At this point, I'm starting to think that watching the root might actually be the better option.

I don't see your point 🤔 What I'm pointing out is unnecessary rebuilds in build --watch or rebuilds in dev mode.

@shulaoda

Copy link
Copy Markdown
Member Author

I found that when incremental build is enabled, it doesn't trigger rebuilds, but it seems to produce extra modules. The changed files are treated as new modules and added to the module graph. 😅

shulaoda added a commit that referenced this pull request Nov 3, 2026
…reation and deletion (#10551)

Part of #10059

## Problem

Nothing covers end to end that a glob follows files appearing and
disappearing.

## What this PR does

Adds the `hmr-import-glob` playground. It covers a new file, a deleted
file, a file that does not match, a new nested directory, and a glob
root that is missing at boot, with and without its parent.

The playground configures no plugin. Vite installs the native one for
the bundled environment, so the test goes through the real Vite path.

## Note

The `hotUpdate` hook is off by default, so the playground turns it on
with `experimental.devMode.hotUpdate`. It passes locally on macOS.

There is no case for a directory that is moved away. The hook of #10550
does not follow it.

Co-authored-by: Hana <[email protected]>
shulaoda added a commit that referenced this pull request Nov 3, 2026
Part of #10059

## Problem

The matcher and the hook of #10550 are only covered end to end, by the
playground of #10551. It cannot reach cases like `exhaustive`, case
folding, or a path with glob syntax in it.

## What this PR does

- Adds unit tests for the matcher: single level and `**` patterns,
several patterns, negated patterns, dot entries and `node_modules`,
`exhaustive`, case folding, a glob that does not compile, and the
escaping of the literal part of a path.
- Adds unit tests for the hook: the order of the modules it adds, and
what it declines.
- Removes `test = false` from the crate, so the tests run.
- Adds `futures` as a dev dependency, to call the async hook.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants