[fix] Vite outDir for sourcemaps upload (take 2) - #186
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR ensures bundler output directories are normalized to absolute paths for reliable sourcemaps uploading and refactors the bundler-report plugin to leverage new helper functions.
- Normalize
decomposePathAPI and update sourcemaps tests to use absoluteoutDir - Refactor bundler-report plugin (
index.ts) to usecomputeCwd,computeOutDir, and related helpers - Introduce
rollup.tshelpers and expand tests; addrollupas a devDependency
Reviewed Changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/plugins/error-tracking/src/sourcemaps/index.test.ts | Expanded scenario tests covering multiple bundlers and prefixes |
| packages/plugins/error-tracking/src/sourcemaps/files.ts | Updated decomposePath to accept prefix and absolute outDir |
| packages/plugins/error-tracking/src/sourcemaps/files.test.ts | Adjusted tests to match new decomposePath signature |
| packages/plugins/bundler-report/src/index.ts | Refactored plugin logic to use rollup helpers for path/CWD |
| packages/plugins/bundler-report/src/index.test.ts | Rewrote tests to validate outDir and cwd per bundler |
| packages/plugins/bundler-report/src/helpers/rollup.ts | Added computeCwd, computeOutDir, etc., for rollup support |
| packages/plugins/bundler-report/src/helpers/rollup.test.ts | Added tests covering rollup helper functions |
| packages/plugins/bundler-report/package.json | Added rollup devDependency |
Comments suppressed due to low confidence (1)
packages/plugins/bundler-report/src/index.test.ts:361
- The mock functions
reportCallsandcwdCallsare not reset between test cases, causing cumulative counts. Add abeforeEachor invoke.mockClear()to reset them before each test.
expect(reportCalls).toHaveBeenCalledTimes(1);
🚨 BugBot couldn't runBugBot is experiencing high demand right now. Try again in a few minutes by commenting "bugbot run" (requestId: serverGenReqId_bcbd29d0-616a-45d5-8e7b-79cb9ba9048c). |
|
bugbot run |
🚨 BugBot couldn't runBugBot is experiencing high demand right now. Try again in a few minutes by commenting "bugbot run" (requestId: serverGenReqId_db6684b4-6f19-4bc5-8340-8133e25f3a39). |
|
bugbot run |
There was a problem hiding this comment.
Bug: Shared State Causes Incorrect CWD and OutDir
The gotViteCwd variable is declared at module scope, creating shared state across plugin instances. This causes two issues in Vite plugin hooks:
- Incorrect CWD determination: In watch mode or concurrent builds, the flag retains state or causes race conditions, preventing
context.cwdfrom being correctly reset toprocess.cwd()whenconfig.rootis not provided. - Incorrect
outDircalculation: Whenconfig.rootis not provided andconfig.build.outDiris an absolute path, thepath.relativecalculation in theoptionshook uses a stalecontext.cwd(before it's updated toprocess.cwd()), resulting in an incorrectly computedcontext.bundler.outDir.
packages/plugins/bundler-report/src/index.ts#L42-L131
build-plugins/packages/plugins/bundler-report/src/index.ts
Lines 42 to 131 in e7835a3
Was this report helpful? Give feedback by reacting with 👍 or 👎
| const expectedErrors = shouldThrow ? [shouldThrow] : []; | ||
|
|
||
| try { | ||
| const result = computeCwd(options as InputOptions); |
There was a problem hiding this comment.
similarly here
Potentially you can add a:
const cases = [
// ...
] satisfies Array<{ description: string; expected: string; options: InputOptions }>;There was a problem hiding this comment.
Still need it because Rollup doesn't type InputOptions['output'], yet, it's there.
Added a comment to explain it.
| try { | ||
| const result = computeCwd(options as InputOptions); | ||
| results.push(result); | ||
| } catch (error: any) { |
There was a problem hiding this comment.
| } catch (error: any) { | |
| } catch (error: Error) { |
There was a problem hiding this comment.
TypeScript won't let me.
Catch clause variable type annotation must be 'any' or 'unknown' if specified.
ts(1196)
- early outs - comments - types
| context.bundler.outDir = getAbsolutePath(process.cwd(), context.bundler.outDir); | ||
| // Update the bundler's outDir based on the CWD. | ||
| context.bundler.outDir = getAbsoluteOutDir(context.cwd, relativeOutDir); | ||
| } |
There was a problem hiding this comment.
Bug: Vite Plugin Fails to Adjust Output Directory
The Vite plugin's options hook contains a bug in its fallback logic when config.root is not provided. When context.cwd is reset to process.cwd(), the context.bundler.outDir is recalculated. The issue arises because path.relative is used to compute a relative path from the old context.cwd to the existing context.bundler.outDir (which might be an absolute path or an empty string). This relative path is then incorrectly resolved against the new context.cwd (i.e., process.cwd()), leading to an incorrect final context.bundler.outDir.
Locations (1)
There was a problem hiding this comment.
What would be the vite configuration that would lead to this bug?
What and why?
Fixing #179 again, apparently #182 didn't fully fix it.
How?
I think the uncertainty and flimsiness is coming from the internal
bundler-reportwherebundler.outDirwouldn't be totally normalised across bundlers (sometimes relative, absolute, leading slashes, ...).So I totally reworked this section to be normalised as an absolute path.
Added a ton of tests in both this section and the sourcemaps uploading.