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

Show where syntax error from with ESM #4558

Closed
wants to merge 1 commit into from

Conversation

outsideris
Copy link
Member

Description of the Change

This is a kind of hack because node.js currently not support syntax errors in detail. See nodejs/node#49441
And This is another approach to #4557 .

I found node --unhandled-rejections=throw -e "import('syntax-error-file.mjs')" show syntax erroed file, linenumber, and code.

So, I try to run it with a file to import only when SyntaxError raised.

So, it will show like this:

$ mocha -ui qunit test*.mjs

file:///Users/outsider/Dropbox/projects/github/mocha/test2.mjs:5
test('test2a', => { // syntax error!
               ^^

SyntaxError: Unexpected token '=>'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:145:18)
    at async link (internal/modules/esm/module_job.js:47:21)

And --unhandled-rejections=throw is default since Node.js 15.0.0

Alternate Designs

See #4557

Why should this be in core?

Before node.js itself support it, Users can find easily where a syntax error from.

Possible Drawbacks

It can be broken when Node.js itself supports it.

Applicable issues

close #4551

If we agreed with this approach, I will add tests for It.
I want to hear your opinion about this workaround.

Signed-off-by: Outsider <outsideris@gmail.com>
@outsideris outsideris added type: bug a defect, confirmed by a maintainer area: usability concerning user experience or interface labels Jan 20, 2021
@outsideris outsideris self-assigned this Jan 20, 2021
@coveralls
Copy link

Coverage Status

Coverage decreased (-0.3%) to 93.867% when pulling fa51418 on outsideris:esm-syntax-error into c667d10 on mochajs:master.

@juergba
Copy link
Member

juergba commented Jan 22, 2021

@outsideris IMO this solution goes too far. It's certainly a creative work-around, but we should not try to fix Node bugs in such a complex way as spawning a child-process.

I prefer the simpler approach of @giltayar which just injects the file name into the original error stack.

Btw I noticed that in Node v15.3.0 the stability of Node's ESM implementation has changed to Stable. Nice.

@outsideris
Copy link
Member Author

outsideris commented Jan 26, 2021

I also know that. I don't have a strong opinion to use this approach.
I just made a proposal after digging to find a way where the syntax error is from.

Without this, we can inject the filename only which is not enough information for a large project.
Spawning a child process is a dirty way.

Btw I noticed that in Node v15.3.0 the stability of Node's ESM implementation has changed to Stable. Nice

Anyway, it is so good.

@outsideris
Copy link
Member Author

Closed by #4557

@outsideris outsideris closed this May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: usability concerning user experience or interface type: bug a defect, confirmed by a maintainer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

If one among multiple ESM tests has a syntax error, then Mocha doesn’t report which one
3 participants