From a3903df5d3dd63acb50041d6d189b74840296aea Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Thu, 2 Jan 2025 14:52:40 +0100 Subject: [PATCH 1/9] feat(sveltekit): Enable hidden source maps or keep settings --- packages/sveltekit/src/vite/sourceMaps.ts | 108 +++++++++++++++--- .../sveltekit/test/vite/sourceMaps.test.ts | 67 ++++++++++- 2 files changed, 156 insertions(+), 19 deletions(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index a59e9af19260..9fa0595facbf 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -1,11 +1,11 @@ import * as child_process from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; -import { escapeStringForRegex, uuid4 } from '@sentry/core'; +import { consoleSandbox, escapeStringForRegex, uuid4 } from '@sentry/core'; import { getSentryRelease } from '@sentry/node'; import type { SentryVitePluginOptions } from '@sentry/vite-plugin'; import { sentryVitePlugin } from '@sentry/vite-plugin'; -import type { Plugin } from 'vite'; +import type { Plugin, UserConfig } from 'vite'; import MagicString from 'magic-string'; import { WRAPPED_MODULE_SUFFIX } from './autoInstrument'; @@ -135,26 +135,18 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter // Modify the config to generate source maps - config: config => { - const sourceMapsPreviouslyNotEnabled = !config.build?.sourcemap; - if (debug && sourceMapsPreviouslyNotEnabled) { - // eslint-disable-next-line no-console - console.log('[Source Maps Plugin] Enabling source map generation'); - if (!mergedOptions.sourcemaps?.filesToDeleteAfterUpload) { + config: (config: UserConfig) => { + changeViteSourceMapSettings(config, options); + + if (debug && !mergedOptions.sourcemaps?.filesToDeleteAfterUpload) { + consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( - `[Source Maps Plugin] We recommend setting the \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload\` option to clean up source maps after uploading. -[Source Maps Plugin] Otherwise, source maps might be deployed to production, depending on your configuration`, + '[Source Maps Plugin] We recommend setting the `sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload` option to clean up source maps after uploading. Otherwise, source maps might be deployed to production, depending on your configuration', ); - } + }); } - return { - ...config, - build: { - ...config.build, - sourcemap: true, - }, - }; + return config; }, resolveId: (id, _importer, _ref) => { @@ -332,6 +324,86 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug ]; } +/** + * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps + */ +export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; + +/** There are 3 ways to set up source maps (https://github.com/getsentry/sentry-javascript/issues/13993) + * + * 1. User explicitly disabled source maps + * - keep this setting (emit a warning that errors won't be unminified in Sentry) + * - We won't upload anything + * + * 2. Users enabled source map generation (true, 'hidden', 'inline'). + * - keep this setting (don't do anything - like deletion - besides uploading) + * + * 3. Users didn't set source maps generation + * - we enable 'hidden' source maps generation + * - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) + * + * --> only exported for testing + */ +export function changeViteSourceMapSettings( + viteConfig: { + build?: { + sourcemap?: boolean | 'inline' | 'hidden'; + }; + }, + sentryPluginOptions?: CustomSentryVitePluginOptions, +): UserSourceMapSetting { + let previousUserSourceMapSetting: UserSourceMapSetting = undefined; + + viteConfig.build = viteConfig.build || {}; + + const viteSourceMap = viteConfig.build.sourcemap; + + if (viteSourceMap === false) { + warnExplicitlyDisabledSourceMap('vite.build.sourcemap'); + previousUserSourceMapSetting = 'disabled'; + } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { + logKeepSourceMapSetting('vite.build.sourcemap', viteSourceMap.toString(), sentryPluginOptions); + previousUserSourceMapSetting = 'enabled'; + } else { + viteConfig.build.sourcemap = 'hidden'; + logSentryEnablesSourceMap('vite.build.sourcemap', 'hidden'); + previousUserSourceMapSetting = 'unset'; + } + + return previousUserSourceMapSetting; +} + +function logKeepSourceMapSetting( + settingKey: string, + settingValue: string, + sentryPluginOptions?: CustomSentryVitePluginOptions, +): void { + if (sentryPluginOptions?.debug) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log( + `[Sentry] We discovered \`${settingKey}\` is set to \`${settingValue}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, + ); + }); + } +} + +function warnExplicitlyDisabledSourceMap(settingKey: string): void { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn( + `[Sentry] Parts of source map generation are currently disabled in your Nuxt configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, + ); + }); +} + +function logSentryEnablesSourceMap(settingKey: string, settingValue: string): void { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: ${settingValue}\`.`); + }); +} + function getFiles(dir: string): string[] { if (!fs.existsSync(dir)) { return []; diff --git a/packages/sveltekit/test/vite/sourceMaps.test.ts b/packages/sveltekit/test/vite/sourceMaps.test.ts index 9837067ec643..727f7ebac21a 100644 --- a/packages/sveltekit/test/vite/sourceMaps.test.ts +++ b/packages/sveltekit/test/vite/sourceMaps.test.ts @@ -1,4 +1,6 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { changeViteSourceMapSettings } from '../../src/vite/sourceMaps'; +import type { CustomSentryVitePluginOptions } from '../../src/vite/types'; import type { Plugin } from 'vite'; import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; @@ -75,10 +77,26 @@ describe('makeCustomSentryVitePlugin()', () => { }); describe('Custom debug id source maps plugin plugin', () => { - it('enables source map generation', async () => { + it('enables source map generation when unset', async () => { const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin'); // @ts-expect-error this function exists! const sentrifiedConfig = plugin.config({ build: { foo: {} }, test: {} }); + expect(sentrifiedConfig).toEqual({ + build: { + foo: {}, + sourcemap: 'hidden', + }, + test: {}, + }); + }); + + it('keeps source map generation settings', async () => { + const plugin = await getCustomSentryViteUploadSourcemapsPlugin(); + // @ts-expect-error this function exists! + const sentrifiedConfig = plugin.config({ + build: { sourcemap: true, foo: {} }, + test: {}, + }); expect(sentrifiedConfig).toEqual({ build: { foo: {}, @@ -237,3 +255,50 @@ describe('makeCustomSentryVitePlugin()', () => { }); }); }); + +describe('changeViteSourceMapSettings()', () => { + let viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }; + let sentryModuleOptions: CustomSentryVitePluginOptions; + + beforeEach(() => { + viteConfig = {}; + sentryModuleOptions = {}; + }); + + it('handles vite source map settings', () => { + const cases = [ + { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { sourcemap: 'inline', expectedSourcemap: 'inline', expectedReturn: 'enabled' }, + { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { sourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { + viteConfig.build = { sourcemap }; + const previousUserSourceMapSetting = changeViteSourceMapSettings(viteConfig, sentryModuleOptions); + expect(viteConfig.build.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourceMapSetting).toBe(expectedReturn); + }); + }); + + it('logs warnings and messages when debug is enabled', () => { + const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); + const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); + + sentryModuleOptions = { debug: true }; + + viteConfig.build = { sourcemap: false }; + changeViteSourceMapSettings(viteConfig, sentryModuleOptions); + expect(consoleWarnSpy).toHaveBeenCalledWith( + expect.stringContaining('Parts of source map generation are currently disabled'), + ); + + viteConfig.build = { sourcemap: 'hidden' }; + changeViteSourceMapSettings(viteConfig, sentryModuleOptions); + expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting')); + + consoleWarnSpy.mockRestore(); + consoleLogSpy.mockRestore(); + }); +}); From 55e977540dc47ceb160213066afffadc2d10b2bc Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Tue, 7 Jan 2025 09:54:49 +0100 Subject: [PATCH 2/9] review suggestions --- packages/sveltekit/src/vite/sourceMaps.ts | 57 +++++++++-------------- 1 file changed, 23 insertions(+), 34 deletions(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index 9fa0595facbf..2315091a671e 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -329,7 +329,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug */ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; -/** There are 3 ways to set up source maps (https://github.com/getsentry/sentry-javascript/issues/13993) +/** There are 3 ways to set up source map generation (https://github.com/getsentry/sentry-javascript/issues/13993) * * 1. User explicitly disabled source maps * - keep this setting (emit a warning that errors won't be unminified in Sentry) @@ -358,50 +358,39 @@ export function changeViteSourceMapSettings( const viteSourceMap = viteConfig.build.sourcemap; + const settingKey = 'vite.build.sourcemap'; + if (viteSourceMap === false) { - warnExplicitlyDisabledSourceMap('vite.build.sourcemap'); previousUserSourceMapSetting = 'disabled'; + + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn( + `[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, + ); + }); } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { - logKeepSourceMapSetting('vite.build.sourcemap', viteSourceMap.toString(), sentryPluginOptions); previousUserSourceMapSetting = 'enabled'; + + if (sentryPluginOptions?.debug) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log( + `[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, + ); + }); + } } else { - viteConfig.build.sourcemap = 'hidden'; - logSentryEnablesSourceMap('vite.build.sourcemap', 'hidden'); previousUserSourceMapSetting = 'unset'; - } - - return previousUserSourceMapSetting; -} + viteConfig.build.sourcemap = 'hidden'; -function logKeepSourceMapSetting( - settingKey: string, - settingValue: string, - sentryPluginOptions?: CustomSentryVitePluginOptions, -): void { - if (sentryPluginOptions?.debug) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log( - `[Sentry] We discovered \`${settingKey}\` is set to \`${settingValue}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, - ); + // eslint-disable-next-line no-console + console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`.`); }); } -} - -function warnExplicitlyDisabledSourceMap(settingKey: string): void { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - `[Sentry] Parts of source map generation are currently disabled in your Nuxt configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, - ); - }); -} -function logSentryEnablesSourceMap(settingKey: string, settingValue: string): void { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: ${settingValue}\`.`); - }); + return previousUserSourceMapSetting; } function getFiles(dir: string): string[] { From afc53f6db15b7d2cb784052ba4e01c513070dcb6 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 13 Jan 2025 14:41:04 +0100 Subject: [PATCH 3/9] updated sourcemap generation settings --- packages/solidstart/src/vite/sourceMaps.ts | 2 +- packages/sveltekit/src/vite/sourceMaps.ts | 143 ++++++++++++------ .../sveltekit/test/vite/sourceMaps.test.ts | 119 ++++++++------- 3 files changed, 162 insertions(+), 102 deletions(-) diff --git a/packages/solidstart/src/vite/sourceMaps.ts b/packages/solidstart/src/vite/sourceMaps.ts index 1228249c193d..285b3949bf93 100644 --- a/packages/solidstart/src/vite/sourceMaps.ts +++ b/packages/solidstart/src/vite/sourceMaps.ts @@ -18,7 +18,7 @@ export function makeAddSentryVitePlugin(options: SentrySolidStartPluginOptions, // Only if source maps were previously not set, we update the "filesToDeleteAfterUpload" (as we override the setting with "hidden") typeof viteConfig.build?.sourcemap === 'undefined' ) { - // This also works for adapters, as the source maps are also copied to e.g. the .vercel folder + // For .output, .vercel, .netlify etc. updatedFilesToDeleteAfterUpload = ['.*/**/*.map']; consoleSandbox(() => { diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index 2315091a671e..bc19b2b771f9 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-lines */ import * as child_process from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; @@ -5,7 +6,7 @@ import { consoleSandbox, escapeStringForRegex, uuid4 } from '@sentry/core'; import { getSentryRelease } from '@sentry/node'; import type { SentryVitePluginOptions } from '@sentry/vite-plugin'; import { sentryVitePlugin } from '@sentry/vite-plugin'; -import type { Plugin, UserConfig } from 'vite'; +import { type Plugin, type UserConfig, loadConfigFromFile } from 'vite'; import MagicString from 'magic-string'; import { WRAPPED_MODULE_SUFFIX } from './autoInstrument'; @@ -27,6 +28,18 @@ type Sorcery = { // and we only want to generate a uuid once in case we have to fall back to it. const releaseName = detectSentryRelease(); +let sourceMapSetting: { + updatedSourceMapSetting?: boolean | 'inline' | 'hidden'; + previousSourceMapSetting?: UserSourceMapSetting; +} = { previousSourceMapSetting: undefined, updatedSourceMapSetting: undefined }; + +/** + * For mocking the value of `sourceMapSetting` in tests + */ +export const __setSourceMapSettingForTest = (value: typeof sourceMapSetting): void => { + sourceMapSetting = value; +}; + /** * Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create * releases and upload source maps to Sentry. @@ -60,6 +73,34 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug }, }; + const filesToDeleteGlob = './.*/**/*.map'; + + if (sourceMapSetting.updatedSourceMapSetting === undefined) { + const configFile = await loadConfigFromFile({ command: 'build', mode: 'production' }); + + if (configFile) { + sourceMapSetting = getUpdatedSourceMapSetting(configFile.config); + } else { + if (options?.debug) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn( + '[Source Maps Plugin] Could not load Vite config with Vite "production" mode. This is needed for Sentry to automatically update source map settings.', + ); + }); + } + } + + if (options?.debug && sourceMapSetting.previousSourceMapSetting === 'unset') { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.warn( + `[Source Maps Plugin] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: ["${filesToDeleteGlob}"]\` to delete generated source maps after they were uploaded to Sentry.`, + ); + }); + } + } + const mergedOptions = { ...defaultPluginOptions, ...options, @@ -67,7 +108,15 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug ...defaultPluginOptions.release, ...options?.release, }, + sourcemaps: { + ...options?.sourcemaps, + filesToDeleteAfterUpload: + sourceMapSetting.previousSourceMapSetting === 'unset' && !options?.sourcemaps?.filesToDeleteAfterUpload + ? ['./.*/**/*.map'] + : options?.sourcemaps?.filesToDeleteAfterUpload, + }, }; + const { debug } = mergedOptions; const sentryPlugins: Plugin[] = await sentryVitePlugin(mergedOptions); @@ -129,26 +178,48 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug __sentry_sveltekit_output_dir: outputDir, }; - const customDebugIdUploadPlugin: Plugin = { - name: 'sentry-sveltekit-debug-id-upload-plugin', + const sourceMapSettingsPlugin: Plugin = { + name: 'sentry-sveltekit-update-source-map-setting-plugin', apply: 'build', // only apply this plugin at build time - enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter - - // Modify the config to generate source maps config: (config: UserConfig) => { - changeViteSourceMapSettings(config, options); + const settingKey = 'build.sourcemap'; - if (debug && !mergedOptions.sourcemaps?.filesToDeleteAfterUpload) { + if (sourceMapSetting.previousSourceMapSetting === 'unset') { consoleSandbox(() => { - // eslint-disable-next-line no-console + // eslint-disable-next-line no-console + console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: "hidden"\`.`); + }); + + return { + ...config, + build: { ...config.build, sourcemap: 'hidden' }, + }; + } else if (sourceMapSetting.previousSourceMapSetting === 'disabled') { + consoleSandbox(() => { + // eslint-disable-next-line no-console console.warn( - '[Source Maps Plugin] We recommend setting the `sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload` option to clean up source maps after uploading. Otherwise, source maps might be deployed to production, depending on your configuration', + `[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, ); }); + } else if (sourceMapSetting.previousSourceMapSetting === 'enabled') { + if (mergedOptions?.debug) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log( + `[Sentry] We discovered you enabled source map generation in your Vite configuration (\`${settingKey}\`). Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, + ); + }); + } } + return config; }, + }; + const customDebugIdUploadPlugin: Plugin = { + name: 'sentry-sveltekit-debug-id-upload-plugin', + apply: 'build', // only apply this plugin at build time + enforce: 'post', // this needs to be set to post, otherwise we don't pick up the output from the SvelteKit adapter resolveId: (id, _importer, _ref) => { if (id === VIRTUAL_GLOBAL_VALUES_FILE) { return { @@ -318,6 +389,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug return [ ...unchangedSentryVitePlugins, + sourceMapSettingsPlugin, customReleaseManagementPlugin, customDebugIdUploadPlugin, customFileDeletionPlugin, @@ -344,53 +416,30 @@ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; * * --> only exported for testing */ -export function changeViteSourceMapSettings( - viteConfig: { - build?: { - sourcemap?: boolean | 'inline' | 'hidden'; - }; - }, - sentryPluginOptions?: CustomSentryVitePluginOptions, -): UserSourceMapSetting { - let previousUserSourceMapSetting: UserSourceMapSetting = undefined; +export function getUpdatedSourceMapSetting(viteConfig: { + build?: { + sourcemap?: boolean | 'inline' | 'hidden'; + }; +}): { updatedSourceMapSetting: boolean | 'inline' | 'hidden'; previousSourceMapSetting: UserSourceMapSetting } { + let previousSourceMapSetting: UserSourceMapSetting; + let updatedSourceMapSetting: boolean | 'inline' | 'hidden' | undefined; viteConfig.build = viteConfig.build || {}; const viteSourceMap = viteConfig.build.sourcemap; - const settingKey = 'vite.build.sourcemap'; - if (viteSourceMap === false) { - previousUserSourceMapSetting = 'disabled'; - - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - `[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, - ); - }); + previousSourceMapSetting = 'disabled'; + updatedSourceMapSetting = viteSourceMap; } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { - previousUserSourceMapSetting = 'enabled'; - - if (sentryPluginOptions?.debug) { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log( - `[Sentry] We discovered \`${settingKey}\` is set to \`${viteSourceMap.toString()}\`. Sentry will keep this source map setting. This will un-minify the code snippet on the Sentry Issue page.`, - ); - }); - } + previousSourceMapSetting = 'enabled'; + updatedSourceMapSetting = viteSourceMap; } else { - previousUserSourceMapSetting = 'unset'; - viteConfig.build.sourcemap = 'hidden'; - - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`.`); - }); + previousSourceMapSetting = 'unset'; + updatedSourceMapSetting = 'hidden'; } - return previousUserSourceMapSetting; + return { previousSourceMapSetting, updatedSourceMapSetting }; } function getFiles(dir: string): string[] { diff --git a/packages/sveltekit/test/vite/sourceMaps.test.ts b/packages/sveltekit/test/vite/sourceMaps.test.ts index 727f7ebac21a..5f7d528516f1 100644 --- a/packages/sveltekit/test/vite/sourceMaps.test.ts +++ b/packages/sveltekit/test/vite/sourceMaps.test.ts @@ -1,6 +1,5 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { changeViteSourceMapSettings } from '../../src/vite/sourceMaps'; -import type { CustomSentryVitePluginOptions } from '../../src/vite/types'; +import { __setSourceMapSettingForTest, getUpdatedSourceMapSetting } from '../../src/vite/sourceMaps'; import type { Plugin } from 'vite'; import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; @@ -57,7 +56,7 @@ async function getSentryViteSubPlugin(name: string): Promise return plugins.find(plugin => plugin.name === name); } -describe('makeCustomSentryVitePlugin()', () => { +describe('makeCustomSentryVitePlugins()', () => { it('returns the custom sentry source maps plugin', async () => { const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin'); @@ -68,7 +67,6 @@ describe('makeCustomSentryVitePlugin()', () => { expect(plugin?.resolveId).toBeInstanceOf(Function); expect(plugin?.transform).toBeInstanceOf(Function); - expect(plugin?.config).toBeInstanceOf(Function); expect(plugin?.configResolved).toBeInstanceOf(Function); // instead of writeBundle, this plugin uses closeBundle @@ -76,36 +74,74 @@ describe('makeCustomSentryVitePlugin()', () => { expect(plugin?.writeBundle).toBeUndefined(); }); - describe('Custom debug id source maps plugin plugin', () => { - it('enables source map generation when unset', async () => { - const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin'); + describe('Custom source map settings update plugin', () => { + it('returns the custom sentry source maps plugin', async () => { + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + + expect(plugin?.name).toEqual('sentry-sveltekit-update-source-map-setting-plugin'); + expect(plugin?.apply).toEqual('build'); + expect(plugin?.config).toBeInstanceOf(Function); + }); + + it('keeps source map generation settings when previously enabled', async () => { + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + + __setSourceMapSettingForTest({ + previousSourceMapSetting: 'enabled', + updatedSourceMapSetting: undefined, + }); + // @ts-expect-error this function exists! - const sentrifiedConfig = plugin.config({ build: { foo: {} }, test: {} }); - expect(sentrifiedConfig).toEqual({ + const sentryConfig = plugin.config({ + build: { sourcemap: true, foo: {} }, + test: {}, + }); + expect(sentryConfig).toEqual({ build: { foo: {}, - sourcemap: 'hidden', + sourcemap: true, }, test: {}, }); }); - it('keeps source map generation settings', async () => { - const plugin = await getCustomSentryViteUploadSourcemapsPlugin(); + it('keeps source map generation settings when previously disabled', async () => { + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + + __setSourceMapSettingForTest({ + previousSourceMapSetting: 'disabled', + updatedSourceMapSetting: undefined, + }); + // @ts-expect-error this function exists! - const sentrifiedConfig = plugin.config({ - build: { sourcemap: true, foo: {} }, + const sentryConfig = plugin.config({ + build: { sourcemap: false, foo: {} }, test: {}, }); - expect(sentrifiedConfig).toEqual({ + expect(sentryConfig).toEqual({ build: { foo: {}, - sourcemap: true, + sourcemap: false, + }, + test: {}, + }); + }); + + it('enables source map generation with "hidden" when unset', async () => { + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + // @ts-expect-error this function exists! + const sentryConfig = plugin.config({ build: { foo: {} }, test: {} }); + expect(sentryConfig).toEqual({ + build: { + foo: {}, + sourcemap: 'hidden', }, test: {}, }); }); + }); + describe('Custom debug id source maps plugin plugin', () => { it('injects the output dir into the server hooks file', async () => { const plugin = await getSentryViteSubPlugin('sentry-sveltekit-debug-id-upload-plugin'); // @ts-expect-error this function exists! @@ -257,48 +293,23 @@ describe('makeCustomSentryVitePlugin()', () => { }); describe('changeViteSourceMapSettings()', () => { - let viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }; - let sentryModuleOptions: CustomSentryVitePluginOptions; - - beforeEach(() => { - viteConfig = {}; - sentryModuleOptions = {}; - }); - it('handles vite source map settings', () => { const cases = [ - { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, - { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, - { sourcemap: 'inline', expectedSourcemap: 'inline', expectedReturn: 'enabled' }, - { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, - { sourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + { sourcemap: false, expectedSourcemap: false, expectedPrevious: 'disabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedPrevious: 'enabled' }, + { sourcemap: 'inline', expectedSourcemap: 'inline', expectedPrevious: 'enabled' }, + { sourcemap: true, expectedSourcemap: true, expectedPrevious: 'enabled' }, + { sourcemap: undefined, expectedSourcemap: 'hidden', expectedPrevious: 'unset' }, ]; - cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { - viteConfig.build = { sourcemap }; - const previousUserSourceMapSetting = changeViteSourceMapSettings(viteConfig, sentryModuleOptions); - expect(viteConfig.build.sourcemap).toBe(expectedSourcemap); - expect(previousUserSourceMapSetting).toBe(expectedReturn); - }); - }); - - it('logs warnings and messages when debug is enabled', () => { - const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); - const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); + cases.forEach(({ sourcemap, expectedSourcemap, expectedPrevious }) => { + const viteConfig = { build: { sourcemap } }; + const result = getUpdatedSourceMapSetting(viteConfig); - sentryModuleOptions = { debug: true }; - - viteConfig.build = { sourcemap: false }; - changeViteSourceMapSettings(viteConfig, sentryModuleOptions); - expect(consoleWarnSpy).toHaveBeenCalledWith( - expect.stringContaining('Parts of source map generation are currently disabled'), - ); - - viteConfig.build = { sourcemap: 'hidden' }; - changeViteSourceMapSettings(viteConfig, sentryModuleOptions); - expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting')); - - consoleWarnSpy.mockRestore(); - consoleLogSpy.mockRestore(); + expect(result).toEqual({ + updatedSourceMapSetting: expectedSourcemap, + previousSourceMapSetting: expectedPrevious, + }); + }); }); }); From 0b141d08ddbb37e815d847c46ec82bd868eaf2c3 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Mon, 13 Jan 2025 16:05:36 +0100 Subject: [PATCH 4/9] fix tests --- packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts b/packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts index f5fa7327fe49..14977f6978d1 100644 --- a/packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts +++ b/packages/sveltekit/test/vite/sentrySvelteKitPlugins.test.ts @@ -43,7 +43,7 @@ describe('sentrySvelteKit()', () => { expect(plugins).toBeInstanceOf(Array); // 1 auto instrument plugin + 5 source maps plugins - expect(plugins).toHaveLength(7); + expect(plugins).toHaveLength(8); }); it('returns the custom sentry source maps upload plugin, unmodified sourcemaps plugins and the auto-instrument plugin by default', async () => { @@ -56,6 +56,7 @@ describe('sentrySvelteKit()', () => { 'sentry-telemetry-plugin', 'sentry-vite-release-injection-plugin', 'sentry-vite-debug-id-injection-plugin', + 'sentry-sveltekit-update-source-map-setting-plugin', // custom release plugin: 'sentry-sveltekit-release-management-plugin', // custom source maps plugin: @@ -86,7 +87,7 @@ describe('sentrySvelteKit()', () => { it("doesn't return the auto instrument plugin if autoInstrument is `false`", async () => { const plugins = await getSentrySvelteKitPlugins({ autoInstrument: false }); const pluginNames = plugins.map(plugin => plugin.name); - expect(plugins).toHaveLength(6); + expect(plugins).toHaveLength(7); expect(pluginNames).not.toContain('sentry-upload-source-maps'); }); From 5d6c40e3b348fe7b9eb72ad14a52776b52d62912 Mon Sep 17 00:00:00 2001 From: s1gr1d Date: Tue, 14 Jan 2025 10:30:18 +0100 Subject: [PATCH 5/9] change comments --- packages/sveltekit/src/vite/sourceMaps.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index bc19b2b771f9..492780fb92c9 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -85,7 +85,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( - '[Source Maps Plugin] Could not load Vite config with Vite "production" mode. This is needed for Sentry to automatically update source map settings.', + '[Sentry] Could not load Vite config with Vite "production" mode. This is needed for Sentry to automatically update source map settings.', ); }); } @@ -95,7 +95,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( - `[Source Maps Plugin] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: ["${filesToDeleteGlob}"]\` to delete generated source maps after they were uploaded to Sentry.`, + `[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: ["${filesToDeleteGlob}"]\` to delete generated source maps after they were uploaded to Sentry.`, ); }); } From 5b0ece0adaea131df4437268256c6f5651682224 Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 14 Jan 2025 15:06:27 +0100 Subject: [PATCH 6/9] use globalThis, fix tests, use correct globs --- packages/sveltekit/src/vite/sourceMaps.ts | 59 ++++---- .../sveltekit/test/vite/sourceMaps.test.ts | 130 +++++++++++------- 2 files changed, 109 insertions(+), 80 deletions(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index 492780fb92c9..12cce3608978 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -24,22 +24,17 @@ type Sorcery = { load(filepath: string): Promise; }; +type GlobalWithSourceMapSetting = typeof globalThis & { + _sentry_sourceMapSetting?: { + updatedSourceMapSetting?: boolean | 'inline' | 'hidden'; + previousSourceMapSetting?: UserSourceMapSetting; + }; +}; + // storing this in the module scope because `makeCustomSentryVitePlugin` is called multiple times // and we only want to generate a uuid once in case we have to fall back to it. const releaseName = detectSentryRelease(); -let sourceMapSetting: { - updatedSourceMapSetting?: boolean | 'inline' | 'hidden'; - previousSourceMapSetting?: UserSourceMapSetting; -} = { previousSourceMapSetting: undefined, updatedSourceMapSetting: undefined }; - -/** - * For mocking the value of `sourceMapSetting` in tests - */ -export const __setSourceMapSettingForTest = (value: typeof sourceMapSetting): void => { - sourceMapSetting = value; -}; - /** * Creates a new Vite plugin that uses the unplugin-based Sentry Vite plugin to create * releases and upload source maps to Sentry. @@ -60,7 +55,10 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug const svelteConfig = await loadSvelteConfig(); const usedAdapter = options?.adapter || 'other'; - const outputDir = await getAdapterOutputDir(svelteConfig, usedAdapter); + const sveltekitOutputDir = svelteConfig.kit?.outDir || '.svelte-kit'; + const adapterOutputDir = await getAdapterOutputDir(svelteConfig, usedAdapter); + + const globalWithSourceMapSetting = globalThis as GlobalWithSourceMapSetting; const defaultPluginOptions: SentryVitePluginOptions = { release: { @@ -73,13 +71,13 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug }, }; - const filesToDeleteGlob = './.*/**/*.map'; + const defaultFileDeletionGlob = [`./${sveltekitOutputDir}/**/*.map`, `./${adapterOutputDir}/**/*.map`]; - if (sourceMapSetting.updatedSourceMapSetting === undefined) { + if (!globalWithSourceMapSetting._sentry_sourceMapSetting) { const configFile = await loadConfigFromFile({ command: 'build', mode: 'production' }); if (configFile) { - sourceMapSetting = getUpdatedSourceMapSetting(configFile.config); + globalWithSourceMapSetting._sentry_sourceMapSetting = getUpdatedSourceMapSetting(configFile.config); } else { if (options?.debug) { consoleSandbox(() => { @@ -91,16 +89,22 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug } } - if (options?.debug && sourceMapSetting.previousSourceMapSetting === 'unset') { + if (options?.debug && globalWithSourceMapSetting._sentry_sourceMapSetting?.previousSourceMapSetting === 'unset') { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( - `[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: ["${filesToDeleteGlob}"]\` to delete generated source maps after they were uploaded to Sentry.`, + `[Sentry] Automatically setting \`sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [${defaultFileDeletionGlob + .map(file => `"${file}"`) + .join(', ')}]\` to delete generated source maps after they were uploaded to Sentry.`, ); }); } } + const shouldDeleteDefaultSourceMaps = + globalWithSourceMapSetting._sentry_sourceMapSetting?.previousSourceMapSetting === 'unset' && + !options?.sourcemaps?.filesToDeleteAfterUpload; + const mergedOptions = { ...defaultPluginOptions, ...options, @@ -110,10 +114,9 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug }, sourcemaps: { ...options?.sourcemaps, - filesToDeleteAfterUpload: - sourceMapSetting.previousSourceMapSetting === 'unset' && !options?.sourcemaps?.filesToDeleteAfterUpload - ? ['./.*/**/*.map'] - : options?.sourcemaps?.filesToDeleteAfterUpload, + filesToDeleteAfterUpload: shouldDeleteDefaultSourceMaps + ? [`./${sveltekitOutputDir}/**/*.map`, `./${adapterOutputDir}/**/*.map`] + : options?.sourcemaps?.filesToDeleteAfterUpload, }, }; @@ -175,7 +178,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug const serverHooksFile = getHooksFileName(svelteConfig, 'server'); const globalSentryValues: GlobalSentryValues = { - __sentry_sveltekit_output_dir: outputDir, + __sentry_sveltekit_output_dir: adapterOutputDir, }; const sourceMapSettingsPlugin: Plugin = { @@ -184,7 +187,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug config: (config: UserConfig) => { const settingKey = 'build.sourcemap'; - if (sourceMapSetting.previousSourceMapSetting === 'unset') { + if (globalWithSourceMapSetting._sentry_sourceMapSetting?.previousSourceMapSetting === 'unset') { consoleSandbox(() => { // eslint-disable-next-line no-console console.log(`[Sentry] Enabled source map generation in the build options with \`${settingKey}: "hidden"\`.`); @@ -194,14 +197,14 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug ...config, build: { ...config.build, sourcemap: 'hidden' }, }; - } else if (sourceMapSetting.previousSourceMapSetting === 'disabled') { + } else if (globalWithSourceMapSetting._sentry_sourceMapSetting?.previousSourceMapSetting === 'disabled') { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( `[Sentry] Parts of source map generation are currently disabled in your Vite configuration (\`${settingKey}: false\`). This setting is either a default setting or was explicitly set in your configuration. Sentry won't override this setting. Without source maps, code snippets on the Sentry Issues page will remain minified. To show unminified code, enable source maps in \`${settingKey}\` (e.g. by setting them to \`hidden\`).`, ); }); - } else if (sourceMapSetting.previousSourceMapSetting === 'enabled') { + } else if (globalWithSourceMapSetting._sentry_sourceMapSetting?.previousSourceMapSetting === 'enabled') { if (mergedOptions?.debug) { consoleSandbox(() => { // eslint-disable-next-line no-console @@ -274,7 +277,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug return; } - const outDir = path.resolve(process.cwd(), outputDir); + const outDir = path.resolve(process.cwd(), adapterOutputDir); // eslint-disable-next-line no-console debug && console.log('[Source Maps Plugin] Looking up source maps in', outDir); @@ -360,7 +363,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug const writeBundleFn = sentryViteFileDeletionPlugin?.writeBundle; if (typeof writeBundleFn === 'function') { // This is fine though, because the original method doesn't consume any arguments in its `writeBundle` callback. - const outDir = path.resolve(process.cwd(), outputDir); + const outDir = path.resolve(process.cwd(), adapterOutputDir); try { // @ts-expect-error - the writeBundle hook expects two args we can't pass in here (they're only available in `writeBundle`) await writeBundleFn({ dir: outDir }); diff --git a/packages/sveltekit/test/vite/sourceMaps.test.ts b/packages/sveltekit/test/vite/sourceMaps.test.ts index 5f7d528516f1..39be8437db89 100644 --- a/packages/sveltekit/test/vite/sourceMaps.test.ts +++ b/packages/sveltekit/test/vite/sourceMaps.test.ts @@ -1,8 +1,9 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { __setSourceMapSettingForTest, getUpdatedSourceMapSetting } from '../../src/vite/sourceMaps'; +import { getUpdatedSourceMapSetting, makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; import type { Plugin } from 'vite'; -import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; + +import * as vite from 'vite'; const mockedViteDebugIdUploadPlugin = { name: 'sentry-vite-debug-id-upload-plugin', @@ -19,6 +20,15 @@ const mockedFileDeletionPlugin = { writeBundle: vi.fn(), }; +vi.mock('vite', async () => { + const original = (await vi.importActual('vite')) as any; + + return { + ...original, + loadConfigFromFile: vi.fn(), + }; +}); + vi.mock('@sentry/vite-plugin', async () => { const original = (await vi.importActual('@sentry/vite-plugin')) as any; @@ -75,68 +85,83 @@ describe('makeCustomSentryVitePlugins()', () => { }); describe('Custom source map settings update plugin', () => { + beforeEach(() => { + // @ts-expect-error - this global variable is set/accessed in src/vite/sourceMaps.ts + globalThis._sentry_sourceMapSetting = undefined; + }); it('returns the custom sentry source maps plugin', async () => { const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); - expect(plugin?.name).toEqual('sentry-sveltekit-update-source-map-setting-plugin'); - expect(plugin?.apply).toEqual('build'); - expect(plugin?.config).toBeInstanceOf(Function); + expect(plugin).toEqual({ + name: 'sentry-sveltekit-update-source-map-setting-plugin', + apply: 'build', + config: expect.any(Function), + }); }); it('keeps source map generation settings when previously enabled', async () => { - const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); - - __setSourceMapSettingForTest({ - previousSourceMapSetting: 'enabled', - updatedSourceMapSetting: undefined, + const originalConfig = { + build: { sourcemap: true, assetsDir: 'assets' }, + }; + + vi.spyOn(vite, 'loadConfigFromFile').mockResolvedValueOnce({ + path: '', + config: originalConfig, + dependencies: [], }); + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + // @ts-expect-error this function exists! - const sentryConfig = plugin.config({ - build: { sourcemap: true, foo: {} }, - test: {}, - }); - expect(sentryConfig).toEqual({ - build: { - foo: {}, - sourcemap: true, - }, - test: {}, - }); + const sentryConfig = plugin.config(originalConfig); + + expect(sentryConfig).toEqual(originalConfig); }); it('keeps source map generation settings when previously disabled', async () => { - const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); - - __setSourceMapSettingForTest({ - previousSourceMapSetting: 'disabled', - updatedSourceMapSetting: undefined, + const originalConfig = { + build: { sourcemap: false, assetsDir: 'assets' }, + }; + + vi.spyOn(vite, 'loadConfigFromFile').mockResolvedValueOnce({ + path: '', + config: originalConfig, + dependencies: [], }); + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); + // @ts-expect-error this function exists! - const sentryConfig = plugin.config({ - build: { sourcemap: false, foo: {} }, - test: {}, - }); + const sentryConfig = plugin.config(originalConfig); + expect(sentryConfig).toEqual({ build: { - foo: {}, + ...originalConfig.build, sourcemap: false, }, - test: {}, }); }); it('enables source map generation with "hidden" when unset', async () => { + const originalConfig = { + build: { assetsDir: 'assets' }, + }; + + vi.spyOn(vite, 'loadConfigFromFile').mockResolvedValueOnce({ + path: '', + config: originalConfig, + dependencies: [], + }); + const plugin = await getSentryViteSubPlugin('sentry-sveltekit-update-source-map-setting-plugin'); // @ts-expect-error this function exists! - const sentryConfig = plugin.config({ build: { foo: {} }, test: {} }); + const sentryConfig = plugin.config(originalConfig); expect(sentryConfig).toEqual({ + ...originalConfig, build: { - foo: {}, + ...originalConfig.build, sourcemap: 'hidden', }, - test: {}, }); }); }); @@ -293,23 +318,24 @@ describe('makeCustomSentryVitePlugins()', () => { }); describe('changeViteSourceMapSettings()', () => { - it('handles vite source map settings', () => { - const cases = [ - { sourcemap: false, expectedSourcemap: false, expectedPrevious: 'disabled' }, - { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedPrevious: 'enabled' }, - { sourcemap: 'inline', expectedSourcemap: 'inline', expectedPrevious: 'enabled' }, - { sourcemap: true, expectedSourcemap: true, expectedPrevious: 'enabled' }, - { sourcemap: undefined, expectedSourcemap: 'hidden', expectedPrevious: 'unset' }, - ]; - - cases.forEach(({ sourcemap, expectedSourcemap, expectedPrevious }) => { - const viteConfig = { build: { sourcemap } }; - const result = getUpdatedSourceMapSetting(viteConfig); - - expect(result).toEqual({ - updatedSourceMapSetting: expectedSourcemap, - previousSourceMapSetting: expectedPrevious, - }); + const cases = [ + { sourcemap: false, expectedSourcemap: false, expectedPrevious: 'disabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedPrevious: 'enabled' }, + { sourcemap: 'inline', expectedSourcemap: 'inline', expectedPrevious: 'enabled' }, + { sourcemap: true, expectedSourcemap: true, expectedPrevious: 'enabled' }, + { sourcemap: undefined, expectedSourcemap: 'hidden', expectedPrevious: 'unset' }, + ]; + + it.each(cases)('handles vite source map settings $1', async ({ sourcemap, expectedSourcemap, expectedPrevious }) => { + const viteConfig = { build: { sourcemap } }; + + const { getUpdatedSourceMapSetting } = await import('../../src/vite/sourceMaps'); + + const result = getUpdatedSourceMapSetting(viteConfig); + + expect(result).toEqual({ + updatedSourceMapSetting: expectedSourcemap, + previousSourceMapSetting: expectedPrevious, }); }); }); From 88b4fc90a2356bef4d2a7445c359b3c74da23c3b Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 14 Jan 2025 15:12:14 +0100 Subject: [PATCH 7/9] lint --- packages/sveltekit/test/vite/sourceMaps.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/sveltekit/test/vite/sourceMaps.test.ts b/packages/sveltekit/test/vite/sourceMaps.test.ts index 39be8437db89..378cbd2099e1 100644 --- a/packages/sveltekit/test/vite/sourceMaps.test.ts +++ b/packages/sveltekit/test/vite/sourceMaps.test.ts @@ -1,5 +1,5 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; -import { getUpdatedSourceMapSetting, makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; +import { makeCustomSentryVitePlugins } from '../../src/vite/sourceMaps'; import type { Plugin } from 'vite'; From 333e853968b124eacf8a176f1e449643749eea2c Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 14 Jan 2025 15:51:58 +0100 Subject: [PATCH 8/9] fix glob --- packages/sveltekit/src/vite/sourceMaps.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index 12cce3608978..439be8d33eac 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -71,7 +71,10 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug }, }; - const defaultFileDeletionGlob = [`./${sveltekitOutputDir}/**/*.map`, `./${adapterOutputDir}/**/*.map`]; + // Including all hidden (`.*`) directories by default so that folders like .vercel, + // .netlify, etc are also cleaned up. Additionally, we include the adapter output + // dir which could be a non-hidden directory, like `build` for the Node adapter. + const defaultFileDeletionGlob = ['./.*/**/*.map', `./${adapterOutputDir}/**/*.map`]; if (!globalWithSourceMapSetting._sentry_sourceMapSetting) { const configFile = await loadConfigFromFile({ command: 'build', mode: 'production' }); @@ -115,7 +118,7 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug sourcemaps: { ...options?.sourcemaps, filesToDeleteAfterUpload: shouldDeleteDefaultSourceMaps - ? [`./${sveltekitOutputDir}/**/*.map`, `./${adapterOutputDir}/**/*.map`] + ? defaultFileDeletionGlob : options?.sourcemaps?.filesToDeleteAfterUpload, }, }; From 6759d1092796d751ebcc5365d5161d2bc504aeac Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Tue, 14 Jan 2025 16:52:14 +0100 Subject: [PATCH 9/9] remove unused var --- packages/sveltekit/src/vite/sourceMaps.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/sveltekit/src/vite/sourceMaps.ts b/packages/sveltekit/src/vite/sourceMaps.ts index 439be8d33eac..be0334348e70 100644 --- a/packages/sveltekit/src/vite/sourceMaps.ts +++ b/packages/sveltekit/src/vite/sourceMaps.ts @@ -55,7 +55,6 @@ export async function makeCustomSentryVitePlugins(options?: CustomSentryVitePlug const svelteConfig = await loadSvelteConfig(); const usedAdapter = options?.adapter || 'other'; - const sveltekitOutputDir = svelteConfig.kit?.outDir || '.svelte-kit'; const adapterOutputDir = await getAdapterOutputDir(svelteConfig, usedAdapter); const globalWithSourceMapSetting = globalThis as GlobalWithSourceMapSetting;