Repository navigation
fix(node): Read module versions from the pnpm isolated layout #25526
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
| const Sentry = require('@sentry/node'); | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| transport: loggingTransport, | ||
| }); | ||
|
|
||
| // `test.ts` writes these packages in the folders where the isolated layouts of pnpm and Bun keep them | ||
| require('./node_modules/.pnpm/pnpm-isolated-package@1.2.3/node_modules/pnpm-isolated-package'); | ||
| require('./node_modules/.bun/bun-isolated-package@4.5.6/node_modules/bun-isolated-package'); | ||
|
|
||
| Sentry.captureException(new Error('error_1')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { existsSync, readFileSync } from 'node:fs'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { basename, dirname, join } from 'node:path'; | ||
| import { GLOBAL_OBJ, type IntegrationFn } from '@sentry/core'; | ||
|
|
||
| type ModuleInfo = Record<string, string>; | ||
|
|
@@ -73,6 +73,24 @@ function collectModules(): ModuleInfo { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Whether `dir` is a node_modules folder of the main module, or a folder | ||
| * `<that node_modules>/<.pnpm or .bun>/<package id>/node_modules`, where the isolated layouts of pnpm and Bun keep | ||
| * each package. | ||
| */ | ||
| function isMainModulesDir(dir: string, mainPaths: string[]): boolean { | ||
| if (mainPaths.includes(dir)) { | ||
| return true; | ||
| } | ||
|
|
||
| const storeDir = dirname(dirname(dir)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The pnpm/Bun store holds every version of every package, including transitive ones. Each store entry now counts as a "main" package, so Before this PR, pnpm users got the range from Suggestion: when a name has more than one store version, prefer the one that |
||
| return ( | ||
| basename(dir) === 'node_modules' && | ||
| ['.pnpm', '.bun'].includes(basename(storeDir)) && | ||
| mainPaths.includes(dirname(storeDir)) | ||
| ); | ||
| } | ||
|
|
||
| /** Extract information about package.json modules from require.cache */ | ||
| function collectRequireModules(): ModuleInfo { | ||
| const mainPaths = require.main?.paths || []; | ||
|
|
@@ -94,7 +112,7 @@ function collectRequireModules(): ModuleInfo { | |
| if (!dir || orig === dir || seen.has(orig)) { | ||
| return undefined; | ||
| } | ||
| if (mainPaths.indexOf(dir) < 0) { | ||
| if (!isMainModulesDir(dir, mainPaths)) { | ||
| return updir(); | ||
| } | ||
|
|
||
|
|
||
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.
For
.../node_modules/@scope/name/index.js, the walk checks.../node_modules/@scope(not a modules folder), then.../node_moduleswithorig = .../@scope, finds no@scope/package.json, and moves on. So no scoped package is ever read fromrequire.cache, in any layout. I confirmed this with real installs:@isaacs/cliuiis missing from the npm, pnpm, and Bun results (it only shows up when the cwdpackage.jsonlists it, as a range). This is not a regression, but the PR's goal is to report installed versions for pnpm users, and many of their deps are scoped (@sentry/*,@opentelemetry/*,@aws-sdk/*, ...).Suggestion (could be a follow-up, since it's a pre-existing issue, but I'd fix it now, why not): treat a scope folder as part of the modules folder. I tested this one-line change on the built output, and it fixes scoped packages in the hoisted, pnpm, and Bun layouts: