module: add dynamic file-specific ESM warnings · nodejs/node@4c29cc7 · GitHub
Skip to content

Commit 4c29cc7

Browse files
mertcanaltinRafaelGSS
authored andcommitted
module: add dynamic file-specific ESM warnings
PR-URL: #56628 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 766efc7 commit 4c29cc7

5 files changed

Lines changed: 55 additions & 21 deletions

File tree

src/node_contextify.cc

Lines changed: 14 additions & 8 deletions

src/node_process_events.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ using v8::Just;
1313
using v8::Local;
1414
using v8::Maybe;
1515
using v8::MaybeLocal;
16+
using v8::NewStringType;
1617
using v8::Nothing;
1718
using v8::Object;
1819
using v8::String;
@@ -21,7 +22,14 @@ using v8::Value;
2122
Maybe<bool> ProcessEmitWarningSync(Environment* env, std::string_view message) {
2223
Isolate* isolate = env->isolate();
2324
Local<Context> context = env->context();
24-
Local<String> message_string = OneByteString(isolate, message);
25+
Local<String> message_string;
26+
if (!String::NewFromUtf8(isolate,
27+
message.data(),
28+
NewStringType::kNormal,
29+
static_cast<int>(message.size()))
30+
.ToLocal(&message_string)) {
31+
return Nothing<bool>();
32+
}
2533

2634
Local<Value> argv[] = {message_string};
2735
Local<Function> emit_function = env->process_emit_warning_sync();

test/es-module/test-esm-cjs-load-error-note.mjs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import assert from 'node:assert';
44
import { execPath } from 'node:process';
55
import { describe, it } from 'node:test';
66

7-
87
// Expect note to be included in the error output
98
// Don't match the following sentence because it can change as features are
109
// added.
11-
const expectedNote = 'Warning: To load an ES module';
10+
const expectedNote = 'Failed to load the ES module';
1211

1312
const mustIncludeMessage = {
1413
getMessage: (stderr) => `${expectedNote} not found in ${stderr}`,

test/es-module/test-esm-long-path-win.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ describe('long path on Windows', () => {
182182
fs.writeFileSync(cjsIndexJSPath, 'import fs from "node:fs/promises";');
183183
const { code, signal, stderr } = await spawnPromisified(execPath, [cjsIndexJSPath]);
184184

185-
assert.ok(stderr.includes('Warning: To load an ES module'));
185+
assert.ok(stderr.includes('Failed to load the ES module'));
186186
assert.strictEqual(code, 1);
187187
assert.strictEqual(signal, null);
188188
});

test/es-module/test-typescript-commonjs.mjs

Lines changed: 30 additions & 9 deletions

0 commit comments

Comments
 (0)