fix: fix importOriginal with optimizer and query import#10469
Conversation
✅ Deploy Preview for vitest-dev ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify project configuration. |
hi-ogawa
left a comment
There was a problem hiding this comment.
Please add integration test.
|
Hi @hi-ogawa, I'd be happy to add one I couldn't find an existing test suite that directly covers this scenario or usage. Could you point me toward the preferred location for this integration test, along with maybe an example of a similar test you're looking for please? Thank you |
|
For example, there's an integration test with vitest/test/e2e/test/optimize-deps.test.ts Lines 7 to 10 in 46b8277 vitest/test/e2e/fixtures/optimize-deps/vitest.config.ts Lines 3 to 6 in 46b8277 |
|
Added integration test. The test fails on the old code with the exact issue reported |
|
Can you test optmizeDeps scneario that reproduced the issue previously? |
|
I apologize i misunderstood. I updated the optimizeDeps test suite to include a new test. This new test uses the same The test uses I removed the other test i had added but i can add it back if you'd like though i think only one is necessary |
Co-authored-by: Codex <noreply@openai.com>
importOriginal with optimizer and query import
| .replace(new RegExp(`([?&])${queryToRemove}(?:&|$)`), '$1') | ||
| .replace(trailingSeparatorRE, '') |
There was a problem hiding this comment.
It looks like Claude and Me hallucinated bad removeQuery in #9772. Now I ported the one from Vite that does handles the multiple queries properly.
There was a problem hiding this comment.
Pull request overview
Fixes a regression where importOriginal/vi.importActual could produce invalid module IDs when stripping Vitest’s internal _vitest_original flag from URLs that also include optimizer/version query parameters, leading to ERR_MODULE_NOT_FOUND (issue #9887).
Changes:
- Adjust
removeQueryin the module runner to correctly remove_vitest_originalwithout leaving malformed query separators. - Add a unit test asserting
importOriginalpreserves query parameters (e.g.?raw). - Add an e2e fixture/test covering mocking behavior with SSR optimizer + (noExternal/external) dependency permutations.
Reviewed changes
Copilot reviewed 10 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/unit/test/mocking/import-actual-query.test.ts | New unit test ensuring importOriginal works with import IDs containing query (?raw). |
| test/unit/test/mocking/import-actual-query-target.ts | Target module used by the new unit test. |
| test/tsconfig.json | Adds vite/client types so query-suffixed imports (e.g. ?raw) typecheck in tests. |
| test/e2e/test/optimize-deps-mock.test.ts | New e2e test runner asserting the new optimize-deps mock fixture passes across pools. |
| test/e2e/package.json | Adds test-dep-simple2 to e2e workspace dependencies for the new fixture. |
| test/e2e/fixtures/optimize-deps-mock/vitest.config.ts | New fixture config exercising SSR optimizer behavior in the mocked-deps scenario. |
| test/e2e/fixtures/optimize-deps-mock/basic.test.ts | New fixture test mocking optimized + non-optimized deps and validating resolution behavior. |
| test/e2e/deps/dep-simple2/package.json | Adds a simple ESM dependency used by the new e2e fixture. |
| test/e2e/deps/dep-simple2/index.js | Implementation for test-dep-simple2. |
| pnpm-lock.yaml | Lockfile update for the new local e2e dependency. |
| packages/vitest/src/runtime/moduleRunner/utils.ts | Updates query manipulation logic used when stripping _vitest_original during module fetch. |
Files not reviewed (1)
- pnpm-lock.yaml: Language not supported
|
Thanks for fixing this! Would it be possible to backport to the It blocks Minimal config that triggers it: // vitest.config.ts
export default defineConfig({
optimizeDeps: { include: ['@scope/ui'] },
test: { deps: { optimizer: { client: { enabled: true, include: ['@scope/ui'] } } } },
})
// a spec doing: vi.mock('@scope/ui', async (io) => ({ ...await io(), x: vi.fn() })) -> ERR_MODULE_NOT_FOUND |
Description
original ocmment
Fxes a bug where
removeQueryincorrectly strips out the leading?parameter boundary symbol when purging internal runtime flags (like_vitest_original), leaving an orphaned&boundary behind. When a URL contains multiple query parameters (e.g.,file.js?_vitest_original&v=12345), removing the first parameter results in an invalid string format (file.js&v=12345). The module evaluator subsequently treats the query parameter as a literal extension on disk, throwing anERR_MODULE_NOT_FOUNDruntime error.This fix ensures that if a leading
?is stripped but subsequent¶meters remain, the first remaining ampersand is properly converted back into a?query parameter delimiter.Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
pnpm-lock.yamlunless you introduce a new test example.Tests
pnpm test:ci.Documentation
pnpm run docscommand.Changesets
feat:,fix:,perf:,docs:, orchore:.