From 764fb69c589d5f9a4e393a353de66ebf985134b8 Mon Sep 17 00:00:00 2001 From: Shubham Padkonde Date: Sat, 19 Sep 2026 11:23:02 +0530 Subject: [PATCH] fix(hono): Return the existing client on repeated init in Bun and Deno On Bun and Deno, init() warned when Sentry was already initialized but then initialized again, replacing the client. Anything buffered on the first client was dropped and its integrations stayed installed against it. Keep the warning and return the existing client, as the Node entry does. Fixes #24049 Co-Authored-By: Claude Opus 5 --- packages/hono/src/bun/sdk.ts | 5 ++++- packages/hono/src/deno/sdk.ts | 5 ++++- packages/hono/test/bun/middleware.test.ts | 12 ++++++++++-- packages/hono/test/deno/middleware.test.ts | 12 ++++++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/packages/hono/src/bun/sdk.ts b/packages/hono/src/bun/sdk.ts index c62dae003f27..26f9c987348b 100644 --- a/packages/hono/src/bun/sdk.ts +++ b/packages/hono/src/bun/sdk.ts @@ -13,13 +13,16 @@ import { LOW_QUALITY_TRANSACTION_PATTERNS } from '../shared/lowQualityTransactio * When manually calling `init`, add the `honoIntegration` to the `integrations` array to set up the Hono integration. */ export function init(options: HonoBunOptions): Client | undefined { - if (getClient()) { + const existingClient = getClient(); + if (existingClient) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( '[Sentry] Sentry is already initialized. Sentry should only be initialized once, through the `sentry()` middleware. Remove the `Sentry.init()` call, if one exists.', ); }); + // Re-initializing would replace the client and drop anything buffered on it + return existingClient; } applySdkMetadata(options, 'hono', ['hono', 'bun']); diff --git a/packages/hono/src/deno/sdk.ts b/packages/hono/src/deno/sdk.ts index 43083981a3c3..1c40bdb3bdbf 100644 --- a/packages/hono/src/deno/sdk.ts +++ b/packages/hono/src/deno/sdk.ts @@ -13,13 +13,16 @@ import { LOW_QUALITY_TRANSACTION_PATTERNS } from '../shared/lowQualityTransactio * When manually calling `init`, add the `honoIntegration` to the `integrations` array to set up the Hono integration. */ export function init(options: HonoDenoOptions): Client | undefined { - if (getClient()) { + const existingClient = getClient(); + if (existingClient) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( '[Sentry] Sentry is already initialized. Sentry should only be initialized once, through the `sentry()` middleware. Remove the `Sentry.init()` call, if one exists.', ); }); + // Re-initializing would replace the client and drop anything buffered on it + return existingClient; } applySdkMetadata(options, 'hono', ['hono', 'deno']); diff --git a/packages/hono/test/bun/middleware.test.ts b/packages/hono/test/bun/middleware.test.ts index 50f70a77883e..f2ef49fa19a8 100644 --- a/packages/hono/test/bun/middleware.test.ts +++ b/packages/hono/test/bun/middleware.test.ts @@ -171,14 +171,22 @@ describe('Hono Bun Middleware', () => { }); describe('double-init guard', () => { - it('still calls init even when Sentry is already initialized', () => { + it('does not re-initialize when Sentry is already initialized', () => { const fakeClient = { getOptions: () => ({}) }; getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); const app = new Hono(); sentry(app, { dsn: 'https://public@dsn.ingest.sentry.io/1337' }); - expect(initBunMock).toHaveBeenCalledTimes(1); + expect(initBunMock).not.toHaveBeenCalled(); + }); + + it('returns the existing client when Sentry is already initialized', () => { + const fakeClient = { getOptions: () => ({}) }; + getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); + + expect(init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' })).toBe(fakeClient); + expect(initBunMock).not.toHaveBeenCalled(); }); it('emits a console.warn directing to remove the duplicate init call when Sentry is already initialized', () => { diff --git a/packages/hono/test/deno/middleware.test.ts b/packages/hono/test/deno/middleware.test.ts index a6caa2d44455..84506ab494fa 100644 --- a/packages/hono/test/deno/middleware.test.ts +++ b/packages/hono/test/deno/middleware.test.ts @@ -171,14 +171,22 @@ describe('Hono Deno Middleware', () => { }); describe('double-init guard', () => { - it('still calls init even when Sentry is already initialized', () => { + it('does not re-initialize when Sentry is already initialized', () => { const fakeClient = { getOptions: () => ({}) }; getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); const app = new Hono(); sentry(app, { dsn: 'https://public@dsn.ingest.sentry.io/1337' }); - expect(initDenoMock).toHaveBeenCalledTimes(1); + expect(initDenoMock).not.toHaveBeenCalled(); + }); + + it('returns the existing client when Sentry is already initialized', () => { + const fakeClient = { getOptions: () => ({}) }; + getClientMock.mockReturnValue(fakeClient as unknown as SentryCore.Client); + + expect(init({ dsn: 'https://public@dsn.ingest.sentry.io/1337' })).toBe(fakeClient); + expect(initDenoMock).not.toHaveBeenCalled(); }); it('emits a console.warn directing to remove the duplicate init call when Sentry is already initialized', () => {