Skip to content

Conversation

@maxpatiiuk
Copy link
Contributor

Description

Fixes #19213

abstract from bug description:

This is a Vite bug that affects Vitest.

vite:html-inline-proxy plugin is supposed to resolve requests for module scripts from index.html, but it fails to do so in some environments. The issue is that this plugin expects URL to include ?html-proxy, but in some cases the URL instead includes ?v=12535af4&html-proxy, causing regex to not match, and thus request fails to resolve correctly. See Explanation section below for more details.

const htmlProxyRE = /[?&]html-proxy\b/
const htmlProxyIndexRE = /&index=(\d+)/
const commonjsProxyRE = /\?commonjs-proxy/
const commonjsProxyRE = /[?&]commonjs-proxy/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this looks to be the only other place where such bug is present

const htmlProxyRE = /[?&]html-proxy\b/
const htmlProxyIndexRE = /&index=(\d+)/
const commonjsProxyRE = /\?commonjs-proxy/
const commonjsProxyRE = /[?&]commonjs-proxy/
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I noticed another possible bug:
Vite built-in plugins use a convention of appending query-like arguments to import specifiers to update the plugin behavior (example: ?url)
Custom plugins may also adapt this convention.

However, if ? character is present in the import specifier, some parts of vite code incorrectly consider it to be a glob pattern:

if (isDynamicPattern(file)) {

if (isDynamicPattern(id)) {

if (resolvedPatterns.every((str) => !isDynamicPattern(str))) {

I will refactor my plugin to not use the ? convention

Copy link
Member

@sapphi-red sapphi-red Apr 1, 2025

Choose a reason for hiding this comment

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

However, if ? character is present in the import specifier, some parts of vite code incorrectly consider it to be a glob pattern:

All these codes receives globs (including normal file paths) instead of a list of ids (import specifiers). So it should be fine to use ?s in ids.

@sapphi-red sapphi-red added feat: html p2-edge-case Bug, but has workaround or limited in scope (priority) labels Apr 1, 2025
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.

Thanks!

@maxpatiiuk
Copy link
Contributor Author

Thank you for the review
I do not have the merge permission so please merge this pull request if it looks good

@sapphi-red
Copy link
Member

We need 1 more reviews before merging as we normally require 2 or more reviews.
https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md#pull-request-review-workflow

@patak-dev patak-dev merged commit f530a72 into vitejs:main Apr 4, 2025
25 of 26 checks passed
moonlitusun pushed a commit to moonlitusun/vite that referenced this pull request May 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat: html p2-edge-case Bug, but has workaround or limited in scope (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Vite fails to resolve html-proxy in vitest browser mode

3 participants