GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cloudflare): auto-instrument when wrangler `main` is a package by isaacs · Pull Request #25343 · getsentry/sentry-javascript

fix(cloudflare): auto-instrument when wrangler main is a package #25343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
isaacs merged 5 commits into develop from isaacschlueter/js-3725-sentrycloudflarevite-auto-instrumentation-skips-the-worker
Oct 14, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

fix(cloudflare): auto-instrument when wrangler main is a package #25343

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
5 commits
Select commit Hold shift + click to select a range
1c3a46f
fix(cloudflare): auto-instrument when wrangler `main` is a package
isaacs Oct 14, 2026
103b2dd
fix: bugbot
isaacs Oct 14, 2026
2d9b239
fix(cloudflare): set the wrapped default export where its binding is set
isaacs Oct 14, 2026
1698a1d
fix(cloudflare): wrap the default export as it is at the end of the m…
isaacs Oct 14, 2026
99fc4b2
fix: bugbot finding
isaacs Oct 14, 2026
File filter
Filter by extension


Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

4 changes: 4 additions & 0 deletions 4 dev-packages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/.gitignore
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist
.wrangler
test-results
pnpm-lock.yaml
10 changes: 10 additions & 0 deletions 10 .../e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/instrument.server.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineCloudflareOptions } from '@sentry/cloudflare';

// The worker entry is a package, so the plugin reads this file from beside
// the wrangler config.
export default defineCloudflareOptions((env: { E2E_TEST_DSN: string }) => ({
dsn: env.E2E_TEST_DSN,
environment: 'qa',
tunnel: 'http://localhost:3031/',
tracesSampleRate: 1.0,
}));
29 changes: 29 additions & 0 deletions 29 ...packages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/package.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "cloudflare-autoinstrument-package-entry",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"build": "vite build",
"preview": "wrangler dev --var \"E2E_TEST_DSN:$E2E_TEST_DSN\" --log-level=$(test $CI && echo 'none' || echo 'log')",
"test": "playwright test",
"test:build": "pnpm install && pnpm build",
"test:assert": "pnpm test"
},
"dependencies": {
"@sentry/cloudflare": "file:../../packed/sentry-cloudflare-packed.tgz",
"worker-entry": "file:../../test-applications/cloudflare-autoinstrument-package-entry/worker-entry"
Comment thread
isaacs marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

},
"devDependencies": {
"@cloudflare/vite-plugin": "^1.47.0",
"@cloudflare/workers-types": "^5.20260727.1",
"@playwright/test": "~1.63.0",
"@sentry/conventions": "0.27.0",
"@sentry-internal/test-utils": "link:../../../test-utils",
"vite": "^8.1.5",
"wrangler": "^4.114.0"
},
"volta": {
"extends": "../../package.json"
}
}
12 changes: 12 additions & 0 deletions 12 .../e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/playwright.config.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { getPlaywrightConfig } from '@sentry-internal/test-utils';

export default getPlaywrightConfig(
{
startCommand: 'pnpm preview',
port: 8787,
},
{
workers: '100%',
retries: 0,
},
);
6 changes: 6 additions & 0 deletions 6 ...e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/start-event-proxy.mjs
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { startEventProxyServer } from '@sentry-internal/test-utils';

startEventProxyServer({
port: 3031,
proxyServerName: 'cloudflare-autoinstrument-package-entry',
});
35 changes: 35 additions & 0 deletions 35 ...sts/test-applications/cloudflare-autoinstrument-package-entry/tests/package-entry.test.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { expect, test } from '@playwright/test';
import { SENTRY_ENVIRONMENT, SENTRY_ORIGIN } from '@sentry/conventions/attributes';
import { HTTP_SERVER } from '@sentry/conventions/op';
import { getSpanOp, waitForError, waitForStreamedSpan } from '@sentry-internal/test-utils';

// The worker entry is a package with no Sentry calls. Any event that arrives
// proves the plugin wrapped the module that wrangler's `main` resolved to.

test('wraps a package entry with withSentry (options from instrument.server.ts)', async ({ baseURL }) => {
const spanPromise = waitForStreamedSpan(
'cloudflare-autoinstrument-package-entry',
span => getSpanOp(span) === HTTP_SERVER && span.is_segment,
);

const res = await fetch(baseURL!);
expect(res.status).toBe(200);

const span = await spanPromise;
expect(span.attributes[SENTRY_ORIGIN]?.value).toBe('auto.http.cloudflare');
// Only `instrument.server.ts` sets this environment.
expect(span.attributes[SENTRY_ENVIRONMENT]?.value).toBe('qa');
});

test('captures an error thrown by a package entry', async ({ baseURL }) => {
const errorPromise = waitForError(
'cloudflare-autoinstrument-package-entry',
event => event.exception?.values?.[0]?.value === 'Thrown from a package entry',
);

const res = await fetch(`${baseURL}/throw`);
expect(res.status).toBe(500);

const error = await errorPromise;
expect(error.environment).toBe('qa');
});
7 changes: 7 additions & 0 deletions 7 ...ckages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/vite.config.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { cloudflare } from '@cloudflare/vite-plugin';
import { sentryCloudflareVitePlugin } from '@sentry/cloudflare/vite';
import { defineConfig } from 'vite';

export default defineConfig({
plugins: [cloudflare(), sentryCloudflareVitePlugin()],
});
11 changes: 11 additions & 0 deletions 11 ...s/test-applications/cloudflare-autoinstrument-package-entry/worker-entry/fetch-handler.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Shaped like bundler output (e.g. `@astrojs/cloudflare`): the default export
// is a specifier, not `export default <expr>`.
var fetch_handler_default = {
async fetch(request) {
if (new URL(request.url).pathname === '/throw') {
throw new Error('Thrown from a package entry');
}
return new Response('ok');
},
};
export { fetch_handler_default as default };
9 changes: 9 additions & 0 deletions 9 ...tests/test-applications/cloudflare-autoinstrument-package-entry/worker-entry/package.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "worker-entry",
"version": "0.0.0",
"private": true,
"type": "module",
"exports": {
"./fetch-handler": "./fetch-handler.js"
}
}
8 changes: 8 additions & 0 deletions 8 ...ckages/e2e-tests/test-applications/cloudflare-autoinstrument-package-entry/wrangler.jsonc
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "cloudflare-autoinstrument-package-entry",
// A package export, not a file path. The Sentry plugin resolves it with Vite.
"main": "worker-entry/fetch-handler",
"compatibility_date": "2026-05-20",
"compatibility_flags": ["nodejs_compat"],
}
4 changes: 4 additions & 0 deletions 4 packages/cloudflare/package.json
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@
},
"peerDependencies": {
"@cloudflare/workers-types": "^4.x || ^5.x",
"vite": "^6.1.0 || ^7.0.0 || ^8.0.0",
"wrangler": "^4.x"
},
"peerDependenciesMeta": {
"@cloudflare/workers-types": {
"optional": true
},
"vite": {
"optional": true
},
"wrangler": {
"optional": true
}
Expand Down
86 changes: 64 additions & 22 deletions 86 packages/cloudflare/src/vite/autoInstrument.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { basename } from 'node:path';
import { basename, dirname, isAbsolute } from 'node:path';
import { createIdResolver, type ResolvedConfig } from 'vite';
import { collectAgentCandidates, detectAgentClasses, type ModuleResolver } from './agentClass';
import { buildOptionsImport, ENV_FALLBACK_OPTIONS_FN, resolveInstrumentFile } from './instrumentFile';
import { applyAutoInstrumentTransforms, type ClassWrapperKind, type ProgramBody } from './transform';
Expand Down Expand Up @@ -30,14 +31,48 @@ async function resolvesNextjsCloudflare(resolver: ModuleResolver, importer: stri
}
}

type TransformContext = ModuleResolver & {
warn?(msg: string): void;
environment?: Parameters<ReturnType<typeof createIdResolver>>[0];
};

export function sentryCloudflareAutoInstrumentPlugin(options: { wranglerConfigPath?: string } = {}) {
let wranglerConfig: WranglerConfig | undefined;
let entryFilePath: string | undefined;

let optionsFn = ENV_FALLBACK_OPTIONS_FN;
let optionsImport: string | undefined;
// A `main` that is not a file path (a package export or virtual module).
let entrySpecifier: string | undefined;
let wranglerConfigPath: string | undefined;
let resolveId: ReturnType<typeof createIdResolver> | undefined;
// Each environment resolves with its own conditions, so the entry may differ between them.
const resolvedEntries = new Map<string, Promise<string | undefined>>();

let instrumentFilePath: string | undefined;
let isVinext = false;

function resolveEntrySpecifier(ctx: TransformContext, specifier: string): Promise<string | undefined> {
const environment = ctx.environment;
if (!environment || !resolveId) return Promise.resolve(undefined);

let entry = resolvedEntries.get(environment.name);
if (!entry) {
// Resolved from the wrangler config, as wrangler does. Without an importer, Vite's resolver
// tries the specifier as a path below the root first.
entry = resolveId(environment, specifier, wranglerConfigPath)
.catch(() => undefined)
.then(id => {
if (!id) {
ctx.warn?.(
`[sentry] Could not resolve the wrangler \`main\` "${specifier}". Auto-instrumentation disabled.`,
);
return undefined;
}
return normalizePath(id.replace(/[?#].*$/, ''));
});
Comment thread
isaacs marked this conversation as resolved.
Resolved

Uh oh!

There was an error while loading. Please reload this page.

resolvedEntries.set(environment.name, entry);
}
return entry;
}

return {
name: 'sentry-cloudflare-auto-instrument',

Expand All @@ -64,40 +99,43 @@ export function sentryCloudflareAutoInstrumentPlugin(options: { wranglerConfigPa
}

wranglerConfig = result.config;
if (wranglerConfig.main) {
// `main` is already absolute (wrangler resolves it); just normalize
// separators so the entry-module comparison holds on Windows.
entryFilePath = normalizePath(wranglerConfig.main);
}

if (entryFilePath) {
const instrumentFilePath = resolveInstrumentFile(entryFilePath);
if (instrumentFilePath) {
const built = buildOptionsImport(entryFilePath, instrumentFilePath);
optionsFn = built.optionsFn;
optionsImport = built.importStmt;
}
const main = wranglerConfig.main;
if (main && isAbsolute(main)) {
// Normalize separators so the entry-module comparison holds on Windows.
entryFilePath = normalizePath(main);
instrumentFilePath = resolveInstrumentFile(dirname(main));
} else if (main) {
entrySpecifier = main;
wranglerConfigPath = result.configPath;
// Not `this.resolve()`: Rollup's commonjs resolver loads the module it resolves, so a
// resolve of the entry from the entry's own `transform` never finishes.
resolveId = createIdResolver(config as ResolvedConfig);
// A package entry has no project directory of its own.
instrumentFilePath = resolveInstrumentFile(result.configDir);
}
},

async transform(
this: ModuleResolver & { warn?(msg: string): void; environment?: { name?: string } },
this: TransformContext,
code: string,
id: string,
): Promise<{ code: string; map: unknown } | undefined> {
if (!wranglerConfig || !entryFilePath) return undefined;
if (!wranglerConfig || (!entryFilePath && !entrySpecifier)) return undefined;

// The worker entry never belongs to the client (browser) environment.
// Skipping it keeps a same-basename sibling (e.g. a `src/index.tsx`
// client entry next to a `src/index.ts` worker) out of the browser bundle.
if (this.environment?.name === 'client') return undefined;

const entryPath = entrySpecifier ? await resolveEntrySpecifier(this, entrySpecifier) : entryFilePath;
if (!entryPath) return undefined;

// Vite may append query/hash params to the module ID.
const normalizedId = normalizePath(id.replace(/[?#].*$/, ''));
if (normalizedId !== entryFilePath) {
if (normalizedId !== entryPath) {
// Tolerate a differing JS-flavored extension (e.g. `.js` vs `.ts`).
if (!JS_EXTENSION_REGEX.test(normalizedId) || !JS_EXTENSION_REGEX.test(entryFilePath)) return undefined;
if (normalizedId.replace(JS_EXTENSION_REGEX, '') !== entryFilePath.replace(JS_EXTENSION_REGEX, '')) {
if (!JS_EXTENSION_REGEX.test(normalizedId) || !JS_EXTENSION_REGEX.test(entryPath)) return undefined;
if (normalizedId.replace(JS_EXTENSION_REGEX, '') !== entryPath.replace(JS_EXTENSION_REGEX, '')) {
return undefined;
}
}
Expand Down Expand Up @@ -145,6 +183,10 @@ export function sentryCloudflareAutoInstrumentPlugin(options: { wranglerConfigPa
const sentryModule =
isVinext && (await resolvesNextjsCloudflare(this, normalizedId)) ? NEXTJS_CLOUDFLARE_MODULE : undefined;

const { optionsFn, importStmt: optionsImport } = instrumentFilePath
? buildOptionsImport(entryPath, instrumentFilePath)
: { optionsFn: ENV_FALLBACK_OPTIONS_FN, importStmt: undefined };

// No registration import is injected here: the orchestrion plugin's
// subscribe-injection makes each bundled package self-register its channel
// subscriber on the global marker, so wrapping the entry with `withSentry`
Expand Down
10 changes: 5 additions & 5 deletions 10 packages/cloudflare/src/vite/instrumentFile.ts
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ export const ENV_FALLBACK_OPTIONS_FN = '() => undefined';
const OPTIONS_IMPORT_IDENTIFIER = '__SENTRY_OPTIONS_CALLBACK__';

// Conventional, non-configurable name of the Sentry options module. It is
// looked up next to the worker entry file; its default export is the options
// looked up next to the worker entry file, or next to the wrangler config
// when `main` is not a file path; its default export is the options
// callback `(env) => CloudflareOptions`.
const INSTRUMENT_FILE_BASENAME = 'instrument.server';
const INSTRUMENT_FILE_EXTENSIONS = ['ts', 'mts', 'js', 'mjs', 'cjs'];

/**
* Locate the conventional `instrument.server.*` module sitting next to the
* worker entry file. Returns its absolute path, or `undefined` when absent.
* Locate the conventional `instrument.server.*` module in `dir`. Returns its
* absolute path, or `undefined` when absent.
*/
export function resolveInstrumentFile(entryFilePath: string): string | undefined {
const dir = dirname(entryFilePath);
export function resolveInstrumentFile(dir: string): string | undefined {
for (const ext of INSTRUMENT_FILE_EXTENSIONS) {
const candidate = resolve(dir, `${INSTRUMENT_FILE_BASENAME}.${ext}`);
if (existsSync(candidate)) return candidate;
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.