Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Fix issue #14151: Call shouldExternalizeDep for virtual modules in dep-scan

fixes #14151
close #14206

  • Explore repository structure and understand the issue
  • Build the vite package successfully
  • Analyze the scan.ts file and identify the fix location
  • Implement the fix to call shouldExternalizeDep after build.onResolve({ filter: htmlTypesRE }
  • Create a test case in playground/optimize-deps
  • Lint and build the changes
  • Verify the fix manually by starting dev server
  • Test with reproduction from issue [plugin vite:dep-scan] throw an error when start the dev server **at the first time** with import a virtual file #14151
  • Run security check (CodeQL) - no vulnerabilities found
  • Address review feedback (inline script, remove .gitignore entry)

Problem

When importing a virtual module with an HTML-like file extension (.vue, .svelte, .html, .astro, .imba), the dependency scanner would throw an ENOENT: no such file or directory error during the first dev server start. This happened because the scanner attempted to read virtual modules from the filesystem.

Solution

Added a check using shouldExternalizeDep() in the htmlTypesRE onResolve handler (line 505 in scan.ts). This function checks if a module is virtual (by detecting the null byte prefix \0 or if the resolved ID equals the raw ID) and returns early to externalize it, preventing the scanner from trying to load it from disk.

Changes Made

Core Fix (4 lines):

  • Modified packages/vite/src/node/optimizer/scan.ts to call shouldExternalizeDep() before trying to load HTML-like files

Test Case:

  • Added virtual module plugin in playground/optimize-deps/vite.config.js providing virtual:test-virtual-file/Foo.vue
  • Updated playground/optimize-deps/index.html with inline script to test virtual module import
  • Added test cases in playground/optimize-deps/__tests__/optimize-deps.spec.ts to verify no scan errors occur

Verification:

  • ✅ Dev server starts without errors
  • ✅ Virtual modules with .vue extension load correctly
  • ✅ No security vulnerabilities (CodeQL check passed)
  • ✅ Lint and format checks passed
  • ✅ Reproduced and verified fix with original issue scenario
Original prompt

Call shouldExternalizeDep after build.onResolve({ filter: htmlTypesRE } in @vitejs/vite/files/packages/vite/src/node/optimizer/scan.ts to fix @vitejs/vite/issues/14151 . Add a test in playground/optimize-deps


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…p-scan

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix shouldExternalizeDep call in scan.ts fix(deps): call shouldExternalizeDep for virtual modules with html-like extensions in dep scanner Oct 24, 2025
Copilot AI requested a review from sapphi-red October 24, 2025 15:04
Copilot finished work on behalf of sapphi-red October 24, 2025 15:04
@sapphi-red sapphi-red marked this pull request as ready for review October 24, 2025 15:07
@sapphi-red sapphi-red marked this pull request as draft October 24, 2025 15:07
Copy link
Member

@sapphi-red sapphi-red left a comment

Choose a reason for hiding this comment

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

@copilot please fix the test failure

Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 25, 2025

@copilot please fix the test failure

Addressed in commit 1c117e6. Inlined the script and removed the .gitignore entry. The test should now work correctly with the inlined script content in index.html.

Copilot AI requested a review from sapphi-red October 25, 2025 00:50
Copilot finished work on behalf of sapphi-red October 25, 2025 00:50
@sapphi-red sapphi-red added trigger: preview p2-edge-case Bug, but has workaround or limited in scope (priority) feat: deps optimizer Esbuild Dependencies Optimization labels Oct 26, 2025
@pkg-pr-new
Copy link

pkg-pr-new bot commented Oct 26, 2025

Open in StackBlitz

npm i https://pkg.pr.new/vite@21001

commit: acd660e

@sapphi-red sapphi-red marked this pull request as ready for review October 26, 2025 10:43
@sapphi-red sapphi-red changed the title fix(deps): call shouldExternalizeDep for virtual modules with html-like extensions in dep scanner fix(optimizer): call shouldExternalizeDep for virtual modules with html-like extensions in dep scanner Oct 26, 2025
@sapphi-red sapphi-red changed the title fix(optimizer): call shouldExternalizeDep for virtual modules with html-like extensions in dep scanner fix(optimizer): externalize virtual modules for html like files Oct 26, 2025
@sapphi-red sapphi-red merged commit e5af352 into main Oct 29, 2025
26 of 27 checks passed
@sapphi-red sapphi-red deleted the copilot/fix-call-shouldexternalizedep branch October 29, 2025 02:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: deps optimizer Esbuild Dependencies Optimization p2-edge-case Bug, but has workaround or limited in scope (priority) trigger: preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[plugin vite:dep-scan] throw an error when start the dev server **at the first time** with import a virtual file

2 participants