Skip to content

fix(node): Use normal require call to import Undici #10388

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 8 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
"test:express": "node test/manual/express-scope-separation/start.js",
"test:jest": "jest",
"test:release-health": "node test/manual/release-health/runner.js",
"test:webpack": "cd test/manual/webpack-async-context/ && yarn --silent && node npm-build.js",
"test:webpack": "cd test/manual/webpack-async-context/ && yarn --silent --ignore-engines && node npm-build.js",
"test:watch": "jest --watch",
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push --sig"
},
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/integrations/undici/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import type { EventProcessor, Integration, Span } from '@sentry/types';
import {
LRUMap,
dynamicRequire,
dynamicSamplingContextToSentryBaggageHeader,
generateSentryTraceHeader,
getSanitizedUrlString,
Expand Down Expand Up @@ -106,7 +105,7 @@ export class Undici implements Integration {
let ds: DiagnosticsChannel | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
ds = dynamicRequire(module, 'diagnostics_channel') as DiagnosticsChannel;
ds = require('diagnostics_channel') as DiagnosticsChannel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One reason this is there is because diagnostics_channel is only available for Node 16+, won't bundling break when bundling for older versions? (Like Node 14).

Copy link
Contributor Author

@lforst lforst Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it as follows: Every bundler has a built-in escape hatch because diagnostics_channel can always be manually externalized. The newer (sane) webpack versions automatically seem to externalize it out of the box, even on older Node.js versions.

Right now dynamicRequire seems to break fetch instrumentation on the Next.js canary: https://github.com/getsentry/sentry-javascript/actions/runs/7713179805/job/21023035517

With this change, it succeeds: https://github.com/getsentry/sentry-javascript/actions/runs/7712921252/job/21022104165

dynamicRequire effectively breaks bundled code. I see this more as a fix I think.

I have the soft opinion that this change is the lesser evil but I am not totally sure tbh. People affected would be people who are on old node versions && on old webpack versions. Wdyt?

} catch (e) {
// no-op
}
Expand Down
5 changes: 5 additions & 0 deletions packages/node/test/manual/webpack-async-context/npm-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ if (Number(process.versions.node.split('.')[0]) >= 18) {
process.exit(0);
}

// Webpack test does not work in Node 8 and below.
if (Number(process.versions.node.split('.')[0]) <= 8) {
process.exit(0);
}

// biome-ignore format: Follow-up for prettier
webpack(
{
Expand Down
6 changes: 3 additions & 3 deletions packages/node/test/manual/webpack-async-context/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"main": "index.js",
"license": "MIT",
"dependencies": {
"webpack": "^4.42.1"
"webpack": "^5.90.0"
},
"devDependencies": {
"webpack-cli": "^3.3.11"
"volta": {
"extends": "../../../../../package.json"
}
}
Loading