Skip to content

feat(node): Warn if ESM mode is detected #11914

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
May 6, 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
21 changes: 21 additions & 0 deletions dev-packages/node-integration-tests/suites/esm/warn-esm/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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,
});

const { startExpressServerAndSendPortToRunner } = require('@sentry-internal/node-integration-tests');
const express = require('express');

const app = express();

app.get('/test/success', (req, res) => {
res.send({ response: 'response 3' });
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
21 changes: 21 additions & 0 deletions dev-packages/node-integration-tests/suites/esm/warn-esm/server.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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',
transport: loggingTransport,
});

import { startExpressServerAndSendPortToRunner } from '@sentry-internal/node-integration-tests';
import express from 'express';

const app = express();

app.get('/test/success', (req, res) => {
res.send({ response: 'response 3' });
});

Sentry.setupExpressErrorHandler(app);

startExpressServerAndSendPortToRunner(app);
24 changes: 24 additions & 0 deletions dev-packages/node-integration-tests/suites/esm/warn-esm/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { cleanupChildProcesses, createRunner } from '../../../utils/runner';

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

const esmWarning =
'[Sentry] You are using the Sentry SDK with an ESM build. This version of the SDK is not compatible with ESM. Please either build your application with CommonJS, or use v7 of the SDK.';

test('warns if using ESM', async () => {
const runner = createRunner(__dirname, 'server.mjs').ignore('session', 'sessions', 'event').start();

await runner.makeRequest('get', '/test/success');

expect(runner.getLogs()).toContain(esmWarning);
});

test('does not warn if using CJS', async () => {
const runner = createRunner(__dirname, 'server.js').ignore('session', 'sessions', 'event').start();

await runner.makeRequest('get', '/test/success');

expect(runner.getLogs()).not.toContain(esmWarning);
});
18 changes: 13 additions & 5 deletions dev-packages/node-integration-tests/utils/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export function createRunner(...paths: string[]) {
let dockerOptions: DockerOptions | undefined;
let ensureNoErrorOutput = false;
let expectError = false;
const logs: string[] = [];

if (testPath.endsWith('.ts')) {
flags.push('-r', 'ts-node/register');
Expand Down Expand Up @@ -335,12 +336,14 @@ export function createRunner(...paths: string[]) {
child?.kill();
});

if (ensureNoErrorOutput) {
child.stderr?.on('data', (data: Buffer) => {
const output = data.toString();
child.stderr?.on('data', (data: Buffer) => {
const output = data.toString();
logs.push(output.trim());

if (ensureNoErrorOutput) {
complete(new Error(`Expected no error output but got: '${output}'`));
});
}
}
});

child.on('close', () => {
hasExited = true;
Expand Down Expand Up @@ -389,6 +392,8 @@ export function createRunner(...paths: string[]) {
let splitIndex = -1;
while ((splitIndex = buffer.indexOf(0xa)) >= 0) {
const line = buffer.subarray(0, splitIndex).toString();
logs.push(line.trim());

buffer = Buffer.from(buffer.subarray(splitIndex + 1));
// eslint-disable-next-line no-console
if (process.env.DEBUG) console.log('line', line);
Expand All @@ -402,6 +407,9 @@ export function createRunner(...paths: string[]) {
childHasExited: function (): boolean {
return hasExited;
},
getLogs(): string[] {
return logs;
},
makeRequest: async function <T>(
method: 'get' | 'post',
path: string,
Expand Down
18 changes: 16 additions & 2 deletions packages/node/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ import { defaultStackParser, getSentryRelease } from './api';
import { NodeClient } from './client';
import { initOpenTelemetry } from './initOtel';

function getCjsOnlyIntegrations(isCjs = typeof require !== 'undefined'): Integration[] {
return isCjs ? [modulesIntegration()] : [];
function isCjs(): boolean {
return typeof require !== 'undefined';
}

function getCjsOnlyIntegrations(): Integration[] {
return isCjs() ? [modulesIntegration()] : [];
}

/** Get the default integrations for the Node Experimental SDK. */
Expand Down Expand Up @@ -85,6 +89,16 @@ export function init(options: NodeOptions | undefined = {}): void {
}
}

if (!isCjs()) {
// We want to make sure users see this warning
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
'[Sentry] You are using the Sentry SDK with an ESM build. This version of the SDK is not compatible with ESM. Please either build your application with CommonJS, or use v7 of the SDK.',
);
});
}

setOpenTelemetryContextAsyncContextStrategy();

const scope = getCurrentScope();
Expand Down
Loading