Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- "You miss 100 percent of the chances you don't take. — Wayne Gretzky" — Michael Scott

- **fix(node-core): Read `__SENTRY_SERVER_MODULES__` lazily so Turbopack injection is honored [#21339](https://github.com/getsentry/sentry-javascript/pull/21339)**

On Next.js 16 / Turbopack production builds, `modulesIntegration` captured `__SENTRY_SERVER_MODULES__` at module-evaluation time, before Turbopack's runtime `globalThis` assignment ran. This silently disabled module-detection-based auto integrations (Vercel AI, OpenAI, Anthropic, Google GenAI, LangChain, LangGraph) and left `event.modules` empty. The value is now read lazily, supporting both the webpack (`DefinePlugin`) and Turbopack (runtime global) injection styles.

- **ref(core): Deprecate `sendDefaultPii` in favor of `dataCollection` [#21277](https://github.com/getsentry/sentry-javascript/pull/21277)**

`sendDefaultPii` is deprecated and will be removed in v11. The new `dataCollection` option lets you control each category of collected data.
Expand Down
31 changes: 26 additions & 5 deletions packages/node-core/src/integrations/modules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import type { IntegrationFn } from '@sentry/core';
import { GLOBAL_OBJ, type IntegrationFn } from '@sentry/core';
import { isCjs } from '../utils/detection';

type ModuleInfo = Record<string, string>;
Expand All @@ -12,10 +12,31 @@ const INTEGRATION_NAME = 'Modules';
declare const __SENTRY_SERVER_MODULES__: Record<string, string>;

/**
* `__SENTRY_SERVER_MODULES__` can be replaced at build time with the modules loaded by the server.
* Right now, we leverage this in Next.js to circumvent the problem that we do not get access to these things at runtime.
* Reads the modules that were injected at build time into `__SENTRY_SERVER_MODULES__`
* (e.g. by the Next.js SDK, to work around not having access to these at runtime).
*
* This MUST be read lazily (on every call) rather than captured once at module-evaluation
* time, because the two supported bundlers inject the value differently:
* - webpack replaces the `__SENTRY_SERVER_MODULES__` token with a literal via `DefinePlugin`
* (available as soon as this module is evaluated).
* - Turbopack assigns `globalThis.__SENTRY_SERVER_MODULES__` at runtime, from a value-injection
* loader applied to `instrumentation.*`. The instrumentation file's ESM imports are hoisted
* above that assignment, so this module is evaluated *before* the global is set. A
* module-level `const` capture would therefore always be empty under Turbopack, silently
* disabling every module-detection-based auto integration (Vercel AI, OpenAI, Anthropic,
* Google GenAI, LangChain, LangGraph). See getsentry/sentry-javascript#19147.
*/
const SERVER_MODULES = typeof __SENTRY_SERVER_MODULES__ === 'undefined' ? {} : __SENTRY_SERVER_MODULES__;
function getServerModules(): Record<string, string> {
// webpack: the token is replaced with a literal at build time.
if (typeof __SENTRY_SERVER_MODULES__ !== 'undefined') {
return __SENTRY_SERVER_MODULES__;
}
// Turbopack: the value is assigned onto the global object at runtime.
return (
(GLOBAL_OBJ as typeof GLOBAL_OBJ & { __SENTRY_SERVER_MODULES__?: Record<string, string> })
.__SENTRY_SERVER_MODULES__ ?? {}
);
}

const _modulesIntegration = (() => {
return {
Expand Down Expand Up @@ -52,7 +73,7 @@ function getRequireCachePaths(): string[] {
/** Extract information about package.json modules */
function collectModules(): ModuleInfo {
return {
...SERVER_MODULES,
...getServerModules(),
...getModulesFromPackageJson(),
...(isCjs() ? collectRequireModules() : {}),
};
Expand Down
36 changes: 36 additions & 0 deletions packages/node-core/test/integrations/modules.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GLOBAL_OBJ } from '@sentry/core';
import { afterEach, describe, expect, it, vi } from 'vitest';

type GlobalWithModules = typeof GLOBAL_OBJ & { __SENTRY_SERVER_MODULES__?: Record<string, string> };

describe('modulesIntegration', () => {
afterEach(() => {
delete (GLOBAL_OBJ as GlobalWithModules).__SENTRY_SERVER_MODULES__;
vi.resetModules();
});

it('includes modules injected onto the global AFTER this module was evaluated (Turbopack ordering)', async () => {
// Re-evaluate the integration module with no injected global present. This mirrors Turbopack:
// the instrumentation file's hoisted ESM imports evaluate this module *before* its
// `globalThis.__SENTRY_SERVER_MODULES__ = {...}` assignment runs. A module-level capture would
// freeze an empty value here and never see the injection (getsentry/sentry-javascript#19147).
vi.resetModules();
const { modulesIntegration } = await import('../../src/integrations/modules');

// The runtime injection happens only now — after the module is already evaluated.
(GLOBAL_OBJ as GlobalWithModules).__SENTRY_SERVER_MODULES__ = { '@sentry/turbopack-injected': '1.2.3' };

const modules = modulesIntegration().getModules?.() ?? {};
expect(modules['@sentry/turbopack-injected']).toBe('1.2.3');
});

it('reads modules already present before evaluation (webpack DefinePlugin / pre-set global)', async () => {
(GLOBAL_OBJ as GlobalWithModules).__SENTRY_SERVER_MODULES__ = { '@sentry/prebuilt-injected': '4.5.6' };

vi.resetModules();
const { modulesIntegration } = await import('../../src/integrations/modules');

const modules = modulesIntegration().getModules?.() ?? {};
expect(modules['@sentry/prebuilt-injected']).toBe('4.5.6');
});
});
Loading