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
29 changes: 2 additions & 27 deletions packages/bundler-plugins/src/core/options-mapping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getVercelEnv } from '@sentry/core';
import { getVercelEnv, warnOnConflictingSourceMapOptions } from '@sentry/core';
import type { Logger } from './logger';
import type {
InternalOptions as UserOptions,
Expand Down Expand Up @@ -218,32 +218,7 @@ export function validateOptions(options: NormalizedOptions, logger: Logger): boo
}
}

const sourcemaps = options.sourcemaps;
const usesFeatureOptions = sourcemaps?.upload !== undefined || sourcemaps?.injectDebugIds !== undefined;
if (usesFeatureOptions && sourcemaps?.disable !== undefined) {
logger.warn(
'`sourcemaps.disable` is ignored because `sourcemaps.upload` or `sourcemaps.injectDebugIds` is set.',
'Please remove `sourcemaps.disable`.',
);
}
if (sourcemaps?.injectDebugIds === false && sourcemaps.upload === true) {
logger.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) {
logger.warn(
'`sourcemaps.deleteSourcemapsAfterUpload` is ignored because `sourcemaps.delete` is set.',
'Please remove `sourcemaps.deleteSourcemapsAfterUpload`.',
);
}
if (sourcemaps?.delete === false && sourcemaps.filesToDeleteAfterUpload !== undefined) {
logger.warn(
'`sourcemaps.filesToDeleteAfterUpload` still deletes the files it matches, although `sourcemaps.delete` is `false`.',
'Remove `sourcemaps.filesToDeleteAfterUpload` to keep the source maps.',
);
}
warnOnConflictingSourceMapOptions(options.sourcemaps, message => logger.warn(message));

if (options.release?.deploy && typeof options.release.deploy === 'object' && !options.release.deploy.env) {
logger.error(
Expand Down
28 changes: 3 additions & 25 deletions packages/bundler-plugins/test/core/option-mappings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,34 +448,12 @@ describe('validateOptions', () => {
});

describe('source map options', () => {
it.each([
[{ upload: false, disable: true }, /`sourcemaps\.disable` is ignored/],
[{ injectDebugIds: false, disable: 'disable-upload' }, /`sourcemaps\.disable` is ignored/],
[{ upload: true, injectDebugIds: false }, /`sourcemaps\.injectDebugIds` is `false`/],
[{ delete: true, deleteSourcemapsAfterUpload: false }, /`sourcemaps\.deleteSourcemapsAfterUpload` is ignored/],
[{ delete: false, filesToDeleteAfterUpload: ['dist/**/*.map'] }, /`sourcemaps\.filesToDeleteAfterUpload` still/],
] as const)('warns about %j', (sourcemaps, message) => {
const options = { sourcemaps } as Partial<NormalizedOptions>;
it('warns about conflicting options through the plugin logger', () => {
const options = { sourcemaps: { upload: true, injectDebugIds: false } } as Partial<NormalizedOptions>;

expect(validateOptions(options as unknown as NormalizedOptions, mockedLogger)).toBe(true);
expect(mockedLogger.warn).toHaveBeenCalledTimes(1);
expect(mockedLogger.warn).toHaveBeenCalledWith(expect.stringMatching(message), expect.any(String));
});

it.each([
{ upload: false },
{ injectDebugIds: false },
{ upload: false, injectDebugIds: false },
{ delete: false },
{ disable: true, deleteSourcemapsAfterUpload: true },
{ disable: true, delete: true },
{ delete: true, filesToDeleteAfterUpload: ['extra/**'] },
{ filesToDeleteAfterUpload: ['dist/**/*.map'] },
] as const)('does not warn about %j', sourcemaps => {
const options = { sourcemaps } as Partial<NormalizedOptions>;

expect(validateOptions(options as unknown as NormalizedOptions, mockedLogger)).toBe(true);
expect(mockedLogger.warn).not.toHaveBeenCalled();
expect(mockedLogger.warn).toHaveBeenCalledWith(expect.stringMatching(/`sourcemaps\.injectDebugIds` is `false`/));
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { consoleSandbox } from '../utils/debug-logger';

/** The source map options that can conflict with each other. */
export interface ConflictingSourceMapOptions {
disable?: boolean | 'disable-upload';
upload?: boolean;
injectDebugIds?: boolean;
delete?: boolean;
deleteSourcemapsAfterUpload?: unknown;
filesToDeleteAfterUpload?: unknown;
}

/**
* Warns about `sourcemaps` options that conflict with each other, once per conflict.
*
* 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.
*
* @param sourcemaps The user's `sourcemaps` options, if any.
* @param logWarning Called with each warning message. Defaults to `console.warn` with a
* `[Sentry]` prefix. Pass a logger that adds its own prefix to route the warnings through it.
*
* @internal Only meant for Sentry-internal SDK usage.
* @hidden
*/
export function warnOnConflictingSourceMapOptions(
sourcemaps: ConflictingSourceMapOptions | undefined,
logWarning: (message: string) => void = message =>
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(`[Sentry] ${message}`);
}),
): void {
if (!sourcemaps) {
return;
}

const usesFeatureOptions = sourcemaps.upload !== undefined || sourcemaps.injectDebugIds !== undefined;
// TODO(v12): Remove along with `sourcemaps.disable`.
if (usesFeatureOptions && sourcemaps.disable !== undefined) {
logWarning(
'`sourcemaps.disable` is ignored because `sourcemaps.upload` or `sourcemaps.injectDebugIds` is set. Please remove `sourcemaps.disable`.',
);
}
if (sourcemaps.injectDebugIds === false && sourcemaps.upload === true) {
logWarning(
'`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(
'`sourcemaps.deleteSourcemapsAfterUpload` is ignored because `sourcemaps.delete` is set. Please remove `sourcemaps.deleteSourcemapsAfterUpload`.',
);
}
if (sourcemaps.delete === false && sourcemaps.filesToDeleteAfterUpload !== undefined) {
logWarning(
'`sourcemaps.filesToDeleteAfterUpload` still deletes the files it matches, although `sourcemaps.delete` is `false`. Remove `sourcemaps.filesToDeleteAfterUpload` to keep the source maps.',
);
}
}
2 changes: 2 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,5 +570,7 @@ export {
export { warnOnRemovedBuildOptions } from './build-time-plugins/warnOnRemovedBuildOptions';
export { resolveSourceMapsFeatures } from './build-time-plugins/resolveSourceMapsFeatures';
export type { SourceMapsFeatures } from './build-time-plugins/resolveSourceMapsFeatures';
export { warnOnConflictingSourceMapOptions } from './build-time-plugins/warnOnConflictingSourceMapOptions';
export type { ConflictingSourceMapOptions } from './build-time-plugins/warnOnConflictingSourceMapOptions';
export { stringifyForCodeInjection } from './build-time-plugins/stringifyForCodeInjection';
export { startSpan, startInactiveSpan, startSpanManual } from './tracing/trace';
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { warnOnConflictingSourceMapOptions } from '../../../src/build-time-plugins/warnOnConflictingSourceMapOptions';

describe('warnOnConflictingSourceMapOptions', () => {
afterEach(() => {
vi.restoreAllMocks();
});

it.each([
[{ upload: false, disable: true }, /`sourcemaps\.disable` is ignored/],
[{ injectDebugIds: false, disable: 'disable-upload' }, /`sourcemaps\.disable` is ignored/],
[{ upload: true, injectDebugIds: false }, /`sourcemaps\.injectDebugIds` is `false`/],
[{ delete: true, deleteSourcemapsAfterUpload: false }, /`sourcemaps\.deleteSourcemapsAfterUpload` is ignored/],
[{ delete: false, filesToDeleteAfterUpload: ['dist/**/*.map'] }, /`sourcemaps\.filesToDeleteAfterUpload` still/],
] as const)('warns once about %j', (sourcemaps, message) => {
const logWarning = vi.fn();

warnOnConflictingSourceMapOptions(sourcemaps, logWarning);

expect(logWarning).toHaveBeenCalledTimes(1);
expect(logWarning).toHaveBeenCalledWith(expect.stringMatching(message));
});

it.each([
undefined,
{},
{ upload: false },
{ injectDebugIds: false },
{ upload: false, injectDebugIds: false },
{ delete: false },
{ disable: true, deleteSourcemapsAfterUpload: true },
{ disable: true, delete: true },
{ delete: true, filesToDeleteAfterUpload: ['extra/**'] },
{ filesToDeleteAfterUpload: ['dist/**/*.map'] },
] as const)('does not warn about %j', sourcemaps => {
const logWarning = vi.fn();

warnOnConflictingSourceMapOptions(sourcemaps, logWarning);

expect(logWarning).not.toHaveBeenCalled();
});

it('warns about each conflict', () => {
const logWarning = vi.fn();

warnOnConflictingSourceMapOptions(
{ disable: true, upload: true, injectDebugIds: false, delete: false, filesToDeleteAfterUpload: 'x' },
logWarning,
);

expect(logWarning).toHaveBeenCalledTimes(3);
});

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

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

expect(warnSpy).toHaveBeenCalledWith(expect.stringMatching(/^\[Sentry\] `sourcemaps\.upload` is set/));
});
});
Loading