Repository navigation
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
Author
|
hope this is still relevant — ping me if you'd like any changes |
Contributor
|
The PR has been manually labeled as likely to be created by a bot, LLM, or agent, and will be automatically closed. This may be because the PR contains LLM-generated descriptions, does not follow the PR template, is overly verbose, or does not contain any fruitful interaction, which harms the review process. Please read our AI policy for more information. If you believe this is a mistake, please reply to this comment and we will review it. |
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