Skip to content

Module.runMain causes ESM loaders in the chain to be duplicated #43753

Description

@cspotcode

Version

v19.0.0-nightly202207093d575a4f1b

Platform

Linux DESKTOP-5K7R6JP 5.10.102.1-microsoft-standard-WSL2 #1 SMP Wed Mar 2 00:30:59 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Detailed reproduction here:
https://github.com/cspotcode/repros/tree/node-loader-chaining-duplication

On node nightly, use node --loader ./loader.mjs ./index.mjs

Within index.mjs, call import {runMain} from 'module'; runMain() and then do something that uses loader hooks, such as importing or calling import.meta.resolve(). The hooks in loader.mjs have been duplicated in the chain and thus will be called more than they should.

The linked reproduction demonstrates this by implementing a loader hook that logs a message and delegates to the next hook in the chain.

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

Always

What is the expected behavior?

Module.runMain understands if the loader hooks have already been initialized and does not duplicate them in the chain.

What do you see instead?

Module.runMain duplicates all loaders in the chain.

Additional information

No response

Activity

  1. added
    loadersIssues and PRs related to ES module loaders.
    on Jul 10, 2022
  2. aduh95 commented on Jul 10, 2022

    @aduh95
    Contributor

    /cc @nodejs/loaders

  3. daeyeon commented on Jul 10, 2022

    @daeyeon
    Member

    I guess that this could be fixed if we ensure initializeLoader() is called only once.

    Question: runMain() seems not to be documented yet. (#32643)
    If so, how should we deal with this issue?

  4. added
    moduleIssues and PRs related to the module subsystem.
    on Jul 10, 2022
  5. JakobJingleheimer commented on Jul 10, 2022

    @JakobJingleheimer
    Member

    I guess that this could be fixed if we ensure initializeLoader() is called only once.

    That sounds good to me. Happy to tackle that part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    loadersIssues and PRs related to ES module loaders.moduleIssues and PRs related to the module subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions