Repository navigation
feat(deno): Add modulesIntegration #26689
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
Open
sentry-junior
wants to merge
3
commits into
develop
Choose a base branch
from
feat/deno-modules-integration
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { fileURLToPath } from 'node:url'; | ||
| import type { IntegrationFn } from '@sentry/core'; | ||
| import { defineIntegration, dirname, join } from '@sentry/core'; | ||
| import { collectInstalledModules } from '@sentry/server-utils'; | ||
|
|
||
| /** Reads only with granted permission, so it never prompts. */ | ||
| function readFile(path?: string): string | undefined { | ||
| try { | ||
| if (Deno.permissions.querySync({ name: 'read', path }).state === 'granted') { | ||
| return path ? Deno.readTextFileSync(path) : Deno.cwd(); | ||
| } | ||
| } catch { | ||
| // not readable | ||
| } | ||
| return undefined; | ||
| } | ||
|
|
||
| /** Exact versions of the `jsr:` and `npm:` specifiers in the nearest `deno.lock` (versions 3 to 5). */ | ||
| function getDenoLockModules(startDir: string): Record<string, string> { | ||
| const modules: Record<string, string> = {}; | ||
| for (let dir = startDir, prev = ''; dir !== prev && dir !== '.'; prev = dir, dir = dirname(dir)) { | ||
| const content = readFile(join(dir, 'deno.lock')); | ||
| if (content) { | ||
| try { | ||
| const lock = JSON.parse(content) as { specifiers?: object; packages?: { specifiers?: object } }; | ||
| for (const [specifier, resolved] of Object.entries(lock.specifiers || lock.packages?.specifiers || {})) { | ||
| const name = /^(?:npm|jsr):(@?[^@]+)@/.exec(specifier)?.[1]; | ||
| // v3 stores `npm:express@4.21.2`, later versions `4.21.2`; npm versions can end in `_<peer deps>`. | ||
| const version = typeof resolved === 'string' && resolved.replace(/^(?:npm|jsr):@?[^@]+@/, '').split('_')[0]; | ||
| if (name && version) { | ||
| modules[name] = version; | ||
| } | ||
| } | ||
| } catch { | ||
| // invalid lockfile | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| return modules; | ||
| } | ||
|
|
||
| function collectModules(): Record<string, string> { | ||
| const entryPath = Deno.mainModule.startsWith('file:') ? fileURLToPath(Deno.mainModule) : undefined; | ||
| const cwd = readFile(); | ||
| const modules = collectInstalledModules({ readFile, entryPath, cwd }); | ||
| const startDir = (entryPath ? dirname(entryPath.replace(/\\/g, '/')) : cwd?.replace(/\\/g, '/')) || '.'; | ||
| // Without `node_modules`, `modules` only has declared ranges, so the exact versions from `deno.lock` win. | ||
| return { ...modules, ...getDenoLockModules(startDir) }; | ||
| } | ||
|
|
||
| const _modulesIntegration = (() => { | ||
| let modules: Record<string, string> | undefined; | ||
| return { | ||
| name: 'Modules' as const, | ||
| processEvent(event) { | ||
| modules = modules || collectModules(); | ||
|
sentry-warden[bot] marked this conversation as resolved.
Resolved
|
||
| event.modules = { ...event.modules, ...modules }; | ||
| return event; | ||
| }, | ||
| }; | ||
| }) satisfies IntegrationFn; | ||
|
|
||
| /** | ||
| * Adds the app's packages and versions to events, from `deno.lock` and from `package.json` and `node_modules`. | ||
| */ | ||
| export const modulesIntegration = defineIntegration(_modulesIntegration); | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
| import * as Sentry from '../../../build/esm/index.js'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://username@domain/123', | ||
| defaultIntegrations: false, | ||
| integrations: [Sentry.modulesIntegration()], | ||
| beforeSend(event) { | ||
| // oxlint-disable-next-line no-console | ||
| console.log(JSON.stringify(event.modules)); | ||
| return null; | ||
| }, | ||
| }); | ||
|
|
||
| Sentry.captureException(new Error('test')); | ||
| await Sentry.flush(); |
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,7 @@ | ||
| { | ||
| "name": "deno-modules-fixture", | ||
| "private": true, | ||
| "dependencies": { | ||
| "left-pad": "^1.3.0" | ||
| } | ||
| } |
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,27 @@ | ||
| import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; | ||
|
|
||
| const fixtureDir = new URL('./fixtures/modules/', import.meta.url); | ||
|
|
||
| async function runFixture(permissions: string[]): Promise<unknown> { | ||
| const { stdout, stderr, success } = await new Deno.Command(Deno.execPath(), { | ||
| // `--no-lock` keeps Deno from rewriting the fixture lockfile. | ||
| args: ['run', '--no-prompt', '--no-lock', ...permissions, new URL('main.ts', fixtureDir).pathname], | ||
| cwd: fixtureDir.pathname, | ||
| stdin: 'null', | ||
| }).output(); | ||
| assertEquals(success, true, new TextDecoder().decode(stderr)); | ||
| return JSON.parse(new TextDecoder().decode(stdout).trim()); | ||
| } | ||
|
|
||
| Deno.test('modulesIntegration reads deno.lock and package.json', async () => { | ||
| assertEquals(await runFixture([`--allow-read=${fixtureDir.pathname}`]), { | ||
| 'left-pad': '^1.3.0', | ||
| '@std/assert': '1.0.13', | ||
| '@types/react-dom': '18.3.0', | ||
| express: '4.21.2', | ||
| }); | ||
| }); | ||
|
|
||
| Deno.test('modulesIntegration reads nothing without read permission', async () => { | ||
| assertEquals(await runFixture([]), {}); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cwd is never found with scoped read permission
Low Severity
readFile()with no path also acts as the cwd getter. It callsDeno.permissions.querySyncwithpath: undefined, which only reportsgrantedwhen read access to everything is allowed. With a scoped permission such as--allow-read=.,cwdis alwaysundefined. When the main module isn't afile:URL, nothing is collected even though the files can be read.Additional Locations (1)
packages/deno/src/integrations/modules.ts#L44-L47Reviewed by Cursor Bugbot for commit 550c807. Configure here.