Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as path from 'path';
import type { VercelCronsConfig } from '../../common/types';
import type { RouteManifest } from '../manifest/types';
import type { JSONValue, TurbopackMatcherWithRule } from '../types';
import { getPackageModules } from '../util';

/**
* Generate the value injection rules for client and server in turbopack config.
Expand Down Expand Up @@ -40,6 +41,9 @@ export function generateValueInjectionRules({
if (vercelCronsConfig) {
serverValues._sentryVercelCronsConfig = JSON.stringify(vercelCronsConfig);
}
// Inject server modules (matching webpack's __SENTRY_SERVER_MODULES__ behavior)
// Use process.cwd() to get the project directory at build time
serverValues.__SENTRY_SERVER_MODULES__ = getPackageModules(process.cwd());
Copy link

Choose a reason for hiding this comment

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

Wrong package.json chosen in monorepos

Medium Severity

generateValueInjectionRules uses getPackageModules(process.cwd()) to populate __SENTRY_SERVER_MODULES__, which can read the wrong package.json when next runs from a different working directory (common in monorepos/workspaces). This can inject an unrelated dependency map, breaking module auto-detection or producing misleading module availability.

Fix in Cursor Fix in Web

Copy link

Choose a reason for hiding this comment

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

Turbopack value injection incompatible with bare identifier access

High Severity

The __SENTRY_SERVER_MODULES__ value is injected onto globalThis by the valueInjectionLoader, but the consumer code in modules.ts accesses it as a bare identifier rather than via globalThis. In ES modules with strict mode, properties on globalThis aren't accessible as bare identifiers, causing the typeof check to evaluate as 'undefined' and fall back to an empty object. This breaks module detection for Turbopack builds, defeating the entire purpose of this PR.

Fix in Cursor Fix in Web


if (Object.keys(isomorphicValues).length > 0) {
clientValues = { ...clientValues, ...isomorphicValues };
Expand Down
22 changes: 22 additions & 0 deletions packages/nextjs/src/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parseSemver } from '@sentry/core';
import * as fs from 'fs';
import { createRequire } from 'module';
import * as path from 'path';

/**
* Returns the version of Next.js installed in the project, or undefined if it cannot be determined.
Expand Down Expand Up @@ -181,3 +182,24 @@ export function detectActiveBundler(): 'turbopack' | 'webpack' {
return 'webpack';
}
}

/**
* Extract modules from project directory's package.json
*/
export function getPackageModules(projectDir: string): Record<string, string> {
try {
const packageJson = path.join(projectDir, 'package.json');
const packageJsonContent = fs.readFileSync(packageJson, 'utf8');
const packageJsonObject = JSON.parse(packageJsonContent) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};

return {
...packageJsonObject.dependencies,
...packageJsonObject.devDependencies,
};
} catch {
return {};
}
}
22 changes: 2 additions & 20 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import type {
WebpackConfigObjectWithModuleRules,
WebpackEntryProperty,
} from './types';
import { getNextjsVersion } from './util';
import { getNextjsVersion, getPackageModules } from './util';
import type { VercelCronsConfigResult } from './withSentryConfig/getFinalConfigObjectUtils';

// Next.js runs webpack 3 times, once for the client, the server, and for edge. Because we don't want to print certain
Expand Down Expand Up @@ -425,7 +425,7 @@ export function constructWebpackConfigFunction({
newConfig.plugins = newConfig.plugins || [];
newConfig.plugins.push(
new buildContext.webpack.DefinePlugin({
__SENTRY_SERVER_MODULES__: JSON.stringify(_getModules(projectDir)),
__SENTRY_SERVER_MODULES__: JSON.stringify(getPackageModules(projectDir)),
}),
);

Expand Down Expand Up @@ -883,24 +883,6 @@ function addEdgeRuntimePolyfills(newConfig: WebpackConfigObjectWithModuleRules,
};
}

function _getModules(projectDir: string): Record<string, string> {
try {
const packageJson = path.join(projectDir, 'package.json');
const packageJsonContent = fs.readFileSync(packageJson, 'utf8');
const packageJsonObject = JSON.parse(packageJsonContent) as {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
};

return {
...packageJsonObject.dependencies,
...packageJsonObject.devDependencies,
};
} catch {
return {};
}
}

/**
* Sets up the tree-shaking flags based on the user's configuration.
* https://docs.sentry.io/platforms/javascript/guides/nextjs/configuration/tree-shaking/
Expand Down
145 changes: 141 additions & 4 deletions packages/nextjs/test/config/turbopack/constructTurbopackConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('constructTurbopackConfig', () => {
{ path: '/users', regex: '/users' },
{ path: '/api/health', regex: '/api/health' },
],
isrRoutes: [],
};

const mockSentryOptions = {};
Expand All @@ -36,7 +37,22 @@ describe('constructTurbopackConfig', () => {
userSentryOptions: mockSentryOptions,
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should create turbopack config with instrumentation rule when manifest is provided', () => {
Expand All @@ -61,6 +77,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -135,6 +163,18 @@ describe('constructTurbopackConfig', () => {
},
rules: {
'*.test.js': ['jest-loader'],
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -174,6 +214,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -224,7 +276,7 @@ describe('constructTurbopackConfig', () => {
describe('with edge cases', () => {
it('should handle empty route manifest', () => {
const userNextConfig: NextConfigObject = {};
const emptyManifest: RouteManifest = { dynamicRoutes: [], staticRoutes: [] };
const emptyManifest: RouteManifest = { dynamicRoutes: [], staticRoutes: [], isrRoutes: [] };

const result = constructTurbopackConfig({
userNextConfig,
Expand All @@ -245,13 +297,26 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should handle complex route manifest', () => {
const userNextConfig: NextConfigObject = {};
const complexManifest: RouteManifest = {
isrRoutes: [],
dynamicRoutes: [
{ path: '/users/[id]/posts/[postId]', regex: '/users/([^/]+)/posts/([^/]+)', paramNames: ['id', 'postId'] },
{ path: '/api/[...params]', regex: '/api/(.+)', paramNames: ['params'] },
Expand All @@ -278,6 +343,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -308,6 +385,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -342,6 +431,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: '15.0.0',
},
},
Expand Down Expand Up @@ -397,6 +487,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: '14.0.0',
},
},
Expand Down Expand Up @@ -433,6 +524,18 @@ describe('constructTurbopackConfig', () => {
},
],
},
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});
Expand Down Expand Up @@ -493,6 +596,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -534,6 +638,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -586,6 +691,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down Expand Up @@ -624,7 +730,22 @@ describe('constructTurbopackConfig', () => {
nextJsVersion: undefined,
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should not create Next.js version rule when nextJsVersion is empty string', () => {
Expand All @@ -635,7 +756,22 @@ describe('constructTurbopackConfig', () => {
nextJsVersion: '',
});

expect(result).toEqual({});
expect(result).toEqual({
rules: {
'**/instrumentation.*': {
loaders: [
{
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
},
},
},
],
},
},
});
});

it('should not override existing instrumentation rule when nextJsVersion is provided', () => {
Expand Down Expand Up @@ -725,6 +861,7 @@ describe('constructTurbopackConfig', () => {
loader: '/mocked/path/to/valueInjectionLoader.js',
options: {
values: {
__SENTRY_SERVER_MODULES__: expect.any(Object),
_sentryNextJsVersion: nextJsVersion,
},
},
Expand Down
Loading
Loading