-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(node): Vendor InstrumentationNodeModuleFile to fix Bun --bytecode crash
#21262
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.
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8b187e8
fix(node): Vendor `InstrumentationNodeModuleFile` to fix Bun ``--byte…
s1gr1d 579f19c
add tests
s1gr1d 9eb38b5
add bun
s1gr1d d6e1174
add oxlint disable for vendored code
s1gr1d 045075c
remove optional
s1gr1d 17724a0
fix test
s1gr1d File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
dev-packages/e2e-tests/test-applications/bun-bytecode/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "name": "bun-bytecode-test", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "scripts": { | ||
| "clean": "npx rimraf node_modules pnpm-lock.yaml dist", | ||
| "test:build": "pnpm install && bun build src/main.ts --compile --bytecode --format=esm --outfile dist/main", | ||
| "test:assert": "dist/main" | ||
| }, | ||
| "dependencies": { | ||
| "@sentry/bun": "file:../../packed/sentry-bun-packed.tgz" | ||
| }, | ||
| "devDependencies": { | ||
| "bun-types": "^1.2.9" | ||
| }, | ||
| "volta": { | ||
| "extends": "../../package.json" | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
dev-packages/e2e-tests/test-applications/bun-bytecode/src/main.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /** | ||
| * Verifies that @sentry/bun can be compiled with `bun build --compile --bytecode` without crashing at runtime. | ||
| */ | ||
| import * as Sentry from '@sentry/bun'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://username@domain/123', | ||
| tracesSampleRate: 0, | ||
| }); | ||
|
|
||
| console.log('Bun bytecode compilation: OK'); |
10 changes: 10 additions & 0 deletions
10
dev-packages/e2e-tests/test-applications/bun-bytecode/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "types": ["bun-types"], | ||
| "strict": true, | ||
| "module": "ESNext", | ||
| "moduleResolution": "bundler", | ||
| "target": "ESNext" | ||
| }, | ||
| "include": ["src"] | ||
| } |
45 changes: 45 additions & 0 deletions
45
packages/node/src/integrations/tracing/InstrumentationNodeModuleFile.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| * NOTICE from the Sentry authors: | ||
| * - Vendored locally from @opentelemetry/instrumentation to work around a Bun | ||
| * --bytecode bug (https://github.com/getsentry/sentry-javascript/issues/21256). | ||
| * - The upstream class imports `normalize` via an indirect platform/index | ||
| * re-export chain, which Bun's bytecode bundler renames and loses scope for. | ||
| * - This copy imports `normalize` directly from 'path' to break that chain. | ||
| */ | ||
| /* oxlint-disable */ | ||
|
|
||
| import { normalize } from 'path'; | ||
| import type { InstrumentationModuleFile } from '@opentelemetry/instrumentation'; | ||
|
|
||
| export class InstrumentationNodeModuleFile implements InstrumentationModuleFile { | ||
| public name: string; | ||
| public supportedVersions: string[]; | ||
| public patch: (moduleExports: any, moduleVersion?: string) => any; | ||
| public unpatch: (moduleExports?: any, moduleVersion?: string) => void; | ||
|
|
||
| constructor( | ||
| name: string, | ||
| supportedVersions: string[], | ||
| patch: (moduleExports: any, moduleVersion?: string) => any, | ||
| unpatch: (moduleExports?: any, moduleVersion?: string) => void, | ||
| ) { | ||
| this.name = normalize(name); | ||
| this.supportedVersions = supportedVersions; | ||
| this.patch = patch; | ||
| this.unpatch = unpatch; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 2 additions & 6 deletions
8
packages/node/src/integrations/tracing/firebase/otel/patches/firestore.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 2 additions & 6 deletions
8
packages/node/src/integrations/tracing/firebase/otel/patches/functions.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 2 additions & 5 deletions
7
packages/node/src/integrations/tracing/google-genai/instrumentation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/node/test/integrations/tracing/instrumentationNodeModuleFile.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import * as fs from 'fs'; | ||
| import * as path from 'path'; | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| const TRACING_DIR = path.resolve(__dirname, '../../../src/integrations/tracing'); | ||
|
|
||
| /** | ||
| * Importing InstrumentationNodeModuleFile from @opentelemetry/instrumentation causes | ||
| * Bun's --bytecode compiler to inline a re-export chain that loses scope bindings. | ||
| * All instrumentations must use the local vendored copy instead. | ||
| */ | ||
| describe('InstrumentationNodeModuleFile import guard', () => { | ||
| it('no file should import InstrumentationNodeModuleFile from @opentelemetry/instrumentation', () => { | ||
| const offendingFiles: string[] = []; | ||
|
|
||
| function walkDir(dir: string): void { | ||
| const entries = fs.readdirSync(dir, { withFileTypes: true }); | ||
| for (const entry of entries) { | ||
| const fullPath = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) { | ||
| walkDir(fullPath); | ||
| } else if (entry.name.endsWith('.ts') && !entry.name.endsWith('.test.ts')) { | ||
| const content = fs.readFileSync(fullPath, 'utf-8'); | ||
| // Match multi-line imports: look for an import block from @opentelemetry/instrumentation | ||
| // that includes InstrumentationNodeModuleFile as a named import | ||
| const importBlockRegex = | ||
| /import\s*\{[^}]*InstrumentationNodeModuleFile[^}]*\}\s*from\s*['"]@opentelemetry\/instrumentation['"]/s; | ||
| if (importBlockRegex.test(content)) { | ||
| offendingFiles.push(path.relative(TRACING_DIR, fullPath)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| walkDir(TRACING_DIR); | ||
|
|
||
| expect(offendingFiles).toEqual([]); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.