Skip to content

Commit

Permalink
module: print better message on esm import error
Browse files Browse the repository at this point in the history
Use the same approach as a previous PR to include the offending line in
the output and underline imports of inexistent exports.

PR-URL: #17786
Fixes: #17785
Refs: #17281
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos authored and MylesBorins committed Jan 9, 2018
1 parent 14eb97e commit 3b2d8cb
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/internal/loader/ModuleJob.js
Expand Up @@ -2,6 +2,7 @@

const { ModuleWrap } = internalBinding('module_wrap');
const { SafeSet, SafePromise } = require('internal/safe_globals');
const { decorateErrorStack } = require('internal/util');
const assert = require('assert');
const resolvedPromise = SafePromise.resolve();

Expand Down Expand Up @@ -81,7 +82,12 @@ class ModuleJob {
}
throw e;
}
this.module.instantiate();
try {
this.module.instantiate();
} catch (e) {
decorateErrorStack(e);
throw e;
}
for (const dependencyJob of jobsInGraph) {
// Calling `this.module.instantiate()` instantiates not only the
// ModuleWrap in this module, but all modules in the graph.
Expand Down
8 changes: 8 additions & 0 deletions src/module_wrap.cc
Expand Up @@ -179,13 +179,15 @@ void ModuleWrap::Link(const FunctionCallbackInfo<Value>& args) {
}

void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = args.GetIsolate();
Local<Object> that = args.This();
Local<Context> context = that->CreationContext();

ModuleWrap* obj = Unwrap<ModuleWrap>(that);
CHECK_NE(obj, nullptr);
Local<Module> module = obj->module_.Get(isolate);
TryCatch try_catch(isolate);
Maybe<bool> ok =
module->InstantiateModule(context, ModuleWrap::ResolveCallback);

Expand All @@ -195,6 +197,12 @@ void ModuleWrap::Instantiate(const FunctionCallbackInfo<Value>& args) {
obj->resolve_cache_.clear();

if (!ok.FromMaybe(false)) {
CHECK(try_catch.HasCaught());
CHECK(!try_catch.Message().IsEmpty());
CHECK(!try_catch.Exception().IsEmpty());
AppendExceptionLine(env, try_catch.Exception(), try_catch.Message(),
ErrorHandlingMode::MODULE_ERROR);
try_catch.ReThrow();
return;
}
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/es-module-loaders/module-named-exports.mjs
@@ -0,0 +1,2 @@
export const foo = 'foo';
export const bar = 'bar';
1 change: 1 addition & 0 deletions test/fixtures/es-module-loaders/syntax-error-import.mjs
@@ -0,0 +1 @@
import { foo, notfound } from './module-named-exports';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import.mjs
@@ -0,0 +1,7 @@
// Flags: --experimental-modules
/* eslint-disable no-unused-vars */
import '../common';
import {
foo,
notfound
} from '../fixtures/es-module-loaders/module-named-exports';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import.out
@@ -0,0 +1,7 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
file:///*/test/message/esm_display_syntax_error_import.mjs:6
notfound
^^^^^^^^
SyntaxError: The requested module does not provide an export named 'notfound'
at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*)
at <anonymous>
3 changes: 3 additions & 0 deletions test/message/esm_display_syntax_error_import_module.mjs
@@ -0,0 +1,3 @@
// Flags: --experimental-modules
import '../common';
import '../fixtures/es-module-loaders/syntax-error-import';
7 changes: 7 additions & 0 deletions test/message/esm_display_syntax_error_import_module.out
@@ -0,0 +1,7 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1
import { foo, notfound } from './module-named-exports';
^^^^^^^^
SyntaxError: The requested module does not provide an export named 'notfound'
at ModuleJob._instantiate (internal/loader/ModuleJob.js:*:*)
at <anonymous>

0 comments on commit 3b2d8cb

Please sign in to comment.