fix(resolve): strip query string from id before filesystem resolution#359
fix(resolve): strip query string from id before filesystem resolution#359LeSingh1 wants to merge 1 commit into
Conversation
When a module specifier contains a query string (e.g. `./foo.js?v=123` for cache-busting or HMR), the query must be stripped before the path is passed to `moduleResolve` / stat checks. Without this, resolution fails because no file named `foo.js?v=123` exists on disk. Handles relative paths, absolute paths, bare package specifiers, and file:// URLs with appended queries. Adds four tests covering each case.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesQuery String Stripping in
Estimated code review effort🎯 2 (Simple) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Problem
When a module specifier contains a query string — a common pattern for cache-busting (
./foo.js?v=123) or Vite/Rollup HMR invalidation (./foo.js?t=1718000000) —resolveSync/resolvePathfail withERR_MODULE_NOT_FOUND.The root cause: the query string is never stripped before the
idis handed tomoduleResolve(fromimport-meta-resolve) or tostatSync. The OS/resolver looks for a file literally namedfoo.js?v=123which does not exist on disk.Reproduction (before fix)
This surfaces in real projects wherever a bundler/dev-server passes query-annotated specifiers through
mlly's resolution utilities (Nuxt, Nitro, Vite SSR, etc.).Fix
Strip the query portion of
id(everything from the first?onwards) early in_resolve(), after thefile://fast-path but before any filesystem ormoduleResolvecalls. This mirrors what Node's ownfileURLToPathalready does forfile://URLs, and whatsanitizeFilePath(inutils.ts) already does for the path-sanitisation helper.The query is meaningless for local file resolution — it carries no information about which file to open.
Tests added (
test/resolve.test.ts)./fixture/cjs.mjs?v=123resolves identically to./fixture/cjs.mjsfile:///path/to/cjs.mjs?t=456resolves to the real fileufo?hash=abcresolves identically touforesolvePathSyncend-to-end?in itAll 202 existing tests continue to pass.
Checklist
mainbefore fix_resolve)http:,https:,node:,data:specifiers (those return early before the new code runs)202 passed)Summary by CodeRabbit
Bug Fixes
Tests