Skip to content

Commit f2deca0

Browse files
committed
bun adjustments
1 parent 6d29d45 commit f2deca0

2 files changed

Lines changed: 60 additions & 3 deletions

File tree

packages/server-utils/src/orchestrion/bundler/bun.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import codeTransformer from '@apm-js-collab/code-transformer-bundler-plugins/bun';
2-
import { INSTRUMENTED_MODULE_NAMES, withoutInstrumentedExternals } from '../config';
2+
import { instrumentedModuleNames, withoutInstrumentedExternals } from '../config';
33
import { ORCHESTRION_BUNDLER_MARKER_BANNER } from './moduleInjectedTransform';
44
import type { PluginOptions } from './options';
55
import { orchestrionTransformOptions } from './options';
@@ -48,6 +48,10 @@ export function sentryOrchestrionPlugin(options: PluginOptions = {}): UnknownPlu
4848
setup: (build: BunPluginBuilder) => void;
4949
};
5050

51+
// Custom `instrumentations` must be bundled too, otherwise those packages can stay externalized
52+
// and their transform never runs, so fold them into the names we un-externalize and warn about.
53+
const moduleNames = instrumentedModuleNames(options.instrumentations);
54+
5155
return {
5256
name: 'sentry-orchestrion',
5357
setup(build: BunPluginBuilder): void {
@@ -64,7 +68,7 @@ export function sentryOrchestrionPlugin(options: PluginOptions = {}): UnknownPlu
6468
// `node_modules` at runtime and never passes through the transform's `onLoad`, so its
6569
// diagnostics_channel calls would be silently never injected. Bun has no runtime fallback
6670
// here, so bundling is the only injection path.
67-
build.config.external = withoutInstrumentedExternals(build.config.external);
71+
build.config.external = withoutInstrumentedExternals(build.config.external, moduleNames);
6872

6973
// A blanket externalization strategy like `packages: 'external'` or `'*'` in `external`
7074
// externalizes instrumented packages too, and `withoutInstrumentedExternals` only strips
@@ -84,7 +88,7 @@ export function sentryOrchestrionPlugin(options: PluginOptions = {}): UnknownPlu
8488
console.warn(
8589
`[Sentry] This Bun build externalizes all dependencies (${blanketExternal}), so Sentry ` +
8690
'cannot instrument bundled libraries. Instrumentation will be missing for any of ' +
87-
`these packages your app uses: ${INSTRUMENTED_MODULE_NAMES.join(', ')}. To instrument them, ` +
91+
`these packages your app uses: ${moduleNames.join(', ')}. To instrument them, ` +
8892
'externalize only the specific packages you need external instead of all of them.',
8993
);
9094
}

packages/server-utils/test/orchestrion/bundler.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,59 @@ describe('sentryOrchestrionPlugin (vite)', () => {
257257
});
258258
});
259259

260+
describe('sentryOrchestrionPlugin (bun)', () => {
261+
type BunConfig = { banner?: string; external?: string[]; packages?: 'bundle' | 'external' };
262+
263+
function runSetup(config: BunConfig, options?: Parameters<typeof bunPlugin>[0]): BunConfig {
264+
const build = { config };
265+
bunPlugin(options).setup(build);
266+
return build.config;
267+
}
268+
269+
it('strips instrumented modules from external so they get bundled and transformed', () => {
270+
const config = runSetup({ external: ['mysql', 'some-other-package'] });
271+
272+
expect(config.external).toEqual(['some-other-package']);
273+
});
274+
275+
it('strips custom instrumentations passed via options from external', () => {
276+
const config = runSetup(
277+
{ external: ['my-custom-lib', 'some-other-package'] },
278+
{
279+
instrumentations: [
280+
{
281+
channelName: 'x',
282+
module: { name: 'my-custom-lib', versionRange: '*', filePath: 'index.js' },
283+
functionQuery: { expressionName: 'x', kind: 'Sync' },
284+
},
285+
],
286+
},
287+
);
288+
289+
expect(config.external).toEqual(['some-other-package']);
290+
});
291+
292+
it('warns and names custom instrumentations for a blanket external strategy', () => {
293+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
294+
295+
runSetup(
296+
{ packages: 'external' },
297+
{
298+
instrumentations: [
299+
{
300+
channelName: 'x',
301+
module: { name: 'my-custom-lib', versionRange: '*', filePath: 'index.js' },
302+
functionQuery: { expressionName: 'x', kind: 'Sync' },
303+
},
304+
],
305+
},
306+
);
307+
308+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('my-custom-lib'));
309+
warn.mockRestore();
310+
});
311+
});
312+
260313
describe('buildTimeInstrumentation: false', () => {
261314
const disabled = { buildTimeInstrumentation: false };
262315

0 commit comments

Comments
 (0)