Skip to content

Commit

Permalink
fix(node): Make sure modulesIntegration does not crash esm apps
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Nov 4, 2024
1 parent 6729214 commit e21fc2d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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://public@dsn.ingest.sentry.io/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

0 comments on commit e21fc2d

Please sign in to comment.