Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
6 changes: 0 additions & 6 deletions dev-packages/bun-integration-tests/node-suites/excludes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,6 @@ export const NO_AUTO_INSTRUMENTATION = [
// `new`, and the top-level frame of a CommonJS module is `<anonymous>`, not `Object.?`.
const BUN_STACK_FRAMES_DIFFER = ['suites/contextLines/filename-with-spaces/test.ts'];

// The CJS and ESM cases need Express to capture the route error, which `bun run` does not
// instrument. A bundled app does not help: it has no `require.cache`, and the isolated packages
// are inlined, so the module versions do not match.
const MODULES_NEED_UNBUNDLED_EXPRESS = ['suites/modules/test.ts'];

// The scenario configures `nativeNodeFetchIntegration`, which `@sentry/bun` does not export.
const NO_NATIVE_NODE_FETCH_INTEGRATION = [
'suites/tracing/http-client-spans/fetch-forward-request-hook/test.ts',
Expand Down Expand Up @@ -189,7 +184,6 @@ export const NODE_SUITES_EXCLUDE = [
...NODE_ONLY,
...NO_AUTO_INSTRUMENTATION,
...BUN_STACK_FRAMES_DIFFER,
...MODULES_NEED_UNBUNDLED_EXPRESS,
];

// Excluded only in the `node-suites` project, which runs the suites with `@sentry/node`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import 'esm-app-transitive';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
// Tracing is off, so `expressIntegration()` is not a default integration; opt in explicitly to
// capture the thrown route error this test inspects.
integrations: [Sentry.expressIntegration()],
});

Sentry.captureException(new Error('error_1'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "esm-app",
"private": true,
"dependencies": {
"esm-app-dependency": "^1.0.0"
},
"devDependencies": {
"esm-app-missing-dependency": "^2.0.0"
}
}
12 changes: 12 additions & 0 deletions dev-packages/node-integration-tests/suites/modules/scenario.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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,
});

require('express');

Sentry.captureException(new Error('error_1'));
11 changes: 11 additions & 0 deletions dev-packages/node-integration-tests/suites/modules/scenario.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import 'express';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
transport: loggingTransport,
});

Sentry.captureException(new Error('error_1'));
23 changes: 0 additions & 23 deletions dev-packages/node-integration-tests/suites/modules/server.js

This file was deleted.

10 changes: 0 additions & 10 deletions dev-packages/node-integration-tests/suites/modules/server.mjs

This file was deleted.

81 changes: 60 additions & 21 deletions dev-packages/node-integration-tests/suites/modules/test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import { SDK_VERSION } from '@sentry/core';
import { mkdirSync, rmSync, symlinkSync, writeFileSync } from 'fs';
import { copyFileSync, cpSync, mkdirSync, mkdtempSync, rmSync, symlinkSync, writeFileSync } from 'fs';
import { dirname, join } from 'path';
import { afterAll, describe, expect, test } from 'vitest';
import { cleanupChildProcesses, createRunner } from '../../utils/runner';

// Each test writes its packages into a folder of its own, because the Bun integration tests run this suite in
// several projects at the same time.
function makeTempDir(): string {
return mkdtempSync(join(__dirname, 'tmp-'));
}

describe('modulesIntegration', () => {
afterAll(() => {
cleanupChildProcesses();
});

test('CJS', async () => {
const runner = createRunner(__dirname, 'server.js')
.withMockSentryServer()
await createRunner(__dirname, 'scenario.js')
.expect({
event: {
modules: {
// exact version comes from require.caches
express: expect.stringMatching(/^4\.\d+\.\d+$/),
// this comes from package.json
'@sentry/node': SDK_VERSION,
yargs: '^18.2.0',
// Declared in package.json and installed, but never loaded
yargs: expect.stringMatching(/^18\.\d+\.\d+$/),
// Not declared, but loaded by express, so it is in require.cache
finalhandler: expect.stringMatching(/^\d+\.\d+\.\d+$/),
},
},
})
.start();
runner.makeRequest('get', '/test1', { expectError: true });
await runner.completed();
.start()
.completed();
});

test('CJS with the isolated layouts of pnpm and Bun', async () => {
const nodeModulesDir = join(__dirname, 'node_modules');
const tempDir = makeTempDir();
copyFileSync(join(__dirname, 'scenario-isolated.js'), join(tempDir, 'scenario-isolated.js'));
const nodeModulesDir = join(tempDir, 'node_modules');
for (const [storeDir, name, version, linked] of [
['.pnpm', 'pnpm-isolated-package', '1.2.3', true],
// Only a dependency of the app uses this version, so it is not linked into `node_modules`
Expand All @@ -50,7 +57,7 @@ describe('modulesIntegration', () => {
}

try {
await createRunner(__dirname, 'scenario-isolated.js')
await createRunner(tempDir, 'scenario-isolated.js')
.expect({
event: {
modules: {
Expand All @@ -64,26 +71,58 @@ describe('modulesIntegration', () => {
.start()
.completed();
} finally {
rmSync(nodeModulesDir, { recursive: true, force: true });
rmSync(tempDir, { recursive: true, force: true });
}
});

test('ESM', async () => {
const runner = createRunner(__dirname, 'server.mjs')
.withInstrument(join(__dirname, 'instrument.mjs'))
.withMockSentryServer()
await createRunner(__dirname, 'scenario.mjs')
.expect({
event: {
modules: {
// this comes from package.json
express: expect.stringMatching(/^\^4\.\d+\.\d+$/),
express: expect.stringMatching(/^4\.\d+\.\d+$/),
'@sentry/node': SDK_VERSION,
yargs: '^18.2.0',
yargs: expect.stringMatching(/^18\.\d+\.\d+$/),
},
},
})
.start();
runner.makeRequest('get', '/test1', { expectError: true });
await runner.completed();
.start()
.completed();
});

test('ESM app in a subfolder with its own package.json', async () => {
const tempDir = makeTempDir();
const appDir = join(tempDir, 'esm-app');
cpSync(join(__dirname, 'esm-app'), appDir, { recursive: true });
const packageDir = join(appDir, 'node_modules', 'esm-app-dependency');
mkdirSync(packageDir, { recursive: true });
writeFileSync(join(packageDir, 'package.json'), JSON.stringify({ name: 'esm-app-dependency', version: '1.4.0' }));
// A CommonJS package that the app imports but does not declare, like a transitive dependency
const transitiveDir = join(appDir, 'node_modules', 'esm-app-transitive');
mkdirSync(transitiveDir, { recursive: true });
writeFileSync(
join(transitiveDir, 'package.json'),
JSON.stringify({ name: 'esm-app-transitive', version: '3.1.0' }),
);
writeFileSync(join(transitiveDir, 'index.js'), 'module.exports = {};');

try {
await createRunner(appDir, 'lib', 'scenario.js')
.expect({
event: event => {
// Packages the SDK itself loads from the repo's node_modules can show up too
expect(event.modules).toMatchObject({
'esm-app-dependency': '1.4.0',
'esm-app-transitive': '3.1.0',
// Not installed, so the range from package.json is all there is
'esm-app-missing-dependency': '^2.0.0',
});
},
})
.start()
.completed();
} finally {
rmSync(tempDir, { recursive: true, force: true });
}
});
});
31 changes: 12 additions & 19 deletions packages/nextjs/src/config/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseSemver } from '@sentry/core';
import { collectInstalledModules } from '@sentry/server-utils/no-diagnostic-channels';
import * as fs from 'fs';
import { createRequire } from 'module';
import * as path from 'path';

// Default page extensions per https://github.com/vercel/next.js/blob/f1dbc9260d48c7995f6c52f8fbcc65f08e627992/packages/next/server/config-shared.ts#L161
export const DEFAULT_PAGE_EXTENSIONS = ['tsx', 'ts', 'jsx', 'js'];
Expand Down Expand Up @@ -220,23 +220,16 @@ export function detectActiveBundler(): 'turbopack' | 'webpack' {
}
}

/**
* Extract modules from project directory's package.json
*/
/** The project's dependencies, with exact installed versions where they resolve and declared ranges otherwise. */
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 {};
}
return collectInstalledModules({
readFile: filePath => {
try {
return fs.readFileSync(filePath, 'utf8');
} catch {
return undefined;
}
},
cwd: projectDir,
});
}
Loading
Loading