Skip to content

Conversation

abbasyed
Copy link
Contributor

@abbasyed abbasyed commented Oct 2, 2025

fix(import-glob): fix HMR for array patterns with exclusions

fix #20852

Description

Fixes HMR not triggering when files are added or deleted that match import.meta.glob array patterns with exclusions.

Problem

When using array patterns with exclusions like:

import.meta.glob(["./modules/*.js", "!./modules/_excluded.js"], {
  import: "default",
  eager: true,
})

HMR would fail to trigger when:

  1. Adding a file: New files matching the glob pattern wouldn't trigger HMR (page doesn't update)
  2. Deleting a file: Deleted files would cause the error "Pre-transform error: Failed to load url" instead of triggering HMR

Root Cause

The bug was in the hotUpdate hook in packages/vite/src/node/plugins/importMetaGlob.ts where negated patterns were being passed to picomatch with the ! prefix still attached.

Picomatch expects negated patterns without the ! prefix for proper matching. When a file was added/deleted, the matcher would try to match against "!/path/to/_excluded.js" instead of "/path/to/_excluded.js", causing the matcher to always return false.

Solution

Strip the ! prefix from negated patterns before passing them to picomatch:

for (const glob of globs) {
  if (glob[0] === '!') {
    negated.push(glob.slice(1))  // Remove the '!' prefix
  } else {
    affirmed.push(glob)
  }
}

Verification

Using the Reproduction Repo

You can verify this fix using the reproduction repository: https://github.com/MoritzLoewenstein/vite-import-glob-repro

  1. Clone the reproduction repo and install dependencies:
git clone https://github.com/MoritzLoewenstein/vite-import-glob-repro.git
cd vite-import-glob-repro
npm install
  1. Link this fixed version of Vite:
npm link /path/to/this/vite/packages/vite
  1. Start the dev server:
npm run dev
  1. In another terminal, test adding a file:
npm run addMod

Expected: The page should HMR automatically and show both included_a.js and included_b.js

  1. Test deleting the file:
npm run delMod

Expected: The page should HMR automatically and show only included_a.js (no errors)

Manual Verification

  1. Create a test project with the following structure:
test-project/
├── modules/
│   ├── included.js
│   └── excluded.js
└── main.js
  1. In main.js:
const modules = import.meta.glob(["./modules/*.js", "!./modules/excluded.js"], {
  import: "default",
  eager: true,
})
console.log(modules)
  1. Run vite and verify:
    • Only included.js appears in the output (excluded.js is filtered)
    • Adding a new file in modules/ triggers HMR
    • Deleting a file from modules/ triggers HMR without errors

Test Coverage

Added comprehensive test coverage in playground/glob-import/__tests__/glob-import.spec.ts:

  • Tests HMR for adding files with array patterns and exclusions
  • Tests HMR for removing files with array patterns and exclusions
  • Verifies exclusion patterns work correctly

Related Issues

This fix resolves the issue reported in the reproduction repository and ensures that array patterns with exclusions work correctly with HMR, matching the behavior of single-pattern globs.

Fixes HMR not triggering when files are added or deleted that match
import.meta.glob array patterns with exclusions.

The bug was in the hotUpdate hook where negated patterns were being
passed to picomatch with the '!' prefix still attached. Picomatch
expects negated patterns without the '!' prefix for proper matching.

When a file was added/deleted that matched a glob like:
["./modules/*.js", "!./modules/_excluded.js"]

The matcher would fail because it was trying to match against
"!/path/to/_excluded.js" instead of "/path/to/_excluded.js".

The fix strips the '!' prefix from negated patterns before passing
them to picomatch, allowing the matcher to correctly identify files
that match the affirmed patterns but not the negated ones.
Move array pattern test into main index.html to avoid page
navigation issues in the test environment. The test was failing
with 'Cannot navigate to invalid URL' when trying to use page.goto().

Also added a separate test to verify exclusion patterns work correctly.
- Add cleanup at start of HMR test to ensure clean state
- Make static test more flexible by checking properties instead of strict equality
- Move array-result element earlier in HTML to match test expectations

This fixes test failures caused by file persistence across test runs.
Move array pattern test to end of HTML to avoid breaking
existing test selectors and page structure.
Use expect.poll() to wait for content to load before parsing,
preventing JSON.parse errors when content isn't ready yet.
Remove .resolves which is not supported with expect.poll().
Use .toMatchObject() directly on polled value instead.
Add expect.poll() to subpath imports, #alias imports, and
import base glob raw tests to handle timing issues with
async content loading.
Add expect.poll() to wait for async content loading in the
escape test which was failing intermittently on macOS.
@sapphi-red sapphi-red added feat: hmr p3-minor-bug An edge case that only affects very specific usage (priority) labels Oct 2, 2025
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.

Thanks!

@sapphi-red sapphi-red changed the title fix(import-glob): fix HMR for array patterns with exclusions fix(glob): fix HMR for array patterns with exclusions Oct 2, 2025
@sapphi-red sapphi-red merged commit 63e040f into vitejs:main Oct 2, 2025
17 checks passed
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 3, 2025
| datasource | package | from  | to    |
| ---------- | ------- | ----- | ----- |
| npm        | vite    | 7.1.7 | 7.1.8 |


## [v7.1.8](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small-718-2025-10-02-small)

##### Bug Fixes

- **css:** improve url escape characters handling ([#20847](vitejs/vite#20847)) ([24a61a3](vitejs/vite@24a61a3))
- **deps:** update all non-major dependencies ([#20855](vitejs/vite#20855)) ([788a183](vitejs/vite@788a183))
- **deps:** update artichokie to 0.4.2 ([#20864](vitejs/vite#20864)) ([e670799](vitejs/vite@e670799))
- **dev:** skip JS responses for document requests ([#20866](vitejs/vite#20866)) ([6bc6c4d](vitejs/vite@6bc6c4d))
- **glob:** fix HMR for array patterns with exclusions ([#20872](vitejs/vite#20872)) ([63e040f](vitejs/vite@63e040f))
- keep ids for virtual modules as-is ([#20808](vitejs/vite#20808)) ([d4eca98](vitejs/vite@d4eca98))
- **server:** drain stdin when not interactive ([#20837](vitejs/vite#20837)) ([bb950e9](vitejs/vite@bb950e9))
- **server:** improve malformed URL handling in middlewares ([#20830](vitejs/vite#20830)) ([d65a983](vitejs/vite@d65a983))

##### Documentation

- **create-vite:** provide deno example ([#20747](vitejs/vite#20747)) ([fdb758a](vitejs/vite@fdb758a))

##### Miscellaneous Chores

- **deps:** update rolldown-related dependencies ([#20810](vitejs/vite#20810)) ([ea68a88](vitejs/vite@ea68a88))
- **deps:** update rolldown-related dependencies ([#20854](vitejs/vite#20854)) ([4dd06fd](vitejs/vite@4dd06fd))
- update url of `create-react-app` license ([#20865](vitejs/vite#20865)) ([166a178](vitejs/vite@166a178))
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 3, 2025
| datasource | package | from  | to    |
| ---------- | ------- | ----- | ----- |
| npm        | vite    | 7.1.7 | 7.1.9 |


## [v7.1.9](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small-719-2025-10-03-small)

##### Reverts

- **server:** drain stdin when not interactive ([#20885](vitejs/vite#20885)) ([12d72b0](vitejs/vite@12d72b0))


## [v7.1.8](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small-718-2025-10-02-small)

##### Bug Fixes

- **css:** improve url escape characters handling ([#20847](vitejs/vite#20847)) ([24a61a3](vitejs/vite@24a61a3))
- **deps:** update all non-major dependencies ([#20855](vitejs/vite#20855)) ([788a183](vitejs/vite@788a183))
- **deps:** update artichokie to 0.4.2 ([#20864](vitejs/vite#20864)) ([e670799](vitejs/vite@e670799))
- **dev:** skip JS responses for document requests ([#20866](vitejs/vite#20866)) ([6bc6c4d](vitejs/vite@6bc6c4d))
- **glob:** fix HMR for array patterns with exclusions ([#20872](vitejs/vite#20872)) ([63e040f](vitejs/vite@63e040f))
- keep ids for virtual modules as-is ([#20808](vitejs/vite#20808)) ([d4eca98](vitejs/vite@d4eca98))
- **server:** drain stdin when not interactive ([#20837](vitejs/vite#20837)) ([bb950e9](vitejs/vite@bb950e9))
- **server:** improve malformed URL handling in middlewares ([#20830](vitejs/vite#20830)) ([d65a983](vitejs/vite@d65a983))

##### Documentation

- **create-vite:** provide deno example ([#20747](vitejs/vite#20747)) ([fdb758a](vitejs/vite@fdb758a))

##### Miscellaneous Chores

- **deps:** update rolldown-related dependencies ([#20810](vitejs/vite#20810)) ([ea68a88](vitejs/vite@ea68a88))
- **deps:** update rolldown-related dependencies ([#20854](vitejs/vite#20854)) ([4dd06fd](vitejs/vite@4dd06fd))
- update url of `create-react-app` license ([#20865](vitejs/vite#20865)) ([166a178](vitejs/vite@166a178))
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Oct 4, 2025
| datasource | package | from  | to    |
| ---------- | ------- | ----- | ----- |
| npm        | vite    | 7.1.7 | 7.1.9 |


## [v7.1.9](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small-719-2025-10-03-small)

##### Reverts

- **server:** drain stdin when not interactive ([#20885](vitejs/vite#20885)) ([12d72b0](vitejs/vite@12d72b0))


## [v7.1.8](https://github.com/vitejs/vite/blob/HEAD/packages/vite/CHANGELOG.md#small-718-2025-10-02-small)

##### Bug Fixes

- **css:** improve url escape characters handling ([#20847](vitejs/vite#20847)) ([24a61a3](vitejs/vite@24a61a3))
- **deps:** update all non-major dependencies ([#20855](vitejs/vite#20855)) ([788a183](vitejs/vite@788a183))
- **deps:** update artichokie to 0.4.2 ([#20864](vitejs/vite#20864)) ([e670799](vitejs/vite@e670799))
- **dev:** skip JS responses for document requests ([#20866](vitejs/vite#20866)) ([6bc6c4d](vitejs/vite@6bc6c4d))
- **glob:** fix HMR for array patterns with exclusions ([#20872](vitejs/vite#20872)) ([63e040f](vitejs/vite@63e040f))
- keep ids for virtual modules as-is ([#20808](vitejs/vite#20808)) ([d4eca98](vitejs/vite@d4eca98))
- **server:** drain stdin when not interactive ([#20837](vitejs/vite#20837)) ([bb950e9](vitejs/vite@bb950e9))
- **server:** improve malformed URL handling in middlewares ([#20830](vitejs/vite#20830)) ([d65a983](vitejs/vite@d65a983))

##### Documentation

- **create-vite:** provide deno example ([#20747](vitejs/vite#20747)) ([fdb758a](vitejs/vite@fdb758a))

##### Miscellaneous Chores

- **deps:** update rolldown-related dependencies ([#20810](vitejs/vite#20810)) ([ea68a88](vitejs/vite@ea68a88))
- **deps:** update rolldown-related dependencies ([#20854](vitejs/vite#20854)) ([4dd06fd](vitejs/vite@4dd06fd))
- update url of `create-react-app` license ([#20865](vitejs/vite#20865)) ([166a178](vitejs/vite@166a178))
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)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

import.meta.glob negative pattern hmr failure
2 participants