Skip to content

fix: require path separators in isInNodeModules check#21638

Open
veeceey wants to merge 2 commits intovitejs:mainfrom
veeceey:fix/issue-17467-isInNodeModules-check
Open

fix: require path separators in isInNodeModules check#21638
veeceey wants to merge 2 commits intovitejs:mainfrom
veeceey:fix/issue-17467-isInNodeModules-check

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 14, 2026

What this does

Fixes isInNodeModules to require path separators around node_modules, preventing false positives when the string appears as a substring of a directory name.

The problem

If your project path contains "node_modules" as part of a directory name (e.g., /path/to/node_modules_bug/), the current isInNodeModules check returns true for all files in that project. This causes ensureVersionQuery to inject version hashes into URLs that shouldn't have them, which breaks html-proxy regex matching and results in inline <script> tags failing with "invalid JS syntax" errors.

Reproduction is straightforward: just name your project directory something like node_modules_bug and add an inline script tag to index.html.

The fix

Changed id.includes('node_modules') to check for /node_modules/ and \node_modules\ — requiring actual path separators on both sides so it only matches when node_modules is a real directory segment in the path.

Added unit tests covering:

  • Standard forward-slash paths (Unix/normalized)
  • Backslash paths (Windows)
  • False positive cases (substring matches like node_modules_bug, my_node_modules)
  • Paths without node_modules at all

Fixes #17467

veeceey and others added 2 commits February 14, 2026 02:44
The `isInNodeModules` utility used a loose `id.includes('node_modules')`
check that would incorrectly match paths where "node_modules" appeared
as a substring of a directory name (e.g., `/path/to/node_modules_bug/`).

This caused several downstream issues, most notably:
- `ensureVersionQuery` would inject version hashes into URLs for project
  files when the project root contained "node_modules" as a substring
- This broke the html-proxy regex matching, causing inline script tags
  in HTML to fail with "invalid JS syntax" errors

The fix requires proper path separators (`/` or `\`) on both sides of
"node_modules", ensuring it's matched only as a real directory segment.

Fixes vitejs#17467
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.

isInNodeModules check is too loose

1 participant