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 all 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
64 changes: 28 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,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.');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.warn('Source map generation is disabled in your Astro configuration.');
logger.warn(`Source map generation is disabled in your Astro configuration (\`${settingKey}: false\`.`);

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to keep this extra compact, users can use debug: true for more information :)

} 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
64 changes: 44 additions & 20 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,49 @@ 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'));
// 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();
});
});
Loading