Skip to content

Commit a57560f

Browse files
chargomeclaude
andauthored
fix(sveltekit): Resolve Handle and ResolveOptions types on SvelteKit 3 (#26085)
**Why:** SvelteKit 3 moved `Handle` and `ResolveOptions` from `@sveltejs/kit` to `@sveltejs/kit/hooks`. Our declarations still import them from `@sveltejs/kit`, so on Kit 3 the import fails. With `skipLibCheck`, `sentryHandle()` and `initCloudflareSentryHandle()` then quietly return `any`. Kit 2 doesn't export these types from `/hooks`, so no single import path works on both majors. Instead we take them from `sequence`, which both majors export from `/hooks`. Closes #25924 --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 7ae2cc3 commit a57560f

8 files changed

Lines changed: 39 additions & 5 deletions

File tree

‎dev-packages/e2e-tests/test-applications/sveltekit-3/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
"clean": "npx rimraf node_modules pnpm-lock.yaml",
1111
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
1212
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
13+
"check:sentry-types": "svelte-kit sync && tsc -p tsconfig.sentry-types.json",
1314
"test:prod": "TEST_ENV=production playwright test",
14-
"test:build": "pnpm install && pnpm build",
15+
"test:build": "pnpm install && pnpm check:sentry-types && pnpm build",
1516
"test:assert": "pnpm test:prod",
1617
"test:build:tunnel": "pnpm install && E2E_TEST_TUNNEL=1 PUBLIC_E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 pnpm build",
1718
"test:assert:tunnel": "E2E_TEST_TUNNEL=1 PUBLIC_E2E_TEST_TUNNEL=1 E2E_TEST_DSN=http://public@localhost:3031/1337 PUBLIC_E2E_TEST_DSN=http://public@localhost:3031/1337 TEST_ENV=production playwright test tunnel",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type * as Sentry from '@sentry/sveltekit';
2+
3+
// If the SDK's declarations fail to resolve a SvelteKit type, `skipLibCheck` hides the error and
4+
// the type accepts anything. That unresolved type isn't caught by the usual `0 extends 1 & T` any
5+
// check, so test for a top type instead. Checked by `check:sentry-types` in `test:build`.
6+
type AcceptsAnything<T> = unknown extends T ? true : false;
7+
type ExpectFalse<T extends false> = T;
8+
9+
export type SentryTypeChecks = [
10+
ExpectFalse<AcceptsAnything<ReturnType<typeof Sentry.sentryHandle>>>,
11+
ExpectFalse<AcceptsAnything<ReturnType<typeof Sentry.initCloudflareSentryHandle>>>,
12+
];
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"include": ["src/sentryTypes.ts"],
4+
"compilerOptions": {
5+
"noEmit": true
6+
}
7+
}

‎packages/sveltekit/src/server-common/handle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
_INTERNAL_setManagedTunnelDefaults as setManagedTunnelDefaults,
2626
} from '@sentry/core/server';
2727
import { getTunnelBasePath } from '../common/tunnel';
28-
import type { Handle, ResolveOptions } from '@sveltejs/kit';
28+
import type { Handle, ResolveOptions } from './handleTypes';
2929
import { DEBUG_BUILD } from '../common/debug-build';
3030
import { getTracePropagationData, sendErrorToSentry } from './utils';
3131
import {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { sequence } from '@sveltejs/kit/hooks';
2+
3+
/**
4+
* SvelteKit's `Handle` type, on both SvelteKit 2.x and 3.
5+
*
6+
* SvelteKit 3 moved `Handle` and `ResolveOptions` from `@sveltejs/kit` to `@sveltejs/kit/hooks`,
7+
* and SvelteKit 2 doesn't export them from `@sveltejs/kit/hooks`, so no named import resolves on
8+
* both majors. An unresolved import silently turns the type into `any` for users with `skipLibCheck`.
9+
* `sequence` lives in `@sveltejs/kit/hooks` on both majors and is typed with `Handle`.
10+
*/
11+
export type Handle = ReturnType<typeof sequence>;
12+
13+
/** SvelteKit's `ResolveOptions` type, on both SvelteKit 2.x and 3. See {@link Handle}. */
14+
export type ResolveOptions = NonNullable<Parameters<Parameters<Handle>[0]['resolve']>[1]>;

‎packages/sveltekit/src/server/handle.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { CloudflareOptions } from '@sentry/cloudflare';
2-
import type { Handle } from '@sveltejs/kit';
2+
import type { Handle } from '../server-common/handleTypes';
33
import { init } from './sdk';
44

55
/**

‎packages/sveltekit/src/worker/cloudflare.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
setAsyncLocalStorageAsyncContextStrategy,
66
} from '@sentry/cloudflare';
77
import { addNonEnumerableProperty } from '@sentry/core';
8-
import type { Handle } from '@sveltejs/kit';
8+
import type { Handle } from '../server-common/handleTypes';
99
import { rewriteFramesIntegration } from '../server-common/integrations/rewriteFramesIntegration';
1010
import { svelteKitSpansIntegration } from '../server-common/integrations/svelteKitSpans';
1111
import { getCloudflareExecutionContext } from '../server-common/utils';

‎packages/sveltekit/test/server-common/handle.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as SentryCore from '@sentry/core';
55
import { NodeClient, serveTunnelIntegration, setCurrentClient } from '@sentry/node';
66
import { _INTERNAL_resetManagedTunnelDefaults } from '@sentry/core/server';
77
import { TUNNEL_BASE_PATH_GLOBAL } from '../../src/common/tunnel';
8-
import type { Handle } from '@sveltejs/kit';
8+
import type { Handle } from '../../src/server-common/handleTypes';
99
import { redirect } from '@sveltejs/kit';
1010
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
1111
import {

0 commit comments

Comments
 (0)