diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 28092cad84be..1a9eeaff8cd4 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -1,6 +1,5 @@ -import { consoleSandbox } from '@sentry/core'; import { sentryVitePlugin } from '@sentry/vite-plugin'; -import type { AstroConfig, AstroIntegration } from 'astro'; +import type { AstroConfig, AstroIntegration, AstroIntegrationLogger } from 'astro'; import * as fs from 'fs'; import * as path from 'path'; import { buildClientSnippet, buildSdkInitFileImportSnippet, buildServerSnippet } from './snippets'; @@ -36,14 +35,11 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { const otherOptionsKeys = Object.keys(otherOptions); if (otherOptionsKeys.length > 0) { - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - `[Sentry] You passed in additional options (${otherOptionsKeys.join( - ', ', - )}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`, - ); - }); + logger.warn( + `You passed in additional options (${otherOptionsKeys.join( + ', ', + )}) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your \`sentry.client.config.(js|ts)\` or \`sentry.server.config.(js|ts)\` files.`, + ); } const sdkEnabled = { @@ -57,7 +53,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { // We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env if (shouldUploadSourcemaps && command !== 'dev') { - const computedSourceMapSettings = getUpdatedSourceMapSettings(config, options); + const computedSourceMapSettings = _getUpdatedSourceMapSettings(config, options, logger); let updatedFilesToDeleteAfterUpload: string[] | undefined = undefined; @@ -68,14 +64,12 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { // This also works for adapters, as the source maps are also copied to e.g. the .vercel folder updatedFilesToDeleteAfterUpload = ['./dist/**/client/**/*.map', './dist/**/server/**/*.map']; - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log( - `[Sentry] Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify( + debug && + logger.info( + `Automatically setting \`sourceMapsUploadOptions.filesToDeleteAfterUpload: ${JSON.stringify( updatedFilesToDeleteAfterUpload, )}\` to delete generated source maps after they were uploaded to Sentry.`, ); - }); } updateConfig({ @@ -222,9 +216,10 @@ export type UserSourceMapSetting = 'enabled' | 'disabled' | 'unset' | undefined; * * --> only exported for testing */ -export function getUpdatedSourceMapSettings( +export function _getUpdatedSourceMapSettings( astroConfig: AstroConfig, - sentryOptions?: SentryOptions, + sentryOptions: SentryOptions | undefined, + logger: AstroIntegrationLogger, ): { previousUserSourceMapSetting: UserSourceMapSetting; updatedSourceMapSetting: boolean | 'inline' | 'hidden' } { let previousUserSourceMapSetting: UserSourceMapSetting = undefined; @@ -234,39 +229,36 @@ export function getUpdatedSourceMapSettings( let updatedSourceMapSetting = viteSourceMap; const settingKey = 'vite.build.sourcemap'; + const debug = sentryOptions?.debug; if (viteSourceMap === false) { previousUserSourceMapSetting = 'disabled'; updatedSourceMapSetting = viteSourceMap; - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - `[Sentry] Source map generation is currently disabled in your Astro 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\`).`, + if (debug) { + // Longer debug message with more details + logger.warn( + `Source map generation is currently disabled in your Astro 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 { + logger.warn('Source map generation is disabled in your Astro configuration.'); + } } else if (viteSourceMap && ['hidden', 'inline', true].includes(viteSourceMap)) { previousUserSourceMapSetting = 'enabled'; updatedSourceMapSetting = viteSourceMap; - if (sentryOptions?.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.`, - ); - }); - } + debug && + logger.info( + `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 { previousUserSourceMapSetting = 'unset'; updatedSourceMapSetting = 'hidden'; - consoleSandbox(() => { - // eslint-disable-next-line no-console - console.log( - `[Sentry] Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`, + debug && + logger.info( + `Enabled source map generation in the build options with \`${settingKey}: 'hidden'\`. The source maps will be deleted after they were uploaded to Sentry.`, ); - }); } return { previousUserSourceMapSetting, updatedSourceMapSetting }; diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index b98644f0b884..f7c0f2ec9e14 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -1,6 +1,6 @@ -import type { AstroConfig } from 'astro'; +import type { AstroConfig, AstroIntegrationLogger } from 'astro'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; -import { getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration'; +import { _getUpdatedSourceMapSettings, sentryAstro } from '../../src/integration'; import type { SentryOptions } from '../../src/integration/types'; const sentryVitePluginSpy = vi.fn(() => 'sentryVitePlugin'); @@ -305,21 +305,25 @@ describe('sentryAstro integration', () => { }); it('injects runtime config into client and server init scripts and warns about deprecation', async () => { - const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); const integration = sentryAstro({ environment: 'test', release: '1.0.0', dsn: 'https://test.sentry.io/123', - debug: true, bundleSizeOptimizations: {}, + // this also warns when debug is not enabled }); + const logger = { + warn: vi.fn(), + info: vi.fn(), + }; + expect(integration.hooks['astro:config:setup']).toBeDefined(); // @ts-expect-error - the hook exists and we only need to pass what we actually use - await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger: { info: vi.fn() } }); + await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, logger }); - expect(consoleWarnSpy).toHaveBeenCalledWith( - '[Sentry] You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.', + expect(logger.warn).toHaveBeenCalledWith( + 'You passed in additional options (environment, release, dsn) to the Sentry integration. This is deprecated and will stop working in a future version. Instead, configure the Sentry SDK in your `sentry.client.config.(js|ts)` or `sentry.server.config.(js|ts)` files.', ); expect(injectScript).toHaveBeenCalledTimes(2); @@ -490,18 +494,23 @@ describe('sentryAstro integration', () => { }); }); -describe('getUpdatedSourceMapSettings', () => { +describe('_getUpdatedSourceMapSettings', () => { let astroConfig: Omit & { vite: { build: { sourcemap?: any } } }; let sentryOptions: SentryOptions; + let logger: AstroIntegrationLogger; beforeEach(() => { astroConfig = { vite: { build: {} } } as Omit & { vite: { build: { sourcemap?: any } } }; sentryOptions = {}; + logger = { + info: vi.fn(), + warn: vi.fn(), + } as unknown as AstroIntegrationLogger; }); it('should keep explicitly disabled source maps disabled', () => { astroConfig.vite.build.sourcemap = false; - const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions); + const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); expect(result.previousUserSourceMapSetting).toBe('disabled'); expect(result.updatedSourceMapSetting).toBe(false); }); @@ -515,7 +524,7 @@ describe('getUpdatedSourceMapSettings', () => { cases.forEach(({ sourcemap, expected }) => { astroConfig.vite.build.sourcemap = sourcemap; - const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions); + const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); expect(result.previousUserSourceMapSetting).toBe('enabled'); expect(result.updatedSourceMapSetting).toBe(expected); }); @@ -523,26 +532,41 @@ describe('getUpdatedSourceMapSettings', () => { it('should enable "hidden" source maps when unset', () => { astroConfig.vite.build.sourcemap = undefined; - const result = getUpdatedSourceMapSettings(astroConfig, sentryOptions); + const result = _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); expect(result.previousUserSourceMapSetting).toBe('unset'); expect(result.updatedSourceMapSetting).toBe('hidden'); }); it('should log warnings and messages when debug is enabled', () => { - const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}); - const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); - sentryOptions = { debug: true }; astroConfig.vite.build.sourcemap = false; - getUpdatedSourceMapSettings(astroConfig, sentryOptions); - expect(consoleWarnSpy).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled')); + _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); + + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(logger.warn).toHaveBeenCalledWith(expect.stringContaining('Source map generation is currently disabled')); + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(logger.warn).toHaveBeenCalledWith( + expect.stringContaining('This setting is either a default setting or was explicitly set in your configuration.'), + ); astroConfig.vite.build.sourcemap = 'hidden'; - getUpdatedSourceMapSettings(astroConfig, sentryOptions); - expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting')); + _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(logger.info).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting')); + }); - consoleWarnSpy.mockRestore(); - consoleLogSpy.mockRestore(); + it('should show short warnings debug is disabled', () => { + sentryOptions = { debug: false }; + + astroConfig.vite.build.sourcemap = false; + _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(logger.warn).toHaveBeenCalledWith('Source map generation is disabled in your Astro configuration.'); + + astroConfig.vite.build.sourcemap = 'hidden'; + _getUpdatedSourceMapSettings(astroConfig, sentryOptions, logger); + // eslint-disable-next-line @typescript-eslint/unbound-method + expect(logger.info).not.toHaveBeenCalled(); }); });