Skip to content

Commit ce8dbc7

Browse files
committed
guard for double import
1 parent 092858f commit ce8dbc7

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/tanstackstart-react/src/vite/autoInstrumentMiddleware.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ export function arrayToObjectShorthand(contents: string): string | null {
166166
export function addSentryImport(code: string): string {
167167
const sentryImport = "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\n";
168168

169+
// Don't add the import if it already exists
170+
if (code.includes(sentryImport.trimEnd())) {
171+
return code;
172+
}
173+
169174
// Check for 'use server' or 'use client' directives, these need to be before any imports
170175
const directiveMatch = code.match(/^(['"])use (client|server)\1;?\s*\n?/);
171176

packages/tanstackstart-react/test/vite/autoInstrumentMiddleware.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,16 @@ describe('addSentryImport', () => {
352352
expect(result).toMatch(/^"use client";\nimport \{ wrapMiddlewaresWithSentry \}/);
353353
expect(result).toContain('const foo = 1;');
354354
});
355+
356+
it('does not add import if it already exists', () => {
357+
const code = "import { wrapMiddlewaresWithSentry } from '@sentry/tanstackstart-react';\nconst foo = 1;";
358+
const result = addSentryImport(code);
359+
360+
expect(result).toBe(code);
361+
// Verify the import appears exactly once
362+
const importCount = (result.match(/import \{ wrapMiddlewaresWithSentry \}/g) || []).length;
363+
expect(importCount).toBe(1);
364+
});
355365
});
356366

357367
describe('arrayToObjectShorthand', () => {

0 commit comments

Comments
 (0)