diff --git a/packages/nuxt/src/vite/sourceMaps.ts b/packages/nuxt/src/vite/sourceMaps.ts index 9abbfe8eaf08..8fd6f0a381fd 100644 --- a/packages/nuxt/src/vite/sourceMaps.ts +++ b/packages/nuxt/src/vite/sourceMaps.ts @@ -1,9 +1,16 @@ import type { Nuxt } from '@nuxt/schema'; import { type SentryRollupPluginOptions, sentryRollupPlugin } from '@sentry/rollup-plugin'; +import { consoleSandbox } from '@sentry/utils'; import { type SentryVitePluginOptions, sentryVitePlugin } from '@sentry/vite-plugin'; import type { NitroConfig } from 'nitropack'; +import type { OutputOptions } from 'rollup'; import type { SentryNuxtModuleOptions } from '../common/types'; +/** + * Whether the user enabled (true, 'hidden', 'inline') or disabled (false) source maps + */ +export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; + /** * Setup source maps for Sentry inside the Nuxt module during build time (in Vite for Nuxt and Rollup for Nitro). */ @@ -11,17 +18,21 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; const sourceMapsEnabled = sourceMapsUploadOptions.enabled ?? true; - nuxt.hook('vite:extendConfig', async (viteInlineConfig, _env) => { - if (sourceMapsEnabled && viteInlineConfig.mode !== 'development') { - // Add Sentry plugin - viteInlineConfig.plugins = viteInlineConfig.plugins || []; - viteInlineConfig.plugins.push(sentryVitePlugin(getPluginOptions(moduleOptions))); + nuxt.hook('modules:done', () => { + if (sourceMapsEnabled && !nuxt.options.dev) { + changeNuxtSourceMapSettings(nuxt, moduleOptions); + } + }); - // Enable source maps - viteInlineConfig.build = viteInlineConfig.build || {}; - viteInlineConfig.build.sourcemap = true; + nuxt.hook('vite:extendConfig', async (viteConfig, _env) => { + if (sourceMapsEnabled && viteConfig.mode !== 'development') { + const previousUserSourceMapSetting = changeViteSourceMapSettings(viteConfig, moduleOptions); - logDebugInfo(moduleOptions, viteInlineConfig.build?.sourcemap); + // Add Sentry plugin + viteConfig.plugins = viteConfig.plugins || []; + viteConfig.plugins.push( + sentryVitePlugin(getPluginOptions(moduleOptions, previousUserSourceMapSetting === 'unset')), + ); } }); @@ -38,15 +49,12 @@ export function setupSourceMaps(moduleOptions: SentryNuxtModuleOptions, nuxt: Nu nitroConfig.rollupConfig.plugins = [nitroConfig.rollupConfig.plugins]; } - // Add Sentry plugin - nitroConfig.rollupConfig.plugins.push(sentryRollupPlugin(getPluginOptions(moduleOptions))); - - // Enable source maps - nitroConfig.rollupConfig.output = nitroConfig?.rollupConfig?.output || {}; - nitroConfig.rollupConfig.output.sourcemap = true; - nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; // Adding "sourcesContent" to the source map (Nitro sets this eto `true`) + const previousUserSourceMapSetting = changeRollupSourceMapSettings(nitroConfig, moduleOptions); - logDebugInfo(moduleOptions, nitroConfig.rollupConfig.output?.sourcemap); + // Add Sentry plugin + nitroConfig.rollupConfig.plugins.push( + sentryRollupPlugin(getPluginOptions(moduleOptions, previousUserSourceMapSetting === 'unset')), + ); } }); } @@ -65,9 +73,19 @@ function normalizePath(path: string): string { */ export function getPluginOptions( moduleOptions: SentryNuxtModuleOptions, + deleteFilesAfterUpload: boolean, ): SentryVitePluginOptions | SentryRollupPluginOptions { const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; + if (typeof sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload === 'undefined' && deleteFilesAfterUpload) { + consoleSandbox(() => { + // eslint-disable-next-line no-console + console.log( + '[Sentry] Setting `sentry.sourceMapsUploadOptions.sourcemaps.filesToDeleteAfterUpload: [".*/**/*.map"]` to delete generated source maps after they were uploaded to Sentry.', + ); + }); + } + return { org: sourceMapsUploadOptions.org ?? process.env.SENTRY_ORG, project: sourceMapsUploadOptions.project ?? process.env.SENTRY_PROJECT, @@ -83,29 +101,192 @@ export function getPluginOptions( sourcemaps: { // The server/client files are in different places depending on the nitro preset (e.g. '.output/server' or '.netlify/functions-internal/server') - // We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that sourcemaps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro). + // We cannot determine automatically how the build folder looks like (depends on the preset), so we have to accept that source maps are uploaded multiple times (with the vitePlugin for Nuxt and the rollupPlugin for Nitro). // If we could know where the server/client assets are located, we could do something like this (based on the Nitro preset): isNitro ? ['./.output/server/**/*'] : ['./.output/public/**/*'], assets: sourceMapsUploadOptions.sourcemaps?.assets ?? undefined, ignore: sourceMapsUploadOptions.sourcemaps?.ignore ?? undefined, - filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload ?? undefined, + filesToDeleteAfterUpload: sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload + ? sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload + : deleteFilesAfterUpload + ? ['.*/**/*.map'] + : undefined, rewriteSources: (source: string) => normalizePath(source), ...moduleOptions?.unstable_sentryBundlerPluginOptions?.sourcemaps, }, }; } -function logDebugInfo(moduleOptions: SentryNuxtModuleOptions, sourceMapsPreviouslyEnabled: boolean): void { - if (moduleOptions.debug && !sourceMapsPreviouslyEnabled) { +/* 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 will not upload anything + 2. users enabled source map generation (true, hidden, inline). + - keep this setting (don't do anything - like deletion - besides uploading) + 3. users did not set source maps generation + - we enable 'hidden' source maps generation + - configure `filesToDeleteAfterUpload` to delete all .map files (we emit a log about this) + + Nuxt has 3 places to set source maps: vite options, rollup options, nuxt itself + Ideally, all 3 are enabled to get all source maps. + */ + +/** only exported for testing */ +export function changeNuxtSourceMapSettings( + nuxt: Nuxt, + sentryModuleOptions: SentryNuxtModuleOptions, +): { client: UserSourceMapSetting; server: UserSourceMapSetting } { + nuxt.options = nuxt.options || {}; + nuxt.options.sourcemap = nuxt.options.sourcemap ?? { server: undefined, client: undefined }; + + let previousUserSourceMapSetting: { client: UserSourceMapSetting; server: UserSourceMapSetting } = { + client: undefined, + server: undefined, + }; + + const nuxtSourceMap = nuxt.options.sourcemap; + + if (typeof nuxtSourceMap === 'string' || typeof nuxtSourceMap === 'boolean' || typeof nuxtSourceMap === 'undefined') { + switch (nuxtSourceMap) { + case false: + warnExplicitlyDisabledSourceMap('sourcemap'); + previousUserSourceMapSetting = { client: 'disabled', server: 'disabled' }; + break; + + case 'hidden': + case true: + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap', (nuxtSourceMap as true).toString()); + previousUserSourceMapSetting = { client: 'enabled', server: 'enabled' }; + break; + case undefined: + nuxt.options.sourcemap = { server: 'hidden', client: 'hidden' }; + logSentryEnablesSourceMap('sourcemap.client', 'hidden'); + logSentryEnablesSourceMap('sourcemap.server', 'hidden'); + previousUserSourceMapSetting = { client: 'unset', server: 'unset' }; + break; + } + } else { + if (nuxtSourceMap.client === false) { + warnExplicitlyDisabledSourceMap('sourcemap.client'); + previousUserSourceMapSetting.client = 'disabled'; + } else if (['hidden', true].includes(nuxtSourceMap.client)) { + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap.client', nuxtSourceMap.client.toString()); + previousUserSourceMapSetting.client = 'enabled'; + } else { + nuxt.options.sourcemap.client = 'hidden'; + logSentryEnablesSourceMap('sourcemap.client', 'hidden'); + previousUserSourceMapSetting.client = 'unset'; + } + + if (nuxtSourceMap.server === false) { + warnExplicitlyDisabledSourceMap('sourcemap.server'); + previousUserSourceMapSetting.server = 'disabled'; + } else if (['hidden', true].includes(nuxtSourceMap.server)) { + logKeepSourceMapSetting(sentryModuleOptions, 'sourcemap.server', nuxtSourceMap.server.toString()); + previousUserSourceMapSetting.server = 'enabled'; + } else { + nuxt.options.sourcemap.server = 'hidden'; + logSentryEnablesSourceMap('sourcemap.server', 'hidden'); + previousUserSourceMapSetting.server = 'unset'; + } + } + + return previousUserSourceMapSetting; +} + +/** only exported for testing */ +export function changeViteSourceMapSettings( + viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }, + sentryModuleOptions: SentryNuxtModuleOptions, +): UserSourceMapSetting { + viteConfig.build = viteConfig.build || {}; + const viteSourceMap = viteConfig.build.sourcemap; + + let previousUserSourceMapSetting: UserSourceMapSetting; + + if (viteSourceMap === false) { + warnExplicitlyDisabledSourceMap('vite.build.sourcemap'); + previousUserSourceMapSetting = 'disabled'; + } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { + logKeepSourceMapSetting(sentryModuleOptions, 'vite.build.sourcemap', viteSourceMap.toString()); + previousUserSourceMapSetting = 'enabled'; + } else { + viteConfig.build.sourcemap = 'hidden'; + logSentryEnablesSourceMap('vite.build.sourcemap', 'hidden'); + previousUserSourceMapSetting = 'unset'; + } + + return previousUserSourceMapSetting; +} + +/** only exported for testing */ +export function changeRollupSourceMapSettings( + nitroConfig: { + rollupConfig?: { + output?: { + sourcemap?: OutputOptions['sourcemap']; + sourcemapExcludeSources?: OutputOptions['sourcemapExcludeSources']; + }; + }; + }, + sentryModuleOptions: SentryNuxtModuleOptions, +): UserSourceMapSetting { + nitroConfig.rollupConfig = nitroConfig.rollupConfig || {}; + nitroConfig.rollupConfig.output = nitroConfig.rollupConfig.output || { sourcemap: undefined }; + + let previousUserSourceMapSetting: UserSourceMapSetting; + + const nitroSourceMap = nitroConfig.rollupConfig.output.sourcemap; + + if (nitroSourceMap === false) { + warnExplicitlyDisabledSourceMap('nitro.rollupConfig.output.sourcemap'); + previousUserSourceMapSetting = 'disabled'; + } else if (nitroSourceMap && ['hidden', 'inline', true].includes(nitroSourceMap)) { + logKeepSourceMapSetting(sentryModuleOptions, 'nitro.rollupConfig.output.sourcemap', nitroSourceMap.toString()); + previousUserSourceMapSetting = 'enabled'; + } else { + nitroConfig.rollupConfig.output.sourcemap = 'hidden'; + logSentryEnablesSourceMap('nitro.rollupConfig.output.sourcemap', 'hidden'); + previousUserSourceMapSetting = 'unset'; + } + + nitroConfig.rollupConfig.output.sourcemapExcludeSources = false; + consoleSandbox(() => { // eslint-disable-next-line no-console - console.log('[Sentry]: Enabled source maps generation in the Vite build options.'); + console.log( + '[Sentry] Disabled source map setting in the Nuxt config: `nitro.rollupConfig.output.sourcemapExcludeSources`. Source maps will include the actual code to be able to un-minify code snippets in Sentry.', + ); + }); - const sourceMapsUploadOptions = moduleOptions.sourceMapsUploadOptions || {}; + return previousUserSourceMapSetting; +} - if (!sourceMapsUploadOptions.sourcemaps?.filesToDeleteAfterUpload) { +function logKeepSourceMapSetting( + sentryNuxtModuleOptions: SentryNuxtModuleOptions, + settingKey: string, + settingValue: string, +): void { + if (sentryNuxtModuleOptions.debug) { + consoleSandbox(() => { // eslint-disable-next-line no-console - console.warn( - '[Sentry] 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', + 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}\`.`, + ); + }); +} + +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}\`.`); + }); +} diff --git a/packages/nuxt/test/vite/sourceMaps.test.ts b/packages/nuxt/test/vite/sourceMaps.test.ts index 34c520b96d83..0c90429fa8d5 100644 --- a/packages/nuxt/test/vite/sourceMaps.test.ts +++ b/packages/nuxt/test/vite/sourceMaps.test.ts @@ -1,6 +1,13 @@ +import type { Nuxt } from '@nuxt/schema'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import type { SentryNuxtModuleOptions } from '../../src/common/types'; -import { getPluginOptions } from '../../src/vite/sourceMaps'; +import type { UserSourceMapSetting } from '../../src/vite/sourceMaps'; +import { + changeNuxtSourceMapSettings, + changeRollupSourceMapSettings, + changeViteSourceMapSettings, + getPluginOptions, +} from '../../src/vite/sourceMaps'; describe('getPluginOptions', () => { beforeEach(() => { @@ -17,7 +24,7 @@ describe('getPluginOptions', () => { process.env = { ...defaultEnv }; - const options = getPluginOptions({} as SentryNuxtModuleOptions); + const options = getPluginOptions({} as SentryNuxtModuleOptions, false); expect(options).toEqual( expect.objectContaining({ @@ -39,7 +46,7 @@ describe('getPluginOptions', () => { }); it('returns default options when no moduleOptions are provided', () => { - const options = getPluginOptions({} as SentryNuxtModuleOptions); + const options = getPluginOptions({} as SentryNuxtModuleOptions, false); expect(options.org).toBeUndefined(); expect(options.project).toBeUndefined(); @@ -75,7 +82,7 @@ describe('getPluginOptions', () => { }, debug: true, }; - const options = getPluginOptions(customOptions); + const options = getPluginOptions(customOptions, true); expect(options).toEqual( expect.objectContaining({ org: 'custom-org', @@ -119,7 +126,7 @@ describe('getPluginOptions', () => { }, }, }; - const options = getPluginOptions(customOptions); + const options = getPluginOptions(customOptions, false); expect(options).toEqual( expect.objectContaining({ debug: true, @@ -137,3 +144,145 @@ describe('getPluginOptions', () => { ); }); }); + +describe('change sourcemap settings', () => { + describe('changeViteSourcemapSettings', () => { + let viteConfig: { build?: { sourcemap?: boolean | 'inline' | 'hidden' } }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + viteConfig = {}; + sentryModuleOptions = {}; + }); + + it('should handle viteConfig.build.sourcemap settings', () => { + const cases: { + sourcemap?: boolean | 'hidden' | 'inline'; + expectedSourcemap: boolean | string; + expectedReturn: UserSourceMapSetting; + }[] = [ + { 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); + }); + }); + }); + + describe('changeRollupSourcemapSettings', () => { + let nitroConfig: { + rollupConfig?: { output?: { sourcemap?: boolean | 'hidden' | 'inline'; sourcemapExcludeSources?: boolean } }; + }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + nitroConfig = {}; + sentryModuleOptions = {}; + }); + + it('should handle nitroConfig.rollupConfig.output.sourcemap settings', () => { + const cases: { + output?: { sourcemap?: boolean | 'hidden' | 'inline' }; + expectedSourcemap: boolean | string; + expectedReturn: UserSourceMapSetting; + }[] = [ + { output: { sourcemap: false }, expectedSourcemap: false, expectedReturn: 'disabled' }, + { output: { sourcemap: 'hidden' }, expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { output: { sourcemap: 'inline' }, expectedSourcemap: 'inline', expectedReturn: 'enabled' }, + { output: { sourcemap: true }, expectedSourcemap: true, expectedReturn: 'enabled' }, + { output: { sourcemap: undefined }, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + { output: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ output, expectedSourcemap, expectedReturn }) => { + nitroConfig.rollupConfig = { output }; + const previousUserSourceMapSetting = changeRollupSourceMapSettings(nitroConfig, sentryModuleOptions); + expect(nitroConfig.rollupConfig?.output?.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourceMapSetting).toBe(expectedReturn); + expect(nitroConfig.rollupConfig?.output?.sourcemapExcludeSources).toBe(false); + }); + }); + }); + + describe('changeNuxtSourcemapSettings', () => { + let nuxt: { options: { sourcemap: { client: boolean | 'hidden'; server: boolean | 'hidden' } } }; + let sentryModuleOptions: SentryNuxtModuleOptions; + + beforeEach(() => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt = { options: { sourcemap: { client: undefined } } }; + sentryModuleOptions = {}; + }); + + it('should handle nuxt.options.sourcemap.client settings', () => { + const cases = [ + // { clientSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + // { clientSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { clientSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { clientSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ clientSourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt.options.sourcemap.client = clientSourcemap; + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.client).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.client).toBe(expectedReturn); + }); + }); + + it('should handle nuxt.options.sourcemap.server settings', () => { + const cases = [ + { serverSourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { serverSourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + { serverSourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { serverSourcemap: undefined, expectedSourcemap: 'hidden', expectedReturn: 'unset' }, + ]; + + cases.forEach(({ serverSourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error server available + nuxt.options.sourcemap.server = serverSourcemap; + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.server).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.server).toBe(expectedReturn); + }); + }); + + describe('should handle nuxt.options.sourcemap as a boolean', () => { + it('keeps setting of nuxt.options.sourcemap if it is set', () => { + const cases = [ + { sourcemap: false, expectedSourcemap: false, expectedReturn: 'disabled' }, + { sourcemap: true, expectedSourcemap: true, expectedReturn: 'enabled' }, + { sourcemap: 'hidden', expectedSourcemap: 'hidden', expectedReturn: 'enabled' }, + ]; + + cases.forEach(({ sourcemap, expectedSourcemap, expectedReturn }) => { + // @ts-expect-error string type is possible in Nuxt (but type says differently) + nuxt.options.sourcemap = sourcemap; + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap).toBe(expectedSourcemap); + expect(previousUserSourcemapSetting.client).toBe(expectedReturn); + expect(previousUserSourcemapSetting.server).toBe(expectedReturn); + }); + }); + + it("sets client and server to 'hidden' if nuxt.options.sourcemap not set", () => { + // @ts-expect-error - Nuxt types don't accept `undefined` but we want to test this case + nuxt.options.sourcemap = undefined; + const previousUserSourcemapSetting = changeNuxtSourceMapSettings(nuxt as Nuxt, sentryModuleOptions); + expect(nuxt.options.sourcemap.client).toBe('hidden'); + expect(nuxt.options.sourcemap.server).toBe('hidden'); + expect(previousUserSourcemapSetting.client).toBe('unset'); + expect(previousUserSourcemapSetting.server).toBe('unset'); + }); + }); + }); +});