Skip to content

fix(node): Make sure modulesIntegration does not crash esm apps #14169

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 1 commit into from
Nov 4, 2024
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
@@ -0,0 +1,10 @@
import { loggingTransport } from '@sentry-internal/node-integration-tests';
import * as Sentry from '@sentry/node';

Sentry.init({
dsn: 'https://[email protected]/1337',
release: '1.0',
autoSessionTracking: false,
integrations: [Sentry.modulesIntegration()],
transport: loggingTransport,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { conditionalTest } from '../../../utils';
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

afterAll(() => {
cleanupChildProcesses();
});

conditionalTest({ min: 18 })('modulesIntegration', () => {
test('does not crash ESM setups', done => {
createRunner(__dirname, 'app.mjs').ensureNoErrorOutput().start(done);
});
});
16 changes: 16 additions & 0 deletions packages/node/src/integrations/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,26 @@ import { existsSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { defineIntegration } from '@sentry/core';
import type { IntegrationFn } from '@sentry/types';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import { isCjs } from '../utils/commonjs';

let moduleCache: { [key: string]: string };

const INTEGRATION_NAME = 'Modules';

const _modulesIntegration = (() => {
// This integration only works in CJS contexts
if (!isCjs()) {
DEBUG_BUILD &&
logger.warn(
'modulesIntegration only works in CommonJS (CJS) environments. Remove this integration if you are using ESM.',
);
return {
name: INTEGRATION_NAME,
};
}

return {
name: INTEGRATION_NAME,
processEvent(event) {
Expand All @@ -23,6 +37,8 @@ const _modulesIntegration = (() => {

/**
* Add node modules / packages to the event.
*
* Only works in CommonJS (CJS) environments.
*/
export const modulesIntegration = defineIntegration(_modulesIntegration);

Expand Down
Loading