From 29c48425bdd817791592ae2b4b9f9d1294a0aad3 Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Mon, 23 Nov 2026 17:40:27 +0100 Subject: [PATCH 1/3] fix(solidstart): Ignore build output spans with span streaming The build asset filter was an event processor, and event processors do not run with span streaming. So the SDK sent a span for every request to `/_build/`. Add an `ignoreSpans` default on `url.path` instead, which works in both trace modes. --- .../tests/performance.server.test.ts | 39 +++++++++- ...erformance.server.static-lifecycle.test.ts | 45 +++++++++++- ...ormance.server.streaming-lifecycle.test.ts | 46 +++++++++++- packages/solidstart/src/server/sdk.ts | 11 ++- packages/solidstart/src/server/utils.ts | 34 --------- packages/solidstart/test/server/sdk.test.ts | 73 ++++++++----------- 6 files changed, 167 insertions(+), 81 deletions(-) diff --git a/dev-packages/e2e-tests/test-applications/solidstart-2/tests/performance.server.test.ts b/dev-packages/e2e-tests/test-applications/solidstart-2/tests/performance.server.test.ts index e2e668c9ec90..d175ad0b25fd 100644 --- a/dev-packages/e2e-tests/test-applications/solidstart-2/tests/performance.server.test.ts +++ b/dev-packages/e2e-tests/test-applications/solidstart-2/tests/performance.server.test.ts @@ -1,5 +1,5 @@ import { expect, test } from '@playwright/test'; -import { collectStreamedSpans, getSpanOp } from '@sentry-internal/test-utils'; +import { collectStreamedSpans, getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; test('sends a server action span on pageload', async ({ page }) => { const spansPromise = collectStreamedSpans( @@ -46,3 +46,40 @@ test('sends a server action span on client navigation', async ({ page }) => { 'code.function.name': { value: 'getPrefecture', type: 'string' }, }); }); + +['/_build/assets/entry.js.map', '/_build/.vite/manifest.json'].forEach(path => { + test(`does not send a server span for the file ${path}`, async ({ baseURL }) => { + const segmentPaths = await getServerSegmentPathsUntilPage(`${baseURL}`, path); + + expect(segmentPaths).not.toContain(path); + }); +}); + +// Routes that look like build output. They also show that the helper sees sent segments, so the tests above cannot +// pass by accident. +['/users/jane.doe', '/users/_build/'].forEach(path => { + test(`sends a server span for the route ${path}`, async ({ baseURL }) => { + const segmentPaths = await getServerSegmentPathsUntilPage(`${baseURL}`, path); + + expect(segmentPaths).toContain(path); + }); +}); + +/** + * Requests `path`, then a page. Returns the `url.path` of each server segment up to the page's segment, which arrives + * after any segment for `path`. + */ +async function getServerSegmentPathsUntilPage(baseURL: string, path: string): Promise { + const pagePath = `/users/after${path.replace(/\W+/g, '-')}`; + const segmentPaths: string[] = []; + const pageSegmentPromise = waitForStreamedSpans('solidstart-2', spans => { + segmentPaths.push(...spans.filter(span => span.is_segment).map(span => `${span.attributes['url.path']?.value}`)); + return segmentPaths.includes(pagePath); + }); + + await fetch(`${baseURL}${path}`); + await fetch(`${baseURL}${pagePath}`); + await pageSegmentPromise; + + return segmentPaths; +} diff --git a/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.static-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.static-lifecycle.test.ts index bbac70d1abcb..c6adae79775e 100644 --- a/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.static-lifecycle.test.ts +++ b/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.static-lifecycle.test.ts @@ -1,4 +1,4 @@ -import { SENTRY_OP, SENTRY_ORIGIN } from '@sentry/conventions/attributes'; +import { SENTRY_OP, SENTRY_ORIGIN, URL_PATH } from '@sentry/conventions/attributes'; import { expect, test } from '@playwright/test'; import { waitForTransaction } from '@sentry-internal/test-utils'; @@ -49,3 +49,46 @@ test('sends a server action transaction on client navigation', async ({ page }) ]), ); }); + +[ + // SolidStart build output that the HTTP integration does not ignore. + '/_build/assets/entry.js.map', + '/_build/.vite/manifest.json', + // Common static file that the HTTP integration ignores. + '/favicon.ico', +].forEach(path => { + test(`does not send a server transaction for the file ${path}`, async ({ baseURL }) => { + const transactionPaths = await getServerTransactionPathsUntilPage(`${baseURL}`, path); + + expect(transactionPaths).not.toContain(path); + }); +}); + +// Routes that look like build output. They also show that the helper sees sent transactions, so the tests above cannot +// pass by accident. +['/users/jane.doe', '/users/_build/'].forEach(path => { + test(`sends a server transaction for the route ${path}`, async ({ baseURL }) => { + const transactionPaths = await getServerTransactionPathsUntilPage(`${baseURL}`, path); + + expect(transactionPaths).toContain(path); + }); +}); + +/** + * Requests `path`, then a page. Returns the `url.path` of each server transaction up to the page's transaction, + * which arrives after any transaction for `path`. + */ +async function getServerTransactionPathsUntilPage(baseURL: string, path: string): Promise { + const pagePath = `/users/after${path.replace(/\W+/g, '-')}`; + const transactionPaths: string[] = []; + const pageTransactionPromise = waitForTransaction('solidstart', transactionEvent => { + transactionPaths.push(`${transactionEvent.contexts?.trace?.data?.[URL_PATH]}`); + return transactionPaths.includes(pagePath); + }); + + await fetch(`${baseURL}${path}`); + await fetch(`${baseURL}${pagePath}`); + await pageTransactionPromise; + + return transactionPaths; +} diff --git a/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.streaming-lifecycle.test.ts b/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.streaming-lifecycle.test.ts index 251a1601ae53..fb2886396856 100644 --- a/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.streaming-lifecycle.test.ts +++ b/dev-packages/e2e-tests/test-applications/solidstart/tests/performance.server.streaming-lifecycle.test.ts @@ -1,5 +1,6 @@ +import { URL_PATH } from '@sentry/conventions/attributes'; import { expect, test } from '@playwright/test'; -import { collectStreamedSpans, getSpanOp } from '@sentry-internal/test-utils'; +import { collectStreamedSpans, getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; test('sends a server action span on pageload', async ({ page }) => { const spansPromise = collectStreamedSpans( @@ -46,3 +47,46 @@ test('sends a server action span on client navigation', async ({ page }) => { 'code.function.name': { value: 'getPrefecture', type: 'string' }, }); }); + +[ + // SolidStart build output that the HTTP integration does not ignore. + '/_build/assets/entry.js.map', + '/_build/.vite/manifest.json', + // Common static file that the HTTP integration ignores. + '/favicon.ico', +].forEach(path => { + test(`does not send a server span for the file ${path}`, async ({ baseURL }) => { + const segmentPaths = await getServerSegmentPathsUntilPage(`${baseURL}`, path); + + expect(segmentPaths).not.toContain(path); + }); +}); + +// Routes that look like build output. They also show that the helper sees sent segments, so the tests above cannot +// pass by accident. +['/users/jane.doe', '/users/_build/'].forEach(path => { + test(`sends a server span for the route ${path}`, async ({ baseURL }) => { + const segmentPaths = await getServerSegmentPathsUntilPage(`${baseURL}`, path); + + expect(segmentPaths).toContain(path); + }); +}); + +/** + * Requests `path`, then a page. Returns the `url.path` of each server segment up to the page's segment, which arrives + * after any segment for `path`. + */ +async function getServerSegmentPathsUntilPage(baseURL: string, path: string): Promise { + const pagePath = `/users/after${path.replace(/\W+/g, '-')}`; + const segmentPaths: string[] = []; + const pageSegmentPromise = waitForStreamedSpans('solidstart', spans => { + segmentPaths.push(...spans.filter(span => span.is_segment).map(span => `${span.attributes[URL_PATH]?.value}`)); + return segmentPaths.includes(pagePath); + }); + + await fetch(`${baseURL}${path}`); + await fetch(`${baseURL}${pagePath}`); + await pageSegmentPromise; + + return segmentPaths; +} diff --git a/packages/solidstart/src/server/sdk.ts b/packages/solidstart/src/server/sdk.ts index 883d2a0ef63f..0696131adc41 100644 --- a/packages/solidstart/src/server/sdk.ts +++ b/packages/solidstart/src/server/sdk.ts @@ -1,7 +1,14 @@ +import { URL_PATH } from '@sentry/conventions/attributes'; +import { HTTP_SERVER } from '@sentry/conventions/op'; import { applySdkMetadata } from '@sentry/core'; import type { NodeClient, NodeOptions } from '@sentry/node'; import { init as initNodeSdk } from '@sentry/node'; -import { filterLowQualityTransactions } from './utils'; + +/** + * SolidStart serves its client build under `/_build/`. Matches `url.path`, because a span name has no path yet when + * `ignoreSpans` runs. + */ +const BUILD_ASSET_REQUEST_FILTER = { op: HTTP_SERVER, attributes: { [URL_PATH]: /^\/_build\// } }; /** * Initializes the server side of the Solid Start SDK @@ -9,10 +16,10 @@ import { filterLowQualityTransactions } from './utils'; export function init(options: NodeOptions): NodeClient | undefined { const opts = { ...options, + ignoreSpans: [...(options.ignoreSpans || []), BUILD_ASSET_REQUEST_FILTER], }; applySdkMetadata(opts, 'solidstart', ['solidstart', 'node']); - filterLowQualityTransactions(opts); return initNodeSdk(opts); } diff --git a/packages/solidstart/src/server/utils.ts b/packages/solidstart/src/server/utils.ts index 8276c32da9e0..56353ab5c421 100644 --- a/packages/solidstart/src/server/utils.ts +++ b/packages/solidstart/src/server/utils.ts @@ -1,7 +1,3 @@ -import type { EventProcessor, Options } from '@sentry/core'; -import { debug } from '@sentry/core'; -import { getGlobalScope } from '@sentry/node'; - /** * Determines if a thrown "error" is a redirect Response which Solid Start users can throw to redirect to another route. * see: https://docs.solidjs.com/solid-router/reference/data-apis/response-helpers#redirect @@ -16,33 +12,3 @@ export function isRedirect(error: unknown): boolean { const hasValidStatus = error.status >= 300 && error.status <= 308; return hasValidLocation && hasValidStatus; } - -/** - * Filter function for low quality transactions - * - * Exported only for tests - */ -export function lowQualityTransactionsFilter(options: Options): EventProcessor { - return Object.assign( - (event => { - if (event.type !== 'transaction') { - return event; - } - // Filter out transactions for build assets - if (event.transaction?.match(/^GET \/_build\//)) { - options.debug && debug.log('SolidStartLowQualityTransactionsFilter filtered transaction', event.transaction); - return null; - } - return event; - }) satisfies EventProcessor, - { id: 'SolidStartLowQualityTransactionsFilter' }, - ); -} - -/** - * Adds an event processor to filter out low quality transactions, - * e.g. to filter out transactions for build assets - */ -export function filterLowQualityTransactions(options: Options): void { - getGlobalScope().addEventProcessor(lowQualityTransactionsFilter(options)); -} diff --git a/packages/solidstart/test/server/sdk.test.ts b/packages/solidstart/test/server/sdk.test.ts index 067e7a6de818..6adce94df494 100644 --- a/packages/solidstart/test/server/sdk.test.ts +++ b/packages/solidstart/test/server/sdk.test.ts @@ -1,9 +1,9 @@ -import type { EventProcessor } from '@sentry/core'; -import { getGlobalScope, Scope, SDK_VERSION } from '@sentry/node'; +import { SENTRY_OP, URL_PATH } from '@sentry/conventions/attributes'; +import { HTTP_SERVER } from '@sentry/conventions/op'; +import { SDK_VERSION } from '@sentry/node'; import * as SentryNode from '@sentry/node'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { init as solidStartInit } from '../../src/server'; -import { lowQualityTransactionsFilter } from '../../src/server/utils'; const browserInit = vi.spyOn(SentryNode, 'init'); @@ -36,52 +36,41 @@ describe('Initialize Solid Start SDK', () => { expect(browserInit).toHaveBeenLastCalledWith(expect.objectContaining(expectedMetadata)); }); - describe('lowQualityTransactionsFilter', () => { - const options = { debug: false }; - const filter = lowQualityTransactionsFilter(options); + describe('ignoreSpans', () => { + const startServerSpan = (path: string): SentryNode.Span => + SentryNode.startInactiveSpan({ + name: 'GET', + forceTransaction: true, + attributes: { [SENTRY_OP]: HTTP_SERVER, [URL_PATH]: path }, + }); - describe('filters out low quality transactions', () => { - it.each(['GET /_build/some_asset.js', 'GET /_build/app.js', 'GET /_build/assets/logo.png'])( - 'filters out low quality transaction: (%s)', - transaction => { - const event = { type: 'transaction' as const, transaction }; - expect(filter(event, {})).toBeNull(); - }, - ); - }); + it.each(['/_build/assets/app.js', '/_build/assets/app.js.map', '/_build/.vite/manifest.json'])( + 'ignores build asset requests (%s)', + path => { + solidStartInit({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }); - describe('keeps high quality transactions', () => { - it.each(['GET /', 'POST /_server'])('does not filter out route transactions (%s)', transaction => { - const event = { type: 'transaction' as const, transaction }; - expect(filter(event, {})).toEqual(event); - }); - }); + expect(startServerSpan(path).isRecording()).toBe(false); + }, + ); - it('does not filter non-transaction events', () => { - const event = { type: 'error' as const, transaction: 'GET /_build/app.js' } as any; - expect(filter(event, {})).toEqual(event); - }); + it.each(['/', '/_server', '/users/jane.doe', '/api/data.json', '/blog/_build/'])( + 'keeps route requests (%s)', + path => { + solidStartInit({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }); - it('handles events without transaction property', () => { - const event = { type: 'transaction' as const }; - expect(filter(event, {})).toEqual(event); - }); - }); + expect(startServerSpan(path).isRecording()).toBe(true); + }, + ); - it('registers an event processor', () => { - let passedEventProcessors: EventProcessor[] = []; - const addEventProcessor = vi - .spyOn(getGlobalScope(), 'addEventProcessor') - .mockImplementation((eventProcessor: EventProcessor) => { - passedEventProcessors = [...passedEventProcessors, eventProcessor]; - return new Scope(); + it('merges the default with user-provided ignoreSpans', () => { + solidStartInit({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + tracesSampleRate: 1, + ignoreSpans: [{ attributes: { [URL_PATH]: '/health' } }], }); - solidStartInit({ - dsn: 'https://public@dsn.ingest.sentry.io/1337', + expect(startServerSpan('/health').isRecording()).toBe(false); + expect(startServerSpan('/_build/assets/app.js.map').isRecording()).toBe(false); }); - - expect(addEventProcessor).toHaveBeenCalledTimes(1); - expect(passedEventProcessors[0]?.id).toEqual('SolidStartLowQualityTransactionsFilter'); }); }); From 83104a1df1b16e1053f8a6dc08e83cbb6b6af07d Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 24 Nov 2026 10:55:58 +0100 Subject: [PATCH 2/3] fix(solidstart): Ignore build output spans under a base path The filter matched only `/_build/`, so with a base path the SDK still sent spans for build files. It now reads the base path: from the build on SolidStart 2, and from `instrument.server.mjs` on SolidStart 1, which `node --import` loads before the server bundle sets it. --- .../tests/build-assets.test.ts | 27 +++++++++++++ .../tests/build-assets.test.ts | 27 +++++++++++++ packages/solidstart/src/common/tunnel.ts | 18 ++++++--- packages/solidstart/src/server/sdk.ts | 22 +++++++---- .../src/vite/buildInstrumentationFile.ts | 19 +++++++-- .../src/vite/sentrySolidStartVite.ts | 2 +- .../solidstart/test/common/tunnel.test.ts | 25 +++++++++++- packages/solidstart/test/server/sdk.test.ts | 39 ++++++++++++++++++- .../test/vite/buildInstrumentation.test.ts | 27 +++++++++++++ 9 files changed, 187 insertions(+), 19 deletions(-) create mode 100644 dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts create mode 100644 dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts diff --git a/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts b/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts new file mode 100644 index 000000000000..719c675a7579 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts @@ -0,0 +1,27 @@ +import { expect, test } from '@playwright/test'; +import { getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; + +const BUILD_FILE_PATH = '/app/_build/assets/entry.js.map'; +const PAGE_PATH = '/app/after-build-file'; + +// In development, the server SDK does not know the base path when it initializes. +test.skip(process.env.TEST_ENV === 'development', 'Production only'); + +test('does not send a server span for a build file under the base path', async ({ baseURL }) => { + const segmentPaths: string[] = []; + const pageSegmentPromise = waitForStreamedSpans('solidstart-1-tunnel', spans => { + segmentPaths.push( + ...spans + .filter(span => span.is_segment && getSpanOp(span) === 'http.server') + .map(span => `${span.attributes['url.path']?.value}`), + ); + return segmentPaths.includes(PAGE_PATH); + }); + + await fetch(`${baseURL}${BUILD_FILE_PATH}`); + // A span for the build file would arrive before the span of this later request. + await fetch(`${baseURL}${PAGE_PATH}`); + await pageSegmentPromise; + + expect(segmentPaths).not.toContain(BUILD_FILE_PATH); +}); diff --git a/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts b/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts new file mode 100644 index 000000000000..c1ddc5670925 --- /dev/null +++ b/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts @@ -0,0 +1,27 @@ +import { expect, test } from '@playwright/test'; +import { getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; + +const BUILD_FILE_PATH = '/app/_build/assets/entry.js.map'; +const PAGE_PATH = '/app/after-build-file'; + +// In development, the server SDK does not know the base path when it initializes. +test.skip(process.env.TEST_ENV === 'development', 'Production only'); + +test('does not send a server span for a build file under the base path', async ({ baseURL }) => { + const segmentPaths: string[] = []; + const pageSegmentPromise = waitForStreamedSpans('solidstart-2-tunnel', spans => { + segmentPaths.push( + ...spans + .filter(span => span.is_segment && getSpanOp(span) === 'http.server') + .map(span => `${span.attributes['url.path']?.value}`), + ); + return segmentPaths.includes(PAGE_PATH); + }); + + await fetch(`${baseURL}${BUILD_FILE_PATH}`); + // A span for the build file would arrive before the span of this later request. + await fetch(`${baseURL}${PAGE_PATH}`); + await pageSegmentPromise; + + expect(segmentPaths).not.toContain(BUILD_FILE_PATH); +}); diff --git a/packages/solidstart/src/common/tunnel.ts b/packages/solidstart/src/common/tunnel.ts index 16f846a83dd0..9407074c5e2e 100644 --- a/packages/solidstart/src/common/tunnel.ts +++ b/packages/solidstart/src/common/tunnel.ts @@ -1,17 +1,25 @@ import type { Options } from '@sentry/core'; -import { _INTERNAL_applyBrowserTunnelDefaults as applyBrowserTunnelDefaults } from '@sentry/core'; +import { _INTERNAL_applyBrowserTunnelDefaults as applyBrowserTunnelDefaults, GLOBAL_OBJ } from '@sentry/core'; -/** Set on the server by `withSentry` to the app's `server.baseURL`. */ +/** Holds the app's `server.baseURL` on SolidStart 1 servers. Set by the server bundle and the `instrument.server` file. */ export const TUNNEL_BASE_PATH_GLOBAL = '__sentry_solidstart_base_path'; declare const __SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__: string | undefined; +/** + * Returns the app's base path without a trailing slash. The build defines it where it bundles the SDK: in the browser + * and in the SolidStart 2 server. The SolidStart 1 server loads the SDK as an external module and reads the global. + */ +export function getBasePath(): string { + return typeof __SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__ !== 'undefined' + ? __SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__ + : ((GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] ?? ''); +} + /** * Serves the browser's managed tunnel under the app's base path, which `withSentry` (SolidStart 1) or * `sentrySolidStart` (SolidStart 2) defines at build time. A `basePath` the user set wins. */ export function applyTunnelBasePath(options: Pick): void { - const basePath = - typeof __SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__ !== 'undefined' ? __SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__ : ''; - options.tunnel = applyBrowserTunnelDefaults(options.tunnel, { basePath }); + options.tunnel = applyBrowserTunnelDefaults(options.tunnel, { basePath: getBasePath() }); } diff --git a/packages/solidstart/src/server/sdk.ts b/packages/solidstart/src/server/sdk.ts index 9fa14344b7ef..bbd7e4ecdbe6 100644 --- a/packages/solidstart/src/server/sdk.ts +++ b/packages/solidstart/src/server/sdk.ts @@ -1,14 +1,9 @@ import { URL_PATH } from '@sentry/conventions/attributes'; import { HTTP_SERVER } from '@sentry/conventions/op'; -import { _INTERNAL_captureOptionsSetByUser, applySdkMetadata } from '@sentry/core'; +import { _INTERNAL_captureOptionsSetByUser, applySdkMetadata, escapeStringForRegex } from '@sentry/core'; import type { NodeClient, NodeOptions } from '@sentry/node'; import { init as initNodeSdk } from '@sentry/node'; - -/** - * SolidStart serves its client build under `/_build/`. Matches `url.path`, because a span name has no path yet when - * `ignoreSpans` runs. - */ -const BUILD_ASSET_REQUEST_FILTER = { op: HTTP_SERVER, attributes: { [URL_PATH]: /^\/_build\// } }; +import { getBasePath } from '../common/tunnel'; /** * Initializes the server side of the Solid Start SDK @@ -16,10 +11,21 @@ const BUILD_ASSET_REQUEST_FILTER = { op: HTTP_SERVER, attributes: { [URL_PATH]: export function init(options: NodeOptions): NodeClient | undefined { const opts = { ..._INTERNAL_captureOptionsSetByUser(options), - ignoreSpans: [...(options.ignoreSpans || []), BUILD_ASSET_REQUEST_FILTER], + ignoreSpans: [...(options.ignoreSpans || []), getBuildAssetRequestFilter()], }; applySdkMetadata(opts, 'solidstart', ['solidstart', 'node']); return initNodeSdk(opts); } + +/** + * SolidStart serves its client build under `/_build/`. Matches `url.path`, because a span name has no path + * yet when `ignoreSpans` runs. + */ +function getBuildAssetRequestFilter(): NonNullable[number] { + const basePath = getBasePath(); + // A full URL as the base (a CDN) is not part of the request path. + const pathPrefix = basePath.includes('://') ? '' : basePath; + return { op: HTTP_SERVER, attributes: { [URL_PATH]: new RegExp(`^${escapeStringForRegex(pathPrefix)}/_build/`) } }; +} diff --git a/packages/solidstart/src/vite/buildInstrumentationFile.ts b/packages/solidstart/src/vite/buildInstrumentationFile.ts index 6fbbb60e1615..fe0a6412bdf2 100644 --- a/packages/solidstart/src/vite/buildInstrumentationFile.ts +++ b/packages/solidstart/src/vite/buildInstrumentationFile.ts @@ -1,18 +1,30 @@ -import { consoleSandbox } from '@sentry/core'; +import { consoleSandbox, stringifyForCodeInjection } from '@sentry/core'; import * as fs from 'fs'; import * as path from 'path'; import type { Plugin, UserConfig } from 'vite'; +import { TUNNEL_BASE_PATH_GLOBAL } from '../common/tunnel'; import type { SentrySolidStartPluginOptions } from './types'; /** * A Sentry plugin for SolidStart to build the server * `instrument.server.ts` file. */ -export function makeBuildInstrumentationFilePlugin(options: SentrySolidStartPluginOptions = {}): Plugin { +export function makeBuildInstrumentationFilePlugin(options: SentrySolidStartPluginOptions = {}, basePath = ''): Plugin { + let instrumentationFile: string | undefined; + return { name: 'sentry-solidstart-build-instrumentation-file', apply: 'build', enforce: 'post', + // `node --import` loads this file before the server bundle, so the file sets the base path for `Sentry.init` itself. + banner(chunk) { + // Vite reports module IDs with forward slashes, also on Windows. + const isInstrumentationFile = + !!chunk.facadeModuleId && path.resolve(chunk.facadeModuleId) === instrumentationFile; + return basePath && isInstrumentationFile + ? `globalThis.${TUNNEL_BASE_PATH_GLOBAL} = ${stringifyForCodeInjection(basePath)};` + : ''; + }, async config(config: UserConfig, { command }) { const instrumentationFilePath = options.instrumentation || './src/instrument.server.ts'; const router = (config as UserConfig & { router: { target: string; name: string; root: string } }).router; @@ -42,7 +54,8 @@ export function makeBuildInstrumentationFilePlugin(options: SentrySolidStartPlug return config; } - input.push(path.resolve(router.root, instrumentationFilePath)); + instrumentationFile = path.resolve(router.root, instrumentationFilePath); + input.push(instrumentationFile); return { ...config, diff --git a/packages/solidstart/src/vite/sentrySolidStartVite.ts b/packages/solidstart/src/vite/sentrySolidStartVite.ts index 449855d95c0b..443934ebb22e 100644 --- a/packages/solidstart/src/vite/sentrySolidStartVite.ts +++ b/packages/solidstart/src/vite/sentrySolidStartVite.ts @@ -26,7 +26,7 @@ export const sentrySolidStartVite = ( ]; if (options.autoInjectServerSentry !== 'experimental_dynamic-import') { - sentryPlugins.push(makeBuildInstrumentationFilePlugin(options)); + sentryPlugins.push(makeBuildInstrumentationFilePlugin(options, tunnelBasePath)); } if (process.env.NODE_ENV !== 'development') { diff --git a/packages/solidstart/test/common/tunnel.test.ts b/packages/solidstart/test/common/tunnel.test.ts index 25263d12d950..f70d9c9d91c4 100644 --- a/packages/solidstart/test/common/tunnel.test.ts +++ b/packages/solidstart/test/common/tunnel.test.ts @@ -1,5 +1,28 @@ +import { GLOBAL_OBJ } from '@sentry/core'; import { afterEach, describe, expect, it, vi } from 'vitest'; -import { applyTunnelBasePath } from '../../src/common/tunnel'; +import { applyTunnelBasePath, getBasePath, TUNNEL_BASE_PATH_GLOBAL } from '../../src/common/tunnel'; + +describe('getBasePath', () => { + afterEach(() => { + vi.unstubAllGlobals(); + (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] = undefined; + }); + + it('prefers the defined base path over the global', () => { + vi.stubGlobal('__SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__', '/app'); + (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] = '/other'; + + expect(getBasePath()).toBe('/app'); + }); + + it('falls back to the global, then to no base path', () => { + expect(getBasePath()).toBe(''); + + (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] = '/app'; + + expect(getBasePath()).toBe('/app'); + }); +}); describe('applyTunnelBasePath', () => { afterEach(() => { diff --git a/packages/solidstart/test/server/sdk.test.ts b/packages/solidstart/test/server/sdk.test.ts index ee008a4ff1ae..38b19053c4af 100644 --- a/packages/solidstart/test/server/sdk.test.ts +++ b/packages/solidstart/test/server/sdk.test.ts @@ -2,7 +2,9 @@ import { SENTRY_OP, URL_PATH } from '@sentry/conventions/attributes'; import { HTTP_SERVER } from '@sentry/conventions/op'; import { SDK_VERSION } from '@sentry/node'; import * as SentryNode from '@sentry/node'; -import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { GLOBAL_OBJ } from '@sentry/core'; +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import { TUNNEL_BASE_PATH_GLOBAL } from '../../src/common/tunnel'; import { init as solidStartInit } from '../../src/server'; const browserInit = vi.spyOn(SentryNode, 'init'); @@ -73,5 +75,40 @@ describe('Initialize Solid Start SDK', () => { expect(startServerSpan('/health').isRecording()).toBe(false); expect(startServerSpan('/_build/assets/app.js.map').isRecording()).toBe(false); }); + + describe('with a base path', () => { + afterEach(() => { + vi.unstubAllGlobals(); + (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] = undefined; + }); + + it('ignores build asset requests under the defined base path (SolidStart 2)', () => { + vi.stubGlobal('__SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__', '/app'); + + solidStartInit({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }); + + expect(startServerSpan('/app/_build/assets/app.js.map').isRecording()).toBe(false); + expect(startServerSpan('/_build/assets/app.js.map').isRecording()).toBe(true); + expect(startServerSpan('/app/users/_build/').isRecording()).toBe(true); + }); + + it('ignores build asset requests under the base path global (SolidStart 1)', () => { + (GLOBAL_OBJ as { [TUNNEL_BASE_PATH_GLOBAL]?: string })[TUNNEL_BASE_PATH_GLOBAL] = '/shop.v2'; + + solidStartInit({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }); + + expect(startServerSpan('/shop.v2/_build/assets/app.js.map').isRecording()).toBe(false); + expect(startServerSpan('/shopxv2/_build/assets/app.js.map').isRecording()).toBe(true); + }); + + it('ignores build asset requests at the root when the base is a full URL', () => { + // `normalizeTunnelBasePath` turns `base: 'https://cdn.example.com/'` into this. + vi.stubGlobal('__SENTRY_SOLIDSTART_TUNNEL_BASE_PATH__', '/https://cdn.example.com'); + + solidStartInit({ dsn: 'https://public@dsn.ingest.sentry.io/1337', tracesSampleRate: 1 }); + + expect(startServerSpan('/_build/assets/app.js.map').isRecording()).toBe(false); + }); + }); }); }); diff --git a/packages/solidstart/test/vite/buildInstrumentation.test.ts b/packages/solidstart/test/vite/buildInstrumentation.test.ts index 465f2801da55..727fbaa98449 100644 --- a/packages/solidstart/test/vite/buildInstrumentation.test.ts +++ b/packages/solidstart/test/vite/buildInstrumentation.test.ts @@ -136,6 +136,33 @@ describe('makeBuildInstrumentationFilePlugin()', () => { expect(config).toEqual(viteConfig); }); + describe('banner', () => { + type BannerHook = (chunk: { facadeModuleId: string | null }) => string; + + async function getBanner(basePath?: string): Promise { + const plugin = makeBuildInstrumentationFilePlugin({}, basePath); + // @ts-expect-error - this is always defined and always a function + await plugin.config(viteConfig, { command: 'build' }); + return plugin.banner as BannerHook; + } + + it('sets the base path in the instrumentation file, which `node --import` loads before the server', async () => { + const banner = await getBanner('/app'); + + expect(banner({ facadeModuleId: '/some/project/path/src/instrument.server.ts' })).toBe( + 'globalThis.__sentry_solidstart_base_path = "/app";', + ); + expect(banner({ facadeModuleId: '/path/to/entry1.js' })).toBe(''); + expect(banner({ facadeModuleId: null })).toBe(''); + }); + + it('adds nothing without a base path', async () => { + const banner = await getBanner(); + + expect(banner({ facadeModuleId: '/some/project/path/src/instrument.server.ts' })).toBe(''); + }); + }); + it("logs a warning if the instrumentation file doesn't exist", async () => { const error = new Error("File doesn't exist."); fsAccessMock.mockRejectedValueOnce(error); From 529ba2a98eae2df8525662c1b25f377a663d0af0 Mon Sep 17 00:00:00 2001 From: s1gr1d <32902192+s1gr1d@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:44:38 +0100 Subject: [PATCH 3/3] test(solidstart): Explain why the tunnel apps test build files --- .../solidstart-1-tunnel/tests/build-assets.test.ts | 1 + .../solidstart-2-tunnel/tests/build-assets.test.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts b/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts index 719c675a7579..8c08e841fa29 100644 --- a/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts +++ b/dev-packages/e2e-tests/test-applications/solidstart-1-tunnel/tests/build-assets.test.ts @@ -1,6 +1,7 @@ import { expect, test } from '@playwright/test'; import { getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; +// This app runs under a base path (`/app/`), so it also tests the build file filter with a base path. const BUILD_FILE_PATH = '/app/_build/assets/entry.js.map'; const PAGE_PATH = '/app/after-build-file'; diff --git a/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts b/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts index c1ddc5670925..b651f5a1adae 100644 --- a/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts +++ b/dev-packages/e2e-tests/test-applications/solidstart-2-tunnel/tests/build-assets.test.ts @@ -1,6 +1,7 @@ import { expect, test } from '@playwright/test'; import { getSpanOp, waitForStreamedSpans } from '@sentry-internal/test-utils'; +// This app runs under a base path (`/app/`), so it also tests the build file filter with a base path. const BUILD_FILE_PATH = '/app/_build/assets/entry.js.map'; const PAGE_PATH = '/app/after-build-file';