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 @@ -10,8 +10,12 @@ export interface ConflictingSourceMapOptions {
filesToDeleteAfterUpload?: unknown;
}

// An SDK and each of its plugin instances call this for the same options.
const loggedWarnings = new Set<string>();

/**
* Warns about `sourcemaps` options that conflict with each other, once per conflict.
* Warns about `sourcemaps` options that conflict with each other. It logs
* each warning once per process.
*
* The bundler plugin calls this for the options it gets. SDKs that apply some of the options
* themselves call it with the user's options, because the plugin does not see them all.
Expand All @@ -35,25 +39,32 @@ export function warnOnConflictingSourceMapOptions(
return;
}

const warn = (message: string): void => {
if (!loggedWarnings.has(message)) {
loggedWarnings.add(message);
logWarning(message);
}
};

const usesFeatureOptions = sourcemaps.upload !== undefined || sourcemaps.injectDebugIds !== undefined;
// TODO(v12): Remove along with `sourcemaps.disable`.
if (usesFeatureOptions && sourcemaps.disable !== undefined) {
logWarning(
warn(
'`sourcemaps.disable` is ignored because `sourcemaps.upload` or `sourcemaps.injectDebugIds` is set. Please remove `sourcemaps.disable`.',
);
}
if (sourcemaps.injectDebugIds === false && sourcemaps.upload === true) {
logWarning(
warn(
'`sourcemaps.upload` is set, but `sourcemaps.injectDebugIds` is `false`. Source maps are not uploaded, because the upload matches files by debug ID.',
);
}
if (sourcemaps.delete !== undefined && sourcemaps.deleteSourcemapsAfterUpload !== undefined) {
logWarning(
warn(
'`sourcemaps.deleteSourcemapsAfterUpload` is ignored because `sourcemaps.delete` is set. Please remove `sourcemaps.deleteSourcemapsAfterUpload`.',
);
}
if (sourcemaps.delete === false && sourcemaps.filesToDeleteAfterUpload !== undefined) {
logWarning(
warn(
'`sourcemaps.filesToDeleteAfterUpload` still deletes the files it matches, although `sourcemaps.delete` is `false`. Remove `sourcemaps.filesToDeleteAfterUpload` to keep the source maps.',
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { warnOnConflictingSourceMapOptions } from '../../../src/build-time-plugins/warnOnConflictingSourceMapOptions';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { warnOnConflictingSourceMapOptions as WarnOnConflictingSourceMapOptions } from '../../../src/build-time-plugins/warnOnConflictingSourceMapOptions';

describe('warnOnConflictingSourceMapOptions', () => {
let warnOnConflictingSourceMapOptions: typeof WarnOnConflictingSourceMapOptions;

// The module remembers the warnings it logged.
beforeEach(async () => {
vi.resetModules();
({ warnOnConflictingSourceMapOptions } =
await import('../../../src/build-time-plugins/warnOnConflictingSourceMapOptions'));
});

afterEach(() => {
vi.restoreAllMocks();
});
Expand Down Expand Up @@ -51,6 +60,17 @@ describe('warnOnConflictingSourceMapOptions', () => {
expect(logWarning).toHaveBeenCalledTimes(3);
});

it('logs each warning once, also for later calls', () => {
const logWarning = vi.fn();

warnOnConflictingSourceMapOptions({ upload: true, injectDebugIds: false }, logWarning);
warnOnConflictingSourceMapOptions({ upload: true, injectDebugIds: false, disable: true }, logWarning);

expect(logWarning).toHaveBeenCalledTimes(2);
expect(logWarning).toHaveBeenNthCalledWith(1, expect.stringMatching(/`sourcemaps\.injectDebugIds` is `false`/));
expect(logWarning).toHaveBeenNthCalledWith(2, expect.stringMatching(/`sourcemaps\.disable` is ignored/));
});

it('logs with a `[Sentry]` prefix by default', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);

Expand Down
18 changes: 17 additions & 1 deletion packages/gatsby/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
const fs = require('fs');

const { sentryWebpackPlugin } = require('@sentry/bundler-plugins/webpack');
const { resolveSourceMapsFeatures } = require('@sentry/core');
const { consoleSandbox, resolveSourceMapsFeatures } = require('@sentry/core');

const SENTRY_USER_CONFIG = ['./sentry.config.js', './sentry.config.ts'];

// Gatsby calls `onCreateWebpackConfig` once per build stage.
let warnedAboutDeleteOptions = false;

exports.onCreateWebpackConfig = ({ getConfig, actions, stage }, options) => {
const enableClientWebpackPlugin = options.enableClientWebpackPlugin !== false;
if (process.env.NODE_ENV === 'production' && enableClientWebpackPlugin) {
const prevSourceMapSetting = getConfig() && 'devtool' in getConfig() ? getConfig().devtool : undefined;
const { injectDebugIds, upload } = resolveSourceMapsFeatures(options.sourcemaps);
// `sourcemaps.delete` replaces the flat `deleteSourcemapsAfterUpload`.
const deleteOption = options.sourcemaps?.delete ?? options.deleteSourcemapsAfterUpload;
if (
options.sourcemaps?.delete !== undefined &&
options.deleteSourcemapsAfterUpload !== undefined &&
!warnedAboutDeleteOptions
) {
warnedAboutDeleteOptions = true;
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
'[Sentry] `deleteSourcemapsAfterUpload` is ignored because `sourcemaps.delete` is set. Please remove `deleteSourcemapsAfterUpload`.',
);
});
}
// Sentry turns source maps on to upload them, or when `sourcemaps.delete: false` keeps them for a manual upload.
const shouldAutomaticallyEnableSourceMaps =
prevSourceMapSetting !== 'source-map' &&
Expand Down
16 changes: 16 additions & 0 deletions packages/gatsby/test/gatsby-node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ describe('onCreateWebpackConfig', () => {
logSpy.mockRestore();
});

it('warns once when `sourcemaps.delete` and the flat option are both set', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const getConfig = vi.fn().mockReturnValue({ devtool: 'source-map' });
const options = { deleteSourcemapsAfterUpload: true, sourcemaps: { delete: false } };

for (const stage of ['build-javascript', 'build-html']) {
onCreateWebpackConfig({ actions: { setWebpackConfig: vi.fn() }, getConfig, stage }, options);
}

expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('`deleteSourcemapsAfterUpload` is ignored because `sourcemaps.delete` is set'),
);
warnSpy.mockRestore();
});

it.each([
[{ delete: false }, { deleteSourcemapsAfterUpload: true }, undefined],
[{ delete: true }, { deleteSourcemapsAfterUpload: false }, true],
Expand Down
5 changes: 4 additions & 1 deletion packages/nitro/src/sourceMaps.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import type { Options as BundlerPluginOptions } from '@sentry/bundler-plugins/core';
import { createSentryBuildPluginManager } from '@sentry/bundler-plugins/core';
import { resolveSourceMapsFeatures } from '@sentry/core';
import { resolveSourceMapsFeatures, warnOnConflictingSourceMapOptions } from '@sentry/core';
import type { Nitro, NitroConfig } from 'nitro/types';
import type { SentryNitroOptions } from './config';

/**
* Registers a `compiled` hook to upload source maps after the build completes.
*/
export function setupSourceMaps(nitro: Nitro, options?: SentryNitroOptions, sentryEnabledSourcemaps?: boolean): void {
// The plugin manager does not get `sourcemaps.delete`.
warnOnConflictingSourceMapOptions(options?.sourcemaps);

// The `compiled` hook fires on EVERY rebuild during `nitro dev` watch mode.
// nitro.options.dev is reliably set by the time module setup runs.
if (shouldSkipBuildHook(nitro, options)) {
Expand Down
16 changes: 16 additions & 0 deletions packages/nitro/test/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,22 @@ describe('setupSentryNitroModule', () => {
});

describe('setupSourceMaps', () => {
// The plugin manager does not get `sourcemaps.delete`.
it('warns about conflicting source map options', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const nitro = {
options: { dev: true, output: { serverDir: '/output/server' } },
hooks: { hook: vi.fn() },
} as any;

setupSourceMaps(nitro, { sourcemaps: { delete: false, filesToDeleteAfterUpload: ['.output/**/*.map'] } });

expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('`sourcemaps.filesToDeleteAfterUpload` still deletes the files'),
);
warnSpy.mockRestore();
});

it('does not register hook in dev mode', () => {
const hookFn = vi.fn();
const nitro = {
Expand Down
4 changes: 3 additions & 1 deletion packages/nuxt/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { setupOrchestrion } from './vite/orchestrion';
import { NUXT_RELEASE_RUNTIME_CONFIG_KEY, setupSourceMaps } from './vite/sourceMaps';
import { findDefaultSdkInitFile, resolveServerApi } from './vite/utils';
import { setupViteServer } from './vite/viteServer';
import { stringifyForCodeInjection } from '@sentry/core';
import { stringifyForCodeInjection, warnOnConflictingSourceMapOptions } from '@sentry/core';

export type ModuleOptions = SentryNuxtModuleOptions;
type NuxtPageSubset = { file?: string; path: string };
Expand Down Expand Up @@ -112,6 +112,8 @@ export default defineNuxtModule<ModuleOptions>({
}

if (clientConfigFile || serverConfigFile) {
// The bundler plugins do not get `sourcemaps.delete`.
warnOnConflictingSourceMapOptions(moduleOptions.sourcemaps);
setupSourceMaps(moduleOptions, nuxt, addVitePlugin, !!clientConfigFile, serverApi === 'nuxt');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { sentryVitePlugin } from '@sentry/bundler-plugins/vite';
import { warnOnRemovedBuildOptions } from '@sentry/core';
import { warnOnConflictingSourceMapOptions, warnOnRemovedBuildOptions } from '@sentry/core';
import { type Plugin } from 'vite';
import type { SentryReactRouterBuildOptions } from './types';

Expand All @@ -8,6 +8,8 @@ import type { SentryReactRouterBuildOptions } from './types';
*/
export async function makeCustomSentryVitePlugins(options: SentryReactRouterBuildOptions): Promise<Plugin[]> {
warnOnRemovedBuildOptions(options, ['unstable_sentryVitePluginOptions']);
// The Vite plugin does not get the user's source map options.
warnOnConflictingSourceMapOptions(options.sourcemaps);

const {
debug,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ describe('makeCustomSentryVitePlugins', () => {

consoleWarnSpy.mockRestore();
});

// The Vite plugin gets none of the user's source map options, so it cannot warn itself.
it('warns about conflicting source map options', async () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => undefined);

await makeCustomSentryVitePlugins({ sourcemaps: { delete: false, filesToDeleteAfterUpload: ['build/**/*.map'] } });

expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('`sourcemaps.filesToDeleteAfterUpload` still deletes the files'),
);

consoleWarnSpy.mockRestore();
});
});
9 changes: 8 additions & 1 deletion packages/sveltekit/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { escapeStringForRegex, resolveSourceMapsFeatures, uuid4 } from '@sentry/core';
import {
escapeStringForRegex,
resolveSourceMapsFeatures,
uuid4,
warnOnConflictingSourceMapOptions,
} from '@sentry/core';
import { getSentryRelease } from '@sentry/node';
import { addDebugIdToEmittedArtifacts } from '@sentry/bundler-plugins/core';
import type { SentryVitePluginOptions } from '@sentry/bundler-plugins/vite';
Expand Down Expand Up @@ -52,6 +57,8 @@ export async function makeCustomSentryVitePlugins(
},
): Promise<Plugin[]> {
const { getAdapterOutputDir, getKitConfig } = deps;
// The plugin does not get `sourcemaps.delete`.
warnOnConflictingSourceMapOptions(options?.sourcemaps);

const defaultPluginOptions: SentryVitePluginOptions = {
release: {
Expand Down
14 changes: 14 additions & 0 deletions packages/sveltekit/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ describe('makeCustomSentryVitePlugins()', () => {
});
});

// The bundler plugin does not get `sourcemaps.delete`.
it('warns about conflicting source map options', async () => {
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});

await makeCustomSentryVitePlugins(
{ sourcemaps: { delete: false, filesToDeleteAfterUpload: ['./build/**/*.map'] } },
{ getAdapterOutputDir: async () => '.svelte-kit/output', getKitConfig: async () => ({}) },
);

expect(consoleWarnSpy).toHaveBeenCalledWith(
expect.stringContaining('`sourcemaps.filesToDeleteAfterUpload` still deletes the files'),
);
});

it('catches errors while uploading source maps', async () => {
mockedMainPlugin.writeBundle.mockImplementationOnce(() => {
throw new Error('test error');
Expand Down
Loading