Skip to content

feat(astro): Streamline build logs #17301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 24 additions & 36 deletions packages/astro/src/integration/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 = {
Expand All @@ -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;

Expand All @@ -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({
Expand Down Expand Up @@ -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;

Expand All @@ -234,39 +229,32 @@ 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\`).`,
debug &&
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 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 };
Expand Down
47 changes: 26 additions & 21 deletions packages/astro/test/integration/index.test.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -490,18 +494,23 @@ describe('sentryAstro integration', () => {
});
});

describe('getUpdatedSourceMapSettings', () => {
describe('_getUpdatedSourceMapSettings', () => {
let astroConfig: Omit<AstroConfig, 'vite'> & { vite: { build: { sourcemap?: any } } };
let sentryOptions: SentryOptions;
let logger: AstroIntegrationLogger;

beforeEach(() => {
astroConfig = { vite: { build: {} } } as Omit<AstroConfig, 'vite'> & { 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);
});
Expand All @@ -515,34 +524,30 @@ 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);
});
});

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'));

astroConfig.vite.build.sourcemap = 'hidden';
getUpdatedSourceMapSettings(astroConfig, sentryOptions);
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Sentry will keep this source map setting'));

consoleWarnSpy.mockRestore();
consoleLogSpy.mockRestore();
_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'));
});
});
4 changes: 2 additions & 2 deletions packages/react-router/src/vite/makeEnableSourceMapsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOption
...viteConfig,
build: {
...viteConfig.build,
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
sourcemap: _getUpdatedSourceMapSettings(viteConfig, options),
},
};
},
Expand All @@ -37,7 +37,7 @@ export function makeEnableSourceMapsPlugin(options: SentryReactRouterBuildOption
*
* --> only exported for testing
*/
export function getUpdatedSourceMapSettings(
export function _getUpdatedSourceMapSettings(
viteConfig: UserConfig,
sentryPluginOptions?: SentryReactRouterBuildOptions,
): boolean | 'inline' | 'hidden' {
Expand Down
12 changes: 6 additions & 6 deletions packages/react-router/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getUpdatedSourceMapSettings, makeEnableSourceMapsPlugin } from '../../src/vite/makeEnableSourceMapsPlugin';
import { _getUpdatedSourceMapSettings, makeEnableSourceMapsPlugin } from '../../src/vite/makeEnableSourceMapsPlugin';

const mockedSentryVitePlugin = {
name: 'sentry-vite-debug-id-upload-plugin',
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('makeEnableSourceMapsPlugin()', () => {
});
});

describe('getUpdatedSourceMapSettings', () => {
describe('_getUpdatedSourceMapSettings', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, 'warn').mockImplementation(() => {});
Expand All @@ -42,7 +42,7 @@ describe('getUpdatedSourceMapSettings', () => {

describe('when sourcemap is false', () => {
it('should keep sourcemap as false and show warning', () => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: false } });

expect(result).toBe(false);
// eslint-disable-next-line no-console
Expand All @@ -58,7 +58,7 @@ describe('getUpdatedSourceMapSettings', () => {
['inline', 'inline'],
[true, true],
] as ('inline' | 'hidden' | boolean)[][])('should keep sourcemap as %s when set to %s', (input, expected) => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });

expect(result).toBe(expected);
// eslint-disable-next-line no-console
Expand All @@ -72,7 +72,7 @@ describe('getUpdatedSourceMapSettings', () => {
it.each([[undefined], ['invalid'], ['something'], [null]])(
'should set sourcemap to hidden when value is %s',
input => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });

expect(result).toBe('hidden');
// eslint-disable-next-line no-console
Expand All @@ -85,7 +85,7 @@ describe('getUpdatedSourceMapSettings', () => {
);

it('should set sourcemap to hidden when build config is empty', () => {
const result = getUpdatedSourceMapSettings({});
const result = _getUpdatedSourceMapSettings({});

expect(result).toBe('hidden');
// eslint-disable-next-line no-console
Expand Down
4 changes: 2 additions & 2 deletions packages/solidstart/src/vite/sourceMaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function makeEnableSourceMapsVitePlugin(options: SentrySolidStartPluginOp
...viteConfig,
build: {
...viteConfig.build,
sourcemap: getUpdatedSourceMapSettings(viteConfig, options),
sourcemap: _getUpdatedSourceMapSettings(viteConfig, options),
},
};
},
Expand All @@ -93,7 +93,7 @@ export function makeEnableSourceMapsVitePlugin(options: SentrySolidStartPluginOp
*
* --> only exported for testing
*/
export function getUpdatedSourceMapSettings(
export function _getUpdatedSourceMapSettings(
viteConfig: UserConfig,
sentryPluginOptions?: SentrySolidStartPluginOptions,
): boolean | 'inline' | 'hidden' {
Expand Down
12 changes: 6 additions & 6 deletions packages/solidstart/test/vite/sourceMaps.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SentryVitePluginOptions } from '@sentry/vite-plugin';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
getUpdatedSourceMapSettings,
_getUpdatedSourceMapSettings,
makeAddSentryVitePlugin,
makeEnableSourceMapsVitePlugin,
} from '../../src/vite/sourceMaps';
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('makeAddSentryVitePlugin()', () => {
});
});

describe('getUpdatedSourceMapSettings', () => {
describe('_getUpdatedSourceMapSettings', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(console, 'warn').mockImplementation(() => {});
Expand All @@ -180,7 +180,7 @@ describe('getUpdatedSourceMapSettings', () => {

describe('when sourcemap is false', () => {
it('should keep sourcemap as false and show warning', () => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: false } });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: false } });

expect(result).toBe(false);
// eslint-disable-next-line no-console
Expand All @@ -196,7 +196,7 @@ describe('getUpdatedSourceMapSettings', () => {
['inline', 'inline'],
[true, true],
] as ('inline' | 'hidden' | boolean)[][])('should keep sourcemap as %s when set to %s', (input, expected) => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input } }, { debug: true });

expect(result).toBe(expected);
// eslint-disable-next-line no-console
Expand All @@ -210,7 +210,7 @@ describe('getUpdatedSourceMapSettings', () => {
it.each([[undefined], ['invalid'], ['something'], [null]])(
'should set sourcemap to hidden when value is %s',
input => {
const result = getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });
const result = _getUpdatedSourceMapSettings({ build: { sourcemap: input as any } });

expect(result).toBe('hidden');
// eslint-disable-next-line no-console
Expand All @@ -223,7 +223,7 @@ describe('getUpdatedSourceMapSettings', () => {
);

it('should set sourcemap to hidden when build config is empty', () => {
const result = getUpdatedSourceMapSettings({});
const result = _getUpdatedSourceMapSettings({});

expect(result).toBe('hidden');
// eslint-disable-next-line no-console
Expand Down
Loading