Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions packages/cloudflare/src/vite/autoInstrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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';
import { applyAutoInstrumentTransforms, type BaseNode, type ClassWrapperKind, type ProgramBody } from './transform';
import { resolveWranglerConfig, type WranglerConfig } from './wranglerConfig';

// Vite normalizes module IDs to posix separators even on Windows, while
Expand Down Expand Up @@ -31,6 +31,20 @@ async function resolvesNextjsCloudflare(resolver: ModuleResolver, importer: stri
}
}

/**
* Whether the module re-exports everything from another module of its own file, as the shim
* `@sentry/tanstackstart-react` loads for the server entry does (`export * from "<entry>?sentry-tunnel-original"`).
* That module is transformed as the entry too, and its classes are wrapped there.
*/
function reexportsAllFromSameFile(ast: ProgramBody, normalizedId: string): boolean {
return ast.body.some(node => {
if (node.type !== 'ExportAllDeclaration') return false;
const { exported, source } = node as BaseNode & { exported?: unknown; source?: { value?: unknown } };
const value = source?.value;
return !exported && typeof value === 'string' && normalizePath(value.replace(/[?#].*$/, '')) === normalizedId;
});
}

type TransformContext = ModuleResolver & {
warn?(msg: string): void;
environment?: Parameters<ReturnType<typeof createIdResolver>>[0];
Expand Down Expand Up @@ -202,9 +216,9 @@ export function sentryCloudflareAutoInstrumentPlugin(options: { wranglerConfigPa

const wrappedClasses = result?.wrappedClasses ?? new Set<string>();
const missing = [...classWrappers.keys()].filter(name => !wrappedClasses.has(name));
if (missing.length > 0) {
if (missing.length > 0 && !reexportsAllFromSameFile(ast, normalizedId)) {
this.warn?.(
`[sentry] Could not auto-instrument ${missing.join(', ')}.` +
`[sentry] Could not auto-instrument ${missing.join(', ')}. ` +
'The worker entry has no export matching them. ' +
'Star re-exports (`export * from "./do"`) are not matched. ' +
'Export them by name or wrap them manually with corresponding `instrument*WithSentry` helpers.',
Expand Down
34 changes: 34 additions & 0 deletions packages/cloudflare/test/vite/autoInstrument.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,40 @@ describe('sentryCloudflareAutoInstrumentPlugin', () => {
expect(warnings).toHaveLength(1);
expect(warnings[0]).toContain('MyDO');
});

it('does not warn for a shim that re-exports everything from a query variant of the entry', async () => {
const dir = writeTempDir({
'wrangler.toml': [
'main = "index.ts"',
'',
'[[durable_objects.bindings]]',
'name = "MY_DO"',
'class_name = "MyDO"',
].join('\n'),
});
const plugin = sentryCloudflareAutoInstrumentPlugin();
plugin.configResolved({ root: dir });

const entry = join(dir, 'index.ts').replace(/\\/g, '/');
const original = JSON.stringify(`${entry}?sentry-tunnel-original`);
const warnings: string[] = [];
const ctx = { parse: (c: string) => parseJS(c), warn: (msg: string) => warnings.push(msg) };

const originalResult = await plugin.transform.call(
ctx,
'export class MyDO {}\nexport default { fetch() {} };',
`${entry}?sentry-tunnel-original`,
);
const shimResult = await plugin.transform.call(
ctx,
`export * from ${original};\nexport { default } from ${original};`,
entry,
);

expect(originalResult.code).toContain('instrumentDurableObjectWithSentry(');
expect(shimResult.code).toContain('_INTERNAL_wrapUnlessInstrumented(');
expect(warnings).toEqual([]);
});
});

describe('wranglerConfigPath option', () => {
Expand Down
Loading