Skip to content

Commit bbe8812

Browse files
authored
fix(astro): Avoid adding the Sentry Vite plugin in dev mode (#9688)
Add a guard to avoid adding the Sentry Vite plugin for source maps upload in dev mode. Reason: The vite plugin ships with a Sentry SDK. This SDK conflicts with the Astro server-side SDK. For example for some reason, it skews up the line numbers of stack frames. Not exactly sure why that's happening but my best guess is that the plugin SDK somehow changes code (maybe we even inject debugIds or release values or something like this) that mixes up line numbers. Anyway, I think it's generally more correct to not use the plugin in dev mode.
1 parent 42e0910 commit bbe8812

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

packages/astro/src/integration/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
1414
name: PKG_NAME,
1515
hooks: {
1616
// eslint-disable-next-line complexity
17-
'astro:config:setup': async ({ updateConfig, injectScript, addMiddleware, config }) => {
17+
'astro:config:setup': async ({ updateConfig, injectScript, addMiddleware, config, command }) => {
1818
// The third param here enables loading of all env vars, regardless of prefix
1919
// see: https://main.vitejs.dev/config/#using-environment-variables-in-config
2020

@@ -29,7 +29,7 @@ export const sentryAstro = (options: SentryOptions = {}): AstroIntegration => {
2929
const shouldUploadSourcemaps = uploadOptions?.enabled ?? true;
3030

3131
// We don't need to check for AUTH_TOKEN here, because the plugin will pick it up from the env
32-
if (shouldUploadSourcemaps) {
32+
if (shouldUploadSourcemaps && command !== 'dev') {
3333
updateConfig({
3434
vite: {
3535
build: {

packages/astro/test/integration/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,19 @@ describe('sentryAstro integration', () => {
155155
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(0);
156156
});
157157

158+
it("doesn't add the Vite plugin in dev mode", async () => {
159+
const integration = sentryAstro({
160+
sourceMapsUploadOptions: { enabled: true },
161+
});
162+
163+
expect(integration.hooks['astro:config:setup']).toBeDefined();
164+
// @ts-expect-error - the hook exists and we only need to pass what we actually use
165+
await integration.hooks['astro:config:setup']({ updateConfig, injectScript, config, command: 'dev' });
166+
167+
expect(updateConfig).toHaveBeenCalledTimes(0);
168+
expect(sentryVitePluginSpy).toHaveBeenCalledTimes(0);
169+
});
170+
158171
it('injects client and server init scripts', async () => {
159172
const integration = sentryAstro({});
160173

0 commit comments

Comments
 (0)