Skip to content
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

module: trim off internal stack frames for require(esm) warnings #55496

Merged
merged 1 commit into from
Oct 29, 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
5 changes: 4 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1389,7 +1389,10 @@ function loadESMFromCJS(mod, filename) {
messagePrefix = `${from} is loading ES Module ${to} using require().\n`;
}
}
emitExperimentalWarning('Support for loading ES Module in require()', messagePrefix);
emitExperimentalWarning('Support for loading ES Module in require()',
messagePrefix,
undefined,
parent?.require);
const {
wrap,
namespace,
Expand Down
10 changes: 8 additions & 2 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,20 @@ function slowCases(enc) {
}
}

function emitExperimentalWarning(feature, messagePrefix) {
/**
* @param {string} feature Feature name used in the warning message
* @param {string} messagePrefix Prefix of the warning message
* @param {string} code See documentation of process.emitWarning
* @param {string} ctor See documentation of process.emitWarning
*/
function emitExperimentalWarning(feature, messagePrefix, code, ctor) {
marco-ippolito marked this conversation as resolved.
Show resolved Hide resolved
if (experimentalWarnings.has(feature)) return;
experimentalWarnings.add(feature);
let msg = `${feature} is an experimental feature and might change at any time`;
if (messagePrefix) {
msg = messagePrefix + msg;
}
process.emitWarning(msg, 'ExperimentalWarning');
process.emitWarning(msg, 'ExperimentalWarning', code, ctor);
}

function filterDuplicateStrings(items, low) {
Expand Down
35 changes: 35 additions & 0 deletions test/es-module/test-require-module-warning.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

// This checks the warning and the stack trace emitted by the require(esm)
// experimental warning. It can get removed when `require(esm)` becomes stable.

require('../common');
const { spawnSyncAndAssert } = require('../common/child_process');
const fixtures = require('../common/fixtures');
const assert = require('assert');

spawnSyncAndAssert(process.execPath, [
'--trace-warnings',
fixtures.path('es-modules', 'require-module.js'),
], {
trim: true,
stderr(output) {
const lines = output.split('\n');
assert.match(
lines[0],
/ExperimentalWarning: CommonJS module .*require-module\.js is loading ES Module .*message\.mjs/
);
assert.strictEqual(
lines[1],
'Support for loading ES Module in require() is an experimental feature and might change at any time'
);
assert.match(
lines[2],
/at require \(.*modules\/helpers:\d+:\d+\)/
);
assert.match(
lines[3],
/at Object\.<anonymous> \(.*require-module\.js:1:1\)/
);
}
});
1 change: 1 addition & 0 deletions test/fixtures/es-modules/require-module.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./message.mjs');
Loading