module: trim off internal stack frames for require(esm) warnings
Trim off irrelevant internal stack frames for require(esm) warnings so it's easier to locate where the call comes from when --trace-warnings is used. PR-URL: https://github.com/nodejs/node/pull/55496 Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Paolo Insogna <paolo@cowtech.it>
This commit is contained in:
parent
4354a1de0e
commit
3b3a95ac0c
@ -1389,7 +1389,10 @@ function loadESMFromCJS(mod, filename) {
|
|||||||
messagePrefix = `${from} is loading ES Module ${to} using require().\n`;
|
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 {
|
const {
|
||||||
wrap,
|
wrap,
|
||||||
namespace,
|
namespace,
|
||||||
|
@ -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) {
|
||||||
if (experimentalWarnings.has(feature)) return;
|
if (experimentalWarnings.has(feature)) return;
|
||||||
experimentalWarnings.add(feature);
|
experimentalWarnings.add(feature);
|
||||||
let msg = `${feature} is an experimental feature and might change at any time`;
|
let msg = `${feature} is an experimental feature and might change at any time`;
|
||||||
if (messagePrefix) {
|
if (messagePrefix) {
|
||||||
msg = messagePrefix + msg;
|
msg = messagePrefix + msg;
|
||||||
}
|
}
|
||||||
process.emitWarning(msg, 'ExperimentalWarning');
|
process.emitWarning(msg, 'ExperimentalWarning', code, ctor);
|
||||||
}
|
}
|
||||||
|
|
||||||
function filterDuplicateStrings(items, low) {
|
function filterDuplicateStrings(items, low) {
|
||||||
|
35
test/es-module/test-require-module-warning.js
Normal file
35
test/es-module/test-require-module-warning.js
Normal file
@ -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
test/fixtures/es-modules/require-module.js
vendored
Normal file
1
test/fixtures/es-modules/require-module.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
require('./message.mjs');
|
Loading…
x
Reference in New Issue
Block a user