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: add dynamic file-specific ESM warnings #56628

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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: 14 additions & 7 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1688,13 +1688,19 @@ static MaybeLocal<Function> CompileFunctionForCJSLoader(
return scope.Escape(fn);
}

static std::string GetRequireEsmWarning(Local<String> filename) {
Isolate* isolate = Isolate::GetCurrent();
Utf8Value filename_utf8(isolate, filename);

std::string warning_message =
"Failed to load the ES module: " + filename_utf8.ToString() +
". Make sure to set \"type\": \"module\" in the nearest package.json "
"file "
"or use the .mjs extension.";
return warning_message;
}

static bool warned_about_require_esm = false;
// TODO(joyeecheung): this was copied from the warning previously emitted in the
// JS land, but it's not very helpful. There should be specific information
// about which file or which package.json to update.
const char* require_esm_warning =
"To load an ES module, set \"type\": \"module\" in the package.json or use "
"the .mjs extension.";

static bool ShouldRetryAsESM(Realm* realm,
Local<String> message,
Expand Down Expand Up @@ -1780,8 +1786,9 @@ static void CompileFunctionForCJSLoader(
// This needs to call process.emit('warning') in JS which can throw if
// the user listener throws. In that case, don't try to throw the syntax
// error.
std::string warning_message = GetRequireEsmWarning(filename);
should_throw =
ProcessEmitWarningSync(env, require_esm_warning).IsJust();
ProcessEmitWarningSync(env, warning_message.c_str()).IsJust();
}
if (should_throw) {
isolate->ThrowException(cjs_exception);
Expand Down
3 changes: 1 addition & 2 deletions test/es-module/test-esm-cjs-load-error-note.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import assert from 'node:assert';
import { execPath } from 'node:process';
import { describe, it } from 'node:test';


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

const mustIncludeMessage = {
getMessage: (stderr) => `${expectedNote} not found in ${stderr}`,
Expand Down
10 changes: 6 additions & 4 deletions test/es-module/test-typescript-commonjs.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { skip, spawnPromisified } from '../common/index.mjs';

Check failure on line 1 in test/es-module/test-typescript-commonjs.mjs

View workflow job for this annotation

GitHub Actions / test-macOS (macos-13)

--- stdout --- Test failure: 'expect failure of an .mts file with CommonJS syntax' Location: test/es-module/test-typescript-commonjs.mjs:61:1 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /Failed to load the ES module: \/Users\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./. Input: '(node:45833) Warning: Failed to load the ES module: /Users/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/Users/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + ' at Module.load (node:internal/modules/cjs/loader:1473:32)\n' + ' at Function._load (node:internal/modules/cjs/loader:1285:12)\n' + ' at TracingChannel.traceSync (node:diagnostics_channel:322:14)\n' + ' at wrapModuleLoad (node:internal/modules/cjs/loader:234:24)\n' + ' at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:151:5)\n' + ' at node:internal/main/run_main_module:33:47\n' + '\n' + 'Node.js v24.0.0-pre\n' at TestContext.<anonymous> (file:///Users/runner/work/node/node/dir%20with%20$unusual%22chars%3F'%C3%A5%C3%9F%E2%88%82%C6%92%C2%A9%E2%88%86%C2%AC%E2%80%A6%60/test/es-module/test-typescript-commonjs.mjs:68:3) at async Test.run (node:internal/test_runner/test:981:9) at async Test.processPendingSubtests (node:internal/test_runner/test:678:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '(node:45833) Warning: Failed to load the ES module: /Users/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/Users/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + '...', expected: /Failed to load the ES module: \/Users\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./, operator: 'match' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout "/Users/runner/work/node/node/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`/test/es-module/test-typescript-commonjs.mjs"

Check failure on line 1 in test/es-module/test-typescript-commonjs.mjs

View workflow job for this annotation

GitHub Actions / test-linux

--- stdout --- Test failure: 'expect failure of an .mts file with CommonJS syntax' Location: test/es-module/test-typescript-commonjs.mjs:61:1 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /Failed to load the ES module: \/home\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./. Input: '(node:211857) Warning: Failed to load the ES module: /home/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/home/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + ' at Module.load (node:internal/modules/cjs/loader:1473:32)\n' + ' at Function._load (node:internal/modules/cjs/loader:1285:12)\n' + ' at TracingChannel.traceSync (node:diagnostics_channel:322:14)\n' + ' at wrapModuleLoad (node:internal/modules/cjs/loader:234:24)\n' + ' at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:151:5)\n' + ' at node:internal/main/run_main_module:33:47\n' + '\n' + 'Node.js v24.0.0-pre\n' at TestContext.<anonymous> (file:///home/runner/work/node/node/dir%20with%20$unusual%22chars%3F'%C3%A5%C3%9F%E2%88%82%C6%92%C2%A9%E2%88%86%C2%AC%E2%80%A6%60/test/es-module/test-typescript-commonjs.mjs:68:3) at process.processTicksAndRejections (node:internal/process/task_queues:105:5) at async Test.run (node:internal/test_runner/test:981:9) at async Test.processPendingSubtests (node:internal/test_runner/test:678:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '(node:211857) Warning: Failed to load the ES module: /home/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/home/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + '...', expected: /Failed to load the ES module: \/home\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./, operator: 'match' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout "/home/runner/work/node/node/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`/test/es-module/test-typescript-commonjs.mjs"

Check failure on line 1 in test/es-module/test-typescript-commonjs.mjs

View workflow job for this annotation

GitHub Actions / test-macOS (macos-14)

--- stdout --- Test failure: 'expect failure of an .mts file with CommonJS syntax' Location: test/es-module/test-typescript-commonjs.mjs:61:1 AssertionError [ERR_ASSERTION]: The input did not match the regular expression /Failed to load the ES module: \/Users\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./. Input: '(node:67004) Warning: Failed to load the ES module: /Users/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/Users/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + ' at Module.load (node:internal/modules/cjs/loader:1473:32)\n' + ' at Function._load (node:internal/modules/cjs/loader:1285:12)\n' + ' at TracingChannel.traceSync (node:diagnostics_channel:322:14)\n' + ' at wrapModuleLoad (node:internal/modules/cjs/loader:234:24)\n' + ' at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:151:5)\n' + ' at node:internal/main/run_main_module:33:47\n' + '\n' + 'Node.js v24.0.0-pre\n' at TestContext.<anonymous> (file:///Users/runner/work/node/node/dir%20with%20$unusual%22chars%3F'%C3%A5%C3%9F%E2%88%82%C6%92%C2%A9%E2%88%86%C2%AC%E2%80%A6%60/test/es-module/test-typescript-commonjs.mjs:68:3) at async Test.run (node:internal/test_runner/test:981:9) at async Test.processPendingSubtests (node:internal/test_runner/test:678:7) { generatedMessage: true, code: 'ERR_ASSERTION', actual: '(node:67004) Warning: Failed to load the ES module: /Users/runner/work/node/node/dir%20with $unusual"chars?\'Ã¥Ã\x9Fâ\x88\x82Æ\x92©â\x88\x86¬â\x80¦`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.\n' + '(Use `node --trace-warnings ...` to show where the warning was created)\n' + '/Users/runner/work/node/node/dir%20with $unusual"chars?\'åß∂ƒ©∆¬…`/test/fixtures/typescript/cts/test-cts-but-module-syntax.cts:1\n' + "import util from 'node:util';\n" + '^^^^^^\n' + '\n' + 'SyntaxError: Cannot use import statement outside a module\n' + ' at wrapSafe (node:internal/modules/cjs/loader:1670:18)\n' + ' at Module._compile (node:internal/modules/cjs/loader:1712:20)\n' + ' at Object.loadCTS [as .cts] (node:internal/modules/cjs/loader:1801:10)\n' + '...', expected: /Failed to load the ES module: \/Users\/runner\/work\/node\/node\/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`\/test\/fixtures\/typescript\/cts\/test-cts-but-module-syntax.cts. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension./, operator: 'match' } Command: out/Release/node --test-reporter=./test/common/test-error-reporter.js --test-reporter-destination=stdout "/Users/runner/work/node/node/dir%20with $unusual"chars?'åß∂ƒ©∆¬…`/test/es-module/test-typescript-commonjs.mjs"
import * as fixtures from '../common/fixtures.mjs';
import { match, strictEqual } from 'node:assert';
import { test } from 'node:test';
Expand Down Expand Up @@ -59,12 +59,14 @@
});

test('expect failure of an .mts file with CommonJS syntax', async () => {
const result = await spawnPromisified(process.execPath, [
fixtures.path('typescript/cts/test-cts-but-module-syntax.cts'),
]);
const testFilePath = fixtures.path('typescript/cts/test-cts-but-module-syntax.cts');
const result = await spawnPromisified(process.execPath, [testFilePath]);

strictEqual(result.stdout, '');
match(result.stderr, /To load an ES module, set "type": "module" in the package\.json or use the \.mjs extension\./);

const expectedWarning = `Failed to load the ES module: ${testFilePath}. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.`;
match(result.stderr, new RegExp(expectedWarning));
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved

strictEqual(result.code, 1);
});

Expand Down
Loading