Skip to content

require(esm) with top level error taints future await import(esm) swallowing errors #58945

Closed
@samuelhnrq

Description

@samuelhnrq

Version

Reproductible in v24.1.0 and v24.2.0

Platform

Linux xnvwjs 6.1.43 #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Code sandbox with MRE: https://codesandbox.io/p/devbox/xnvwjs

but here it goes anyway:

mkdir repro
(
    cd repro
    echo 'throw globalThis.err; export const foo=2' > bad-esm.mjs
    cat -> entry.cjs <<'EOF'
    'use strict';
    const assert = require('node:assert');
    globalThis.err = new Error;
    assert.throws(() => require('./bad-esm.mjs'), globalThis.err);
    assert.rejects(import('./bad-esm.mjs').then(console.log), globalThis.err);
EOF
    node entry.cjs
)
rm -rf repro

How often does it reproduce? Is there a required condition?

100% of the times, no preconditons

What is the expected behavior? Why is that the expected behavior?

require(esm) should not affect future await import(esm), if given file throws error on import it should keep throwing again, regardless of times or the way its imported. As documented here

What do you see instead?

[Module: null prototype] { foo: <uninitialized> }
node:internal/process/promises:332
    triggerUncaughtException(err, true /* fromPromise */);
    ^

[AssertionError [ERR_ASSERTION]: Missing expected rejection (Error).]

Calling require(ESM) on a file with a top level throw/error makes all future import(esm) of the same file fail silently and return garbage (partial module definition I think)

Additional information

I found the error while investigating mochajs/mocha#5396

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions