Skip to content
Merged
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
17 changes: 6 additions & 11 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,11 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
sourceMapState.sdkEnabled && !hasEnvironmentSourceMapSetting(config, environment);
// A user's own glob replaces the SDK's deletion. Maps in a Cloudflare `_worker.js` folder
// stay, so that wrangler can read them for `upload_source_maps`.
// With `sourcemaps.delete` the bundler plugin ignores this option and warns if the user set it.
const deleteSourcemapsAfterUpload =
sourcemaps?.delete !== undefined
? sourcemaps.deleteSourcemapsAfterUpload
: (sourcemaps?.deleteSourcemapsAfterUpload ??
(sourcemaps?.filesToDeleteAfterUpload === undefined
? (environment?: string) =>
sdkEnabledSourceMaps(environment) && !buildState.writesCloudflareWorker ? true : undefined
: undefined));
const sdkDeleteSourceMaps =
sourcemaps?.delete === undefined && sourcemaps?.filesToDeleteAfterUpload === undefined
? (environment?: string) =>
sdkEnabledSourceMaps(environment) && !buildState.writesCloudflareWorker ? true : undefined
: undefined;

const sentryPlugins = sentryVitePlugin({
applicationKey,
Expand All @@ -112,8 +108,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
sourcemaps: {
...sourcemaps,
assets: sourcemaps?.assets ?? getSourcemapsAssetsGlobs(config),
deleteSourcemapsAfterUpload,
sdkEnabledSourceMaps,
sdkDeleteSourceMaps,
},
bundleSizeOptimizations: {
...bundleSizeOptimizations,
Expand Down
77 changes: 33 additions & 44 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { _getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration
import type { SentryOptions } from '../../src/integration/types';

type CapturedSourcemapsOptions = {
deleteSourcemapsAfterUpload?: boolean | ((environment?: string) => boolean | undefined);
sdkEnabledSourceMaps?: (environment?: string) => boolean;
deleteSourcemapsAfterUpload?: boolean;
sdkDeleteSourceMaps?: (environment?: string) => boolean | undefined;
};

let capturedSourcemapsOptions: CapturedSourcemapsOptions | undefined;
Expand Down Expand Up @@ -137,8 +137,7 @@ describe('sentryAstro integration', () => {
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['out/**/*', 'out/**/.prerender/**/*'],
deleteSourcemapsAfterUpload: expect.any(Function),
sdkEnabledSourceMaps: expect.any(Function),
sdkDeleteSourceMaps: expect.any(Function),
},
_metaOptions: {
telemetry: {
Expand Down Expand Up @@ -169,8 +168,7 @@ describe('sentryAstro integration', () => {
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['dist/**/*', 'dist/**/.prerender/**/*'],
deleteSourcemapsAfterUpload: expect.any(Function),
sdkEnabledSourceMaps: expect.any(Function),
sdkDeleteSourceMaps: expect.any(Function),
},
_metaOptions: {
telemetry: {
Expand Down Expand Up @@ -209,8 +207,7 @@ describe('sentryAstro integration', () => {
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['{.vercel,dist}/**/*', '{.vercel,dist}/**/.prerender/**/*'],
deleteSourcemapsAfterUpload: expect.any(Function),
sdkEnabledSourceMaps: expect.any(Function),
sdkDeleteSourceMaps: expect.any(Function),
},
_metaOptions: {
telemetry: {
Expand Down Expand Up @@ -249,8 +246,7 @@ describe('sentryAstro integration', () => {
bundleSizeOptimizations: {},
sourcemaps: {
assets: ['dist/server/**/*, dist/client/**/*'],
deleteSourcemapsAfterUpload: expect.any(Function),
sdkEnabledSourceMaps: expect.any(Function),
sdkDeleteSourceMaps: expect.any(Function),
},
_metaOptions: {
telemetry: {
Expand Down Expand Up @@ -283,7 +279,7 @@ describe('sentryAstro integration', () => {
expect.objectContaining({
sourcemaps: expect.objectContaining({
filesToDeleteAfterUpload: ['./custom/path/**/*'],
deleteSourcemapsAfterUpload: undefined,
sdkDeleteSourceMaps: undefined,
}),
}),
);
Expand All @@ -301,15 +297,15 @@ describe('sentryAstro integration', () => {
});
}

function expectSourcemapsOptions({ sdkEnabledSourceMaps, ...sourcemaps }: Record<string, unknown>): void {
function expectSourcemapsOptions({ sdkDeleteSourceMaps, ...sourcemaps }: Record<string, unknown>): void {
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(1);
expect(sentryVitePluginSpy).toHaveBeenCalledWith(
expect.objectContaining({ sourcemaps: expect.objectContaining(sourcemaps) }),
);
expect(capturedSourcemapsOptions?.sdkEnabledSourceMaps?.()).toBe(sdkEnabledSourceMaps);
expect(capturedSourcemapsOptions?.sdkDeleteSourceMaps?.()).toBe(sdkDeleteSourceMaps);
}

// The bundler plugin calls the function form in its `config` hook, after the build state
// The bundler plugin calls `sdkDeleteSourceMaps` in its `config` hook, after the build state
// plugin ran.
function runBuildStatePlugin(viteConfig: { build?: { outDir?: string } }): void {
const plugin = updateConfig.mock.calls
Expand All @@ -318,10 +314,11 @@ describe('sentryAstro integration', () => {
plugin.config(viteConfig);
}

// Like the bundler plugin, the user's `deleteSourcemapsAfterUpload` wins over the SDK's value.
function expectDeleteOption(value: boolean | undefined): void {
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(1);
const option = capturedSourcemapsOptions?.deleteSourcemapsAfterUpload;
expect(typeof option === 'function' ? option() : option).toBe(value);
const { deleteSourcemapsAfterUpload, sdkDeleteSourceMaps } = capturedSourcemapsOptions ?? {};
expect(deleteSourcemapsAfterUpload ?? sdkDeleteSourceMaps?.()).toBe(value);
}

it('is `true` when the SDK enabled the source maps', async () => {
Expand All @@ -343,10 +340,11 @@ describe('sentryAstro integration', () => {
},
);

it('lets a user-set `true` remove the maps of a Cloudflare `_worker.js` build', async () => {
it('passes a user-set `true` through, but asks for no SDK deletion of a Cloudflare `_worker.js` build', async () => {
await runSetup({ sourcemaps: { deleteSourcemapsAfterUpload: true } });
runBuildStatePlugin({ build: { outDir: 'dist/_worker.js/' } });
expectDeleteOption(true);
expect(capturedSourcemapsOptions?.deleteSourcemapsAfterUpload).toBe(true);
expect(capturedSourcemapsOptions?.sdkDeleteSourceMaps?.()).toBeUndefined();
});

it.each(['hidden', true] as const)('is not set when the user set `vite.build.sourcemap: %s`', async sourcemap => {
Expand All @@ -369,23 +367,23 @@ describe('sentryAstro integration', () => {
expectDeleteOption(undefined);
});

it('lets a user-set `true` win over the user glob', async () => {
it('passes a user-set `true` through, but asks for no SDK deletion with a user glob', async () => {
await runSetup({
sourcemaps: { deleteSourcemapsAfterUpload: true, filesToDeleteAfterUpload: ['./dist/**/*.map'] },
});
expectDeleteOption(true);
expectSourcemapsOptions({ deleteSourcemapsAfterUpload: true, sdkDeleteSourceMaps: undefined });
expect(capturedSourcemapsOptions?.sdkDeleteSourceMaps).toBeUndefined();
});

// Then the plugin removes all of these maps, also those of chunks outside the user's
// `assets` or `ignore`.
it('marks the source maps as SDK-enabled when the user narrowed the upload', async () => {
it('asks for SDK deletion when the user narrowed the upload', async () => {
await runSetup({ sourcemaps: { assets: ['./dist/client/**/*'], ignore: ['**/vendor/**'] } });
expectSourcemapsOptions({
assets: ['./dist/client/**/*'],
ignore: ['**/vendor/**'],
sdkEnabledSourceMaps: true,
sdkDeleteSourceMaps: true,
});
expectDeleteOption(true);
});

// The output setting overrides `vite.build.sourcemap`, so these maps are the user's.
Expand All @@ -396,44 +394,37 @@ describe('sentryAstro integration', () => {
] as const)('leaves the source maps to the user with the output options %j', async build => {
await runSetup({}, { vite: { build } } as Partial<AstroConfig>);
expectViteSourceMapSetting(undefined);
expectSourcemapsOptions({ sdkEnabledSourceMaps: false });
expectDeleteOption(undefined);
expectSourcemapsOptions({ sdkDeleteSourceMaps: undefined });
});

// Astro copies the top-level setting into the `client` environment.
it('marks the source maps of the `client` environment as SDK-enabled', async () => {
it('asks for SDK deletion of the `client` environment', async () => {
await runSetup({});
runBuildStatePlugin({ build: { outDir: 'dist/' } });
expect(capturedSourcemapsOptions?.sdkEnabledSourceMaps?.('client')).toBe(true);
const option = capturedSourcemapsOptions?.deleteSourcemapsAfterUpload;
expect(typeof option === 'function' ? option('client') : option).toBe(true);
expect(capturedSourcemapsOptions?.sdkDeleteSourceMaps?.('client')).toBe(true);
});

it('does not mark the source maps of an environment the user set as SDK-enabled', async () => {
it('asks for no SDK deletion of an environment the user set', async () => {
await runSetup({}, {
vite: { environments: { client: { build: { sourcemap: true } } } },
} as Partial<AstroConfig>);
runBuildStatePlugin({ build: { outDir: 'dist/' } });
expect(capturedSourcemapsOptions?.sdkEnabledSourceMaps?.('client')).toBe(false);
const option = capturedSourcemapsOptions?.deleteSourcemapsAfterUpload;
expect(typeof option === 'function' ? option('client') : option).toBeUndefined();
expectSourcemapsOptions({ sdkEnabledSourceMaps: true });
expectDeleteOption(true);
expect(capturedSourcemapsOptions?.sdkDeleteSourceMaps?.('client')).toBeUndefined();
expectSourcemapsOptions({ sdkDeleteSourceMaps: true });
});

it.each(['hidden', true, false] as const)(
'does not mark source maps as SDK-enabled when the user set `vite.build.sourcemap: %s`',
'asks for no SDK deletion when the user set `vite.build.sourcemap: %s`',
async sourcemap => {
await runSetup({}, { vite: { build: { sourcemap } } });
expectSourcemapsOptions({ sdkEnabledSourceMaps: false });
expectSourcemapsOptions({ sdkDeleteSourceMaps: undefined });
},
);

// The maps are removed in this mode too, after the plugin stamps the debug IDs.
it("is `true` with `disable: 'disable-upload'`", async () => {
await runSetup({ sourcemaps: { disable: 'disable-upload' } });
expectSourcemapsOptions({ disable: 'disable-upload', sdkEnabledSourceMaps: true });
expectDeleteOption(true);
expectSourcemapsOptions({ disable: 'disable-upload', sdkDeleteSourceMaps: true });
});

function expectViteSourceMapSetting(sourcemap: 'hidden' | undefined): void {
Expand All @@ -449,8 +440,7 @@ describe('sentryAstro integration', () => {
'adds the plugin, turns no source maps on and asks for no deletion with %j',
async sourcemaps => {
await runSetup({ sourcemaps });
expectSourcemapsOptions({ ...sourcemaps, sdkEnabledSourceMaps: false });
expectDeleteOption(undefined);
expectSourcemapsOptions({ ...sourcemaps, sdkDeleteSourceMaps: undefined });
expectViteSourceMapSetting(undefined);
},
);
Expand All @@ -466,15 +456,14 @@ describe('sentryAstro integration', () => {
[{ delete: false, deleteSourcemapsAfterUpload: true }, 'hidden', true],
] as const)('with %j it sets the source map setting to %s', async (sourcemaps, setting, deleteAfterUpload) => {
await runSetup({ sourcemaps });
expectSourcemapsOptions({ ...sourcemaps, sdkEnabledSourceMaps: setting === 'hidden' });
expectSourcemapsOptions({ ...sourcemaps, sdkDeleteSourceMaps: undefined });
expect(capturedSourcemapsOptions?.deleteSourcemapsAfterUpload).toBe(deleteAfterUpload);
expectViteSourceMapSetting(setting);
});

it('enables hidden source maps when `upload: true` replaces `disable: true`', async () => {
await runSetup({ sourcemaps: { disable: true, upload: true } });
expectSourcemapsOptions({ sdkEnabledSourceMaps: true });
expectDeleteOption(true);
expectSourcemapsOptions({ sdkDeleteSourceMaps: true });
expectViteSourceMapSetting('hidden');
});
});
Expand Down
3 changes: 1 addition & 2 deletions packages/bundler-plugins/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ export { createDebugIdUploadFunction, addDebugIdToEmittedArtifacts, stampDebugId
export {
decideSourceMapHandling,
getDeleteOption,
getDeleteSourcemapsAfterUpload,
getSdkEnabledSourceMaps,
getSdkDeleteSourceMaps,
getSourceMapFeatures,
} from './sourcemap-settings';
export type { SourceMapDecision, SourceMapKind } from './sourcemap-settings';
Expand Down
4 changes: 2 additions & 2 deletions packages/bundler-plugins/src/core/options-mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ export type NormalizedOptions = {
rewriteSources?: RewriteSourcesHook;
resolveSourceMap?: ResolveSourceMapHook;
filesToDeleteAfterUpload?: string | string[] | Promise<string | string[] | undefined>;
deleteSourcemapsAfterUpload?: boolean | ((environment?: string) => boolean | undefined);
sdkEnabledSourceMaps?: boolean | ((environment?: string) => boolean | undefined);
deleteSourcemapsAfterUpload?: boolean;
sdkDeleteSourceMaps?: boolean | ((environment?: string) => boolean | undefined);
}
| undefined;
release: {
Expand Down
6 changes: 1 addition & 5 deletions packages/bundler-plugins/src/core/sentry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ export function setTelemetryDataOnScope(
scope.setTag('delete-after-upload', !!sourcemaps?.filesToDeleteAfterUpload);
scope.setTag(
'delete-sourcemaps-after-upload',
typeof sourcemaps?.deleteSourcemapsAfterUpload === 'function'
? 'function'
: sourcemaps?.deleteSourcemapsAfterUpload === undefined
? 'auto'
: String(sourcemaps.deleteSourcemapsAfterUpload),
sourcemaps?.deleteSourcemapsAfterUpload === undefined ? 'auto' : String(sourcemaps.deleteSourcemapsAfterUpload),
);
scope.setTag('sourcemaps-disabled', !!sourcemaps?.disable);
scope.setTag('sourcemaps-upload', sourcemaps?.upload === undefined ? 'auto' : String(sourcemaps.upload));
Expand Down
Loading
Loading