fix: require path separators in isInNodeModules check#21638
Open
veeceey wants to merge 2 commits intovitejs:mainfrom
Open
fix: require path separators in isInNodeModules check#21638veeceey wants to merge 2 commits intovitejs:mainfrom
veeceey wants to merge 2 commits intovitejs:mainfrom
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this does
Fixes
isInNodeModulesto require path separators aroundnode_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 currentisInNodeModulescheck returnstruefor all files in that project. This causesensureVersionQueryto 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_bugand add an inline script tag toindex.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 whennode_modulesis a real directory segment in the path.Added unit tests covering:
node_modules_bug,my_node_modules)node_modulesat allFixes #17467