Skip to content

fix: require path separators in isInNodeModules check - #21638

Closed
veeceey wants to merge 2 commits into
vitejs:mainfrom
veeceey:fix/issue-17467-isInNodeModules-check
Closed

veeceey wants to merge 2 commits into
vitejs:mainfrom
veeceey:fix/issue-17467-isInNodeModules-check

Conversation

@veeceey

@veeceey veeceey commented Feb 14, 2026

Copy link
Copy Markdown

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
@veeceey

veeceey commented Mar 10, 2026

Copy link
Copy Markdown
Author

hope this is still relevant — ping me if you'd like any changes

@sapphi-red sapphi-red added the bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR label Aug 28, 2026
@github-actions

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: likely Likely a bot, LLM, or agent. Automatically comments and closes the issue or PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

isInNodeModules check is too loose

2 participants