Chakracore: BUG: Dynamic import should only return a rejected promise if it fails loading a module

Created on 25 Oct 2018  路  7Comments  路  Source: chakra-core/ChakraCore

Test: https://github.com/tc39/test262/blob/master/test/language/expressions/dynamic-import/assignment-expression/unary-expr.js

Result:

Couldn't load file 'true'
Couldn't load file 'undefined'
Couldn't load file 'object'
Couldn't load file 'NaN'
Couldn't load file '-1'

Expected: every import call should return a promise that will be resolved/rejected at some later time. Presently, the import fails and produces these errors synchronously.

Bug

Most helpful comment

In most of these cases the import() call is resulting in a promise that is rejected with the error as the reason BUT additionally ch prints to std whenever failing to load a module (or any file). You should note that execution of the script continues despite the printed exceptions.

That said this behaviour in ch is clearly not the most helpful and to disable it at the moment you'd have to supply the flag -MuteHostErrorMsg which also would disable printing exceptions thrown in normal code.

I could offer a PR to rework this so these module load errors are only printed when flag is supplied instead of the inverse - going to wait until some of my existing PRs are merged before opening another at the moment though.

All 7 comments

More:

The correct errors are produced, but not via a rejected promise, which is what is expected to occur.

There's a lot more of these, but I suspect that if it's fixed once all of these will pass.

In most of these cases the import() call is resulting in a promise that is rejected with the error as the reason BUT additionally ch prints to std whenever failing to load a module (or any file). You should note that execution of the script continues despite the printed exceptions.

That said this behaviour in ch is clearly not the most helpful and to disable it at the moment you'd have to supply the flag -MuteHostErrorMsg which also would disable printing exceptions thrown in normal code.

I could offer a PR to rework this so these module load errors are only printed when flag is supplied instead of the inverse - going to wait until some of my existing PRs are merged before opening another at the moment though.

@rhuanjl thanks for the information.

@rwaldron in the meanwhile it might worth trying another run with this flag on to see if anything persists.

As you said, adding -MuteHostErrorMsg to eshost's ChakraAgent args prevents the messages from being printed out, however these tests are still failing because the import(...) calls are not returning "promises that are eventually rejected" (with whatever error is produced by the module that import(...) attempted to import).

Here's a thing I've just discovered: The tests that DO pass are those that fail before any file IO could possibly occur, eg:

const obj = {
    toString() {
        throw 'custom error';
    }
};

const f = async () => {
  await import(obj);
}

f().catch(error => {
  assert.sameValue(error, 'custom error');
}).then($DONE, $DONE);

So I know that the issue described here: https://github.com/Microsoft/ChakraCore/issues/5237 is still a problem, but import('./cannot-find-this-file.js') should be returning a promise that is rejected with some kind of error that indicates this module file isn't found, instead of just silent failure.

Ok looks there is a second bug in addition to the excess message printing I will investigate it.

I can't reproduce this second error, running with -ESDynamicImport -MuteHostErrorMsg

With this test:

import ("./nonExistant.js").catch(e => print ("caught " + e));

I see:
caught URIError: ./nonExistant.js

Please can you point me to a specific case where the promise isn't rejected but should be?

EDIT:
Got it the second issue is if a child module of the dynamically imported module throws at runtime:

// mod2.js
throw new Error('error');

// mod1.js
import 'mod2.js';

//start.js
import('mod1.js').then (() => print('one'), () => print ('two'));
 // should print 'two' currently doesn't print anything

I will submit a fix for this shortly.

EDIT2: fix is this commit https://github.com/Microsoft/ChakraCore/pull/5803/commits/24bcf2384494c5a8dc8b73e4f6faac370476130b included in PR 5803

Was this page helpful?
0 / 5 - 0 ratings