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: fix error reporting #55561

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1648,7 +1648,7 @@ function loadTS(module, filename) {
function reconstructErrorStack(err, parentPath, parentSource) {
const errLine = StringPrototypeSplit(
StringPrototypeSlice(err.stack, StringPrototypeIndexOf(
err.stack, ' at ')), '\n', 1)[0];
err.stack, ' at ')), '\n', 2)[1];
geeksilva97 marked this conversation as resolved.
Show resolved Hide resolved
const { 1: line, 2: col } =
RegExpPrototypeExec(/(\d+):(\d+)\)/, errLine) || [];
if (line && col) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import nothing from 'somewhere';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const app = require('./app');
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function main() {
func1(func2(func3()))
}

function func1() {
require('./app.js')
}
function func2() {}
function func3() {}

main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import '../common/index.mjs';
import { test } from 'node:test';
import * as fixtures from '../common/fixtures.mjs';
import { spawnSync } from 'node:child_process';
import assert from 'node:assert';

test('correctly reports errors when an ESM module is required with --no-experimental-require-module', () => {
// The following regex matches the error message that is expected to be thrown
//
// package-type-module/require-esm-error-annotation/index.cjs:1
// const app = require('./app');
// ^

const matchRegex = /package-type-module[\\/]+require-esm-error-annotation[\\/]+index\.cjs:1[\r?\n]const app = require\('\.\/app'\);[\r?\n]\s{12}\^/;
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/index.cjs');
const args = ['--no-experimental-require-module', fixture];

const result = spawnSync(process.execPath, args);

assert.strictEqual(result.status, 1);
assert(result.stderr.toString().match(matchRegex));
assert.strictEqual(result.stdout.toString(), '');
});

test('correctly reports error for a longer stack trace', () => {
// The following regex matches the error message that is expected to be thrown
//
// package-type-module/require-esm-error-annotation/longer-stack.cjs:6
// require('./app.js')
// ^

const matchRegex = /package-type-module[\\/]+require-esm-error-annotation[\\/]+longer-stack\.cjs:6[\r?\n]\s{2}require\('\.\/app\.js'\)[\r?\n]\s{2}\^/;
const fixture = fixtures.path('es-modules/package-type-module/require-esm-error-annotation/longer-stack.cjs');
const args = ['--no-experimental-require-module', fixture];

const result = spawnSync(process.execPath, args);

assert.strictEqual(result.status, 1);
assert(result.stderr.toString().match(matchRegex));
assert.strictEqual(result.stdout.toString(), '');
});
Loading