diff --git a/packages/astro/src/integration/index.ts b/packages/astro/src/integration/index.ts index 12eef25a4a67..28092cad84be 100644 --- a/packages/astro/src/integration/index.ts +++ b/packages/astro/src/integration/index.ts @@ -23,13 +23,36 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { // Will revisit this later. const env = process.env; + const { + enabled, + clientInitPath, + serverInitPath, + autoInstrumentation, + sourceMapsUploadOptions, + bundleSizeOptimizations, + debug, + ...otherOptions + } = options; + + 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.`, + ); + }); + } + const sdkEnabled = { - client: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.client ?? true, - server: typeof options.enabled === 'boolean' ? options.enabled : options.enabled?.server ?? true, + client: typeof enabled === 'boolean' ? enabled : enabled?.client ?? true, + server: typeof enabled === 'boolean' ? enabled : enabled?.server ?? true, }; const sourceMapsNeeded = sdkEnabled.client || sdkEnabled.server; - const { unstable_sentryVitePluginOptions, ...uploadOptions } = options.sourceMapsUploadOptions || {}; + const { unstable_sentryVitePluginOptions, ...uploadOptions } = sourceMapsUploadOptions || {}; const shouldUploadSourcemaps = (sourceMapsNeeded && uploadOptions?.enabled) ?? true; // We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env @@ -72,7 +95,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { }, }, ...unstable_sentryVitePluginOptions, - debug: options.debug ?? false, + debug: debug ?? false, sourcemaps: { assets: uploadOptions.assets ?? [getSourcemapsAssetsGlob(config)], filesToDeleteAfterUpload: @@ -80,10 +103,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { ...unstable_sentryVitePluginOptions?.sourcemaps, }, bundleSizeOptimizations: { - ...options.bundleSizeOptimizations, - // TODO: with a future version of the vite plugin (probably 2.22.0) this re-mapping is not needed anymore - // ref: https://github.com/getsentry/sentry-javascript-bundler-plugins/pull/582 - excludePerformanceMonitoring: options.bundleSizeOptimizations?.excludeTracing, + ...bundleSizeOptimizations, ...unstable_sentryVitePluginOptions?.bundleSizeOptimizations, }, }), @@ -93,28 +113,24 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { } if (sdkEnabled.client) { - const pathToClientInit = options.clientInitPath - ? path.resolve(options.clientInitPath) - : findDefaultSdkInitFile('client'); + const pathToClientInit = clientInitPath ? path.resolve(clientInitPath) : findDefaultSdkInitFile('client'); if (pathToClientInit) { - options.debug && logger.info(`Using ${pathToClientInit} for client init.`); + debug && logger.info(`Using ${pathToClientInit} for client init.`); injectScript('page', buildSdkInitFileImportSnippet(pathToClientInit)); } else { - options.debug && logger.info('Using default client init.'); + debug && logger.info('Using default client init.'); injectScript('page', buildClientSnippet(options || {})); } } if (sdkEnabled.server) { - const pathToServerInit = options.serverInitPath - ? path.resolve(options.serverInitPath) - : findDefaultSdkInitFile('server'); + const pathToServerInit = serverInitPath ? path.resolve(serverInitPath) : findDefaultSdkInitFile('server'); if (pathToServerInit) { - options.debug && logger.info(`Using ${pathToServerInit} for server init.`); + debug && logger.info(`Using ${pathToServerInit} for server init.`); injectScript('page-ssr', buildSdkInitFileImportSnippet(pathToServerInit)); } else { - options.debug && logger.info('Using default server init.'); + debug && logger.info('Using default server init.'); injectScript('page-ssr', buildServerSnippet(options || {})); } @@ -136,7 +152,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => { } const isSSR = config && (config.output === 'server' || config.output === 'hybrid'); - const shouldAddMiddleware = sdkEnabled.server && options.autoInstrumentation?.requestHandler !== false; + const shouldAddMiddleware = sdkEnabled.server && autoInstrumentation?.requestHandler !== false; // Guarding calling the addMiddleware function because it was only introduced in astro@3.5.0 // Users on older versions of astro will need to add the middleware manually. diff --git a/packages/astro/src/integration/snippets.ts b/packages/astro/src/integration/snippets.ts index 1d9cea1d8b6f..82278da28925 100644 --- a/packages/astro/src/integration/snippets.ts +++ b/packages/astro/src/integration/snippets.ts @@ -13,7 +13,6 @@ export function buildSdkInitFileImportSnippet(filePath: string): string { * default options. */ export function buildClientSnippet(options: SentryOptions): string { - /* eslint-disable deprecation/deprecation */ return `import * as Sentry from "@sentry/astro"; Sentry.init({ @@ -22,7 +21,6 @@ Sentry.init({ replaysSessionSampleRate: ${options.replaysSessionSampleRate ?? 0.1}, replaysOnErrorSampleRate: ${options.replaysOnErrorSampleRate ?? 1.0}, });`; - /* eslint-enable deprecation/deprecation */ } /** @@ -37,7 +35,6 @@ Sentry.init({ });`; } -/* eslint-disable deprecation/deprecation */ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${ options.dsn ? JSON.stringify(options.dsn) : 'import.meta.env.PUBLIC_SENTRY_DSN' }, @@ -47,7 +44,6 @@ const buildCommonInitOptions = (options: SentryOptions): string => `dsn: ${ tracesSampleRate: ${options.tracesSampleRate ?? 1.0},${ options.sampleRate ? `\n sampleRate: ${options.sampleRate},` : '' }`; -/* eslint-enable deprecation/deprecation */ /** * We don't include the `BrowserTracing` integration if `bundleSizeOptimizations.excludeTracing` is falsy. @@ -64,13 +60,9 @@ const buildClientIntegrations = (options: SentryOptions): string => { } if ( - // eslint-disable-next-line deprecation/deprecation options.replaysSessionSampleRate == null || - // eslint-disable-next-line deprecation/deprecation options.replaysSessionSampleRate || - // eslint-disable-next-line deprecation/deprecation options.replaysOnErrorSampleRate == null || - // eslint-disable-next-line deprecation/deprecation options.replaysOnErrorSampleRate ) { integrations.push('Sentry.replayIntegration()'); diff --git a/packages/astro/src/integration/types.ts b/packages/astro/src/integration/types.ts index 5b5308e3a474..08a8635889fe 100644 --- a/packages/astro/src/integration/types.ts +++ b/packages/astro/src/integration/types.ts @@ -1,5 +1,3 @@ -import type { BrowserOptions } from '@sentry/browser'; -import type { Options } from '@sentry/core'; import type { SentryVitePluginOptions } from '@sentry/vite-plugin'; type SdkInitPaths = { @@ -185,40 +183,14 @@ type SdkEnabledOptions = { }; }; -type DeprecatedRuntimeOptions = Pick< - Options, - 'environment' | 'release' | 'dsn' | 'debug' | 'sampleRate' | 'tracesSampleRate' -> & - Pick & { - /** - * @deprecated Use the `environment` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead. - */ - environment?: string; - /** - * @deprecated Use the `release` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead. - */ - release?: string; - /** - * @deprecated Use the `dsn` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead. - */ - dsn?: string; - /** - * @deprecated Use the `sampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead. - */ - sampleRate?: number; - /** - * @deprecated Use the `tracesSampleRate` option in your runtime-specific Sentry.init() call in sentry.client.config.(js|ts) or sentry.server.config.(js|ts) instead. - */ - tracesSampleRate?: number; - /** - * @deprecated Use the `replaysSessionSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead. - */ - replaysSessionSampleRate?: number; - /** - * @deprecated Use the `replaysOnErrorSampleRate` option in your Sentry.init() call in sentry.client.config.(js|ts) instead. - */ - replaysOnErrorSampleRate?: number; - }; +/** + * We accept aribtrary options that are passed through to the Sentry SDK. + * This is not recommended and will stop working in a future version. + * Note: Not all options are actually passed through, only a select subset: + * release, environment, dsn, debug, sampleRate, tracesSampleRate, replaysSessionSampleRate, replaysOnErrorSampleRate + * @deprecated This will be removed in a future major. + **/ +type DeprecatedRuntimeOptions = Record; /** * A subset of Sentry SDK options that can be set via the `sentryAstro` integration. @@ -230,7 +202,6 @@ type DeprecatedRuntimeOptions = Pick< * If you specify a dedicated init file, the SDK options passed to `sentryAstro` will be ignored. */ export type SentryOptions = SdkInitPaths & - DeprecatedRuntimeOptions & InstrumentationOptions & SdkEnabledOptions & { /** @@ -251,4 +222,5 @@ export type SentryOptions = SdkInitPaths & * If enabled, prints debug logs during the build process. */ debug?: boolean; - }; + // eslint-disable-next-line deprecation/deprecation + } & DeprecatedRuntimeOptions; diff --git a/packages/astro/test/integration/index.test.ts b/packages/astro/test/integration/index.test.ts index df6f37133191..b98644f0b884 100644 --- a/packages/astro/test/integration/index.test.ts +++ b/packages/astro/test/integration/index.test.ts @@ -304,6 +304,35 @@ describe('sentryAstro integration', () => { expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init')); }); + 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: {}, + }); + + 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() } }); + + 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(injectScript).toHaveBeenCalledTimes(2); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('Sentry.init')); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('dsn: "https://test.sentry.io/123"')); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('release: "1.0.0"')); + expect(injectScript).toHaveBeenCalledWith('page', expect.stringContaining('environment: "test"')); + expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('Sentry.init')); + expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('dsn: "https://test.sentry.io/123"')); + expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('release: "1.0.0"')); + expect(injectScript).toHaveBeenCalledWith('page-ssr', expect.stringContaining('environment: "test"')); + }); + it("doesn't inject client init script if `enabled.client` is `false`", async () => { const integration = sentryAstro({ enabled: { client: false } });