From e21fc2d24eceb5b6b406fc056c7bf90502d83a7d Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 4 Nov 2024 09:35:32 -0500 Subject: [PATCH] fix(node): Make sure modulesIntegration does not crash esm apps --- .../suites/esm/modules-integration/app.mjs | 10 ++++++++++ .../suites/esm/modules-integration/test.ts | 12 ++++++++++++ packages/node/src/integrations/modules.ts | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 dev-packages/node-integration-tests/suites/esm/modules-integration/app.mjs create mode 100644 dev-packages/node-integration-tests/suites/esm/modules-integration/test.ts diff --git a/dev-packages/node-integration-tests/suites/esm/modules-integration/app.mjs b/dev-packages/node-integration-tests/suites/esm/modules-integration/app.mjs new file mode 100644 index 000000000000..7f4316dce907 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/esm/modules-integration/app.mjs @@ -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, +}); diff --git a/dev-packages/node-integration-tests/suites/esm/modules-integration/test.ts b/dev-packages/node-integration-tests/suites/esm/modules-integration/test.ts new file mode 100644 index 000000000000..556ec1d52a57 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/esm/modules-integration/test.ts @@ -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); + }); +}); diff --git a/packages/node/src/integrations/modules.ts b/packages/node/src/integrations/modules.ts index b1b56c6c1fda..f434319b5bae 100644 --- a/packages/node/src/integrations/modules.ts +++ b/packages/node/src/integrations/modules.ts @@ -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) { @@ -23,6 +37,8 @@ const _modulesIntegration = (() => { /** * Add node modules / packages to the event. + * + * Only works in CommonJS (CJS) environments. */ export const modulesIntegration = defineIntegration(_modulesIntegration);