Skip to content

fix(importMetaGlob): throw early for relative glob patterns in virtual modules - #22358

Closed
DoriAlagem wants to merge 2 commits into
vitejs:mainfrom
DoriAlagem:fix/glob-relative-error-in-virtual-modules
Closed

DoriAlagem wants to merge 2 commits into
vitejs:mainfrom
DoriAlagem:fix/glob-relative-error-in-virtual-modules

Conversation

@DoriAlagem

Copy link
Copy Markdown

Fixes #22345.

Problem

When a plugin produces a virtual module that calls import.meta.glob with a relative pattern (e.g. './*.js'), Vite silently returns {} instead of throwing the documented error:

In virtual modules, all globs must start with '/'

The existing check inside resolvePaths is only reached when matched files are iterated. If the relative pattern matches no files (or matches files that are then filtered out), resolvePaths is never called — so the error is never thrown and the caller gets an empty object with no diagnostic.

Minimal repro:

// vite plugin
load(id) {
  if (id === '\0foo') return `export default import.meta.glob('./*.js')`
}

modules is {} with no warning or error logged.

Fix

Add an early guard immediately before the glob() call. If the module is virtual (!dir), the pattern is relative (isRelative), and no base option is set, throw the same error message that already exists further down in resolvePaths. This ensures the user gets a clear diagnostic regardless of whether any files happen to match the broken pattern.

@DoriAlagem

Copy link
Copy Markdown
Author

Hey @bluwy — just a gentle ping on this one! It's been about a week since opening. Happy to make any adjustments if needed.

@sapphi-red sapphi-red added p3-minor-bug An edge case that only affects very specific usage (priority) inconsistency Inconsistency between dev & build labels May 14, 2026

@sapphi-red sapphi-red left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you fix the test failure?

@sapphi-red

Copy link
Copy Markdown
Member

closing as #22497 is merged

@sapphi-red sapphi-red closed this May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inconsistency Inconsistency between dev & build 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.

Virtual module import.meta.glob('./...') silently returns {} instead of erroring

2 participants