Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles, runFileInNode }) => {
"sentry-cli-mock.json": "["release","create","CURRENT_SHA","--project","fake-project"],
["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ test(import.meta.url, ({ runBundler, readOutputFiles }) => {
",
"sentry-cli-mock.json": "["release","set-commits","CURRENT_SHA","--auto"],
["release","finalize","CURRENT_SHA"],
["sourcemap","upload","-p","fake-project","--release","CURRENT_SHA","sentry-bundler-plugin-upload-path"],
",
}
`);
Expand Down
1 change: 1 addition & 0 deletions packages/bundler-plugins/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"@types/node": "^20.19.0",
"premove": "^4.0.0",
"rolldown": "^1.0.0",
"vite": "^8.3.1",
"vitest": "^4.1.11",
"webpack": "5.104.1"
},
Expand Down
21 changes: 18 additions & 3 deletions packages/bundler-plugins/src/rollup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { createRequire } from 'node:module';
import { finalizeRolldownDebugIds, getDebugIdForChunk, hasExistingDebugID } from './debug-id-injection';
import { stampDebugIds, type OutputBundle } from './debug-id-stamping';
import { createSourceMapHandling, type OutputDestinationOptions } from './source-map-handling';
import { createViteSourceMapHooks } from '../vite/source-map-hooks';

// The subset of Rollup's `TransformResult` that this plugin's `transform`
// hook actually returns. Defined locally instead of imported from `rollup`
Expand Down Expand Up @@ -321,15 +322,29 @@ export function _createRollupPluginInternals(
...(shouldTransform ? { transform: transformHook } : {}),
// Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin
// decides in its `config` hook instead.
...(buildTool === 'rollup' ? { outputOptions: sourceMapHandling.outputOptions } : {}),
...(buildTool === 'rollup'
? { outputOptions: sourceMapHandling.outputOptions }
: createViteSourceMapHooks(sentryBuildPluginManager, sourceMapHandling)),
renderChunk,
generateBundle: { order: 'pre' as const, handler: generateBundle },
writeBundle,
};

const post = {
name: `sentry-${buildTool}-upload-staging`,
generateBundle: { order: 'post' as const, handler: sourceMapHandling.stageSourceMaps },
name: buildTool === 'vite' ? 'sentry-vite-debug-id-sourcemaps' : 'sentry-rollup-upload-staging',
generateBundle: {
order: 'post' as const,
async handler(outputOptions: OutputDestinationOptions, bundle: OutputBundle, isWrite?: boolean): Promise<void> {
if (buildTool === 'vite' && options.sourcemaps?.disable === 'disable-upload') {
// Vite's module preload rewrite in `generateBundle` regenerates the source map of every
// chunk with a dynamic import, which drops the debug ID the main plugin stamped in its
// `pre` hook. Restore it without touching the chunk code, so file hashes stay valid.
stampDebugIds(bundle, false);
}

await sourceMapHandling.stageSourceMaps(outputOptions, bundle, isWrite);
},
},
};

return { main, post };
Expand Down
35 changes: 8 additions & 27 deletions packages/bundler-plugins/src/vite/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import type { SentryRollupPluginOptions } from '../rollup';
import { _rollupPluginInternal } from '../rollup';
import { _createRollupPluginInternals } from '../rollup';
import { createRequire } from 'node:module';
import { stampDebugIds, type OutputBundle } from '../rollup/debug-id-stamping';

interface SentryVitePlugin {
name: string;
enforce: 'pre' | 'post';
generateBundle?: {
order: 'pre' | 'post';
handler: (_outputOptions: unknown, bundle: OutputBundle) => void;
};
[hook: string]: unknown;
}

function getViteMajorVersion(): string | undefined {
Expand All @@ -27,27 +23,12 @@ function getViteMajorVersion(): string | undefined {
}

export const sentryVitePlugin = (options?: SentryRollupPluginOptions): SentryVitePlugin[] => {
const plugins: SentryVitePlugin[] = [
{
enforce: 'pre',
..._rollupPluginInternal(options, 'vite', getViteMajorVersion()),
},
];

if (!options?.disable && options?.sourcemaps?.disable === 'disable-upload') {
plugins.push({
name: 'sentry-vite-debug-id-sourcemaps',
enforce: 'post',
generateBundle: {
order: 'post',
handler(_outputOptions, bundle) {
// Vite's module preload rewrite in `generateBundle` regenerates the source map of every
// chunk with a dynamic import, which drops the debug ID the main plugin stamped in its
// `pre` hook. Restore it without touching the chunk code, so file hashes stay valid.
stampDebugIds(bundle, false);
},
},
});
const { main, post } = _createRollupPluginInternals(options, 'vite', getViteMajorVersion());

const plugins: SentryVitePlugin[] = [{ enforce: 'pre', ...main }];

if (post) {
plugins.push({ enforce: 'post', ...post });
}

return plugins;
Expand Down
128 changes: 128 additions & 0 deletions packages/bundler-plugins/src/vite/source-map-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import * as path from 'node:path';
import type { SentryBuildPluginManager, SourceMapDecision } from '../core';
import type { RollupSourceMapSetting, SourceMapHandling } from '../rollup/source-map-handling';
import { sourceMapKindFromRollupSetting } from '../rollup/source-map-handling';

type BundlerOutput = { dir?: string; sourcemap?: RollupSourceMapSetting };
type BundlerOutputOptions = BundlerOutput | BundlerOutput[] | undefined;

type ViteBuildOptions = {
sourcemap?: RollupSourceMapSetting;
outDir?: string;
rollupOptions?: { output?: BundlerOutputOptions };
rolldownOptions?: { output?: BundlerOutputOptions };
};

export type ViteUserConfig = {
build?: ViteBuildOptions;
environments?: Record<string, { build?: ViteBuildOptions } | undefined>;
};

export type ViteResolvedConfig = {
root: string;
build: ViteBuildOptions;
environments?: Record<string, { build: ViteBuildOptions }>;
};

export type ViteSourceMapHooks = {
config: (userConfig: ViteUserConfig, env: { command: string }) => ViteUserConfig | null;
configResolved: (config: ViteResolvedConfig) => void;
};

const NO_CHANGE: SourceMapDecision = { enableHidden: false, deleteAfterUpload: false };

type EnvironmentOutput = { destination: string; sourcemap: RollupSourceMapSetting };

/**
* The outputs an environment writes: `build.outDir` (relative to the root) unless an output in
* `rollupOptions` or `rolldownOptions` sets its own `dir`, which the bundler resolves from the
* working directory. An output's own `sourcemap` overrides `build.sourcemap`.
*/
function getOutputs(root: string, build: ViteBuildOptions): EnvironmentOutput[] {
const configured = build.rolldownOptions?.output ?? build.rollupOptions?.output;
const outputs = (Array.isArray(configured) ? configured : [configured]).filter(
(output): output is BundlerOutput => output !== undefined,
);
const outputsWithDir = outputs.filter(
(output): output is BundlerOutput & { dir: string } => typeof output.dir === 'string',
);

if (outputsWithDir.length > 0) {
return outputsWithDir.map(output => ({
destination: path.resolve(output.dir),
sourcemap: output.sourcemap ?? build.sourcemap,
}));
}

return [
{
destination: path.resolve(root, build.outDir ?? 'dist'),
sourcemap: outputs.find(output => output.sourcemap !== undefined)?.sourcemap ?? build.sourcemap,
},
];
}

/**
* Vite warns about `rollupOptions.output.sourcemap`, so the Vite plugin decides on `build.sourcemap`
* in its `config` hook. The output directories are only known in `configResolved`, which then
* records the decision for each of them.
*/
export function createViteSourceMapHooks(
sentryBuildPluginManager: SentryBuildPluginManager,
sourceMapHandling: SourceMapHandling,
): ViteSourceMapHooks {
// `'*'` for the top-level setting, otherwise the name of an environment with its own.
const decisions = new Map<string, SourceMapDecision>();

return {
config: (userConfig, env) => {
if (env.command !== 'build') {
return null;
}

const decision = sentryBuildPluginManager.sourceMaps.decide(
sourceMapKindFromRollupSetting(userConfig.build?.sourcemap),
);
decisions.set('*', decision);

const result: ViteUserConfig = decision.enableHidden ? { build: { sourcemap: 'hidden' } } : {};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Silent sourcemap default change undocumented

Low Severity

Direct sentryVitePlugin() users who never set build.sourcemap now get hidden source maps enabled and removed after upload. That is a silent default change for existing apps, and this commit has no changelog or migration note. Flagged because the review rules require migration notes for silent default changes that affect existing apps.

Fix in Cursor Fix in Web

Triggered by project rule: PR Review Guidelines for Cursor Bot

Reviewed by Cursor Bugbot for commit f2ee9aa. Configure here.


for (const [name, environment] of Object.entries(userConfig.environments ?? {})) {
const setting = environment?.build?.sourcemap;
if (setting === undefined) {
continue;
}

const environmentDecision = sentryBuildPluginManager.sourceMaps.decide(sourceMapKindFromRollupSetting(setting));
decisions.set(name, environmentDecision);

if (environmentDecision.enableHidden) {
result.environments = { ...result.environments, [name]: { build: { sourcemap: 'hidden' } } };
}
}

return result;
},

configResolved: config => {
const environments = config.environments ?? { client: { build: config.build } };

for (const [name, environment] of Object.entries(environments)) {
const decision = decisions.get(name) ?? decisions.get('*') ?? NO_CHANGE;

for (const output of getOutputs(config.root, environment.build)) {
// A plugin that ran after ours may have changed the setting again,
// or the output sets its own.
if (decision.deleteAfterUpload && output.sourcemap !== 'hidden') {
sentryBuildPluginManager.logger.debug(
`Source maps of the "${name}" environment are set to ${JSON.stringify(output.sourcemap)}. Not removing them after upload.`,
);
sourceMapHandling.setDecision(output.destination, NO_CHANGE);
} else {
sourceMapHandling.setDecision(output.destination, decision);
}
}
}
},
};
}
Loading
Loading