Skip to content

Loader preload code never executes #48778

Closed
@ghost

Description

Version

v20.0.0

Platform

Linux silica 5.19.0-45-generic #46-Ubuntu SMP PREEMPT_DYNAMIC Wed Jun 7 09:08:58 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

Loader API

What steps will reproduce the bug?

  • Save this code to loader.mjs.
    export function globalPreload () {
      console.log('PRELOAD')
      return `
    const console = getBuiltin('console')
    console.log('BOOTSTRAP')
    `
    }
  • On any v20 release, run node --loader=./loader.mjs.
  • PRELOAD message is logged. BOOTSTRAP message is never logged.

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

Consistently on any v20 release.

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

The string returned by globalPreload() is executed on the main thread. The API docs claim it will happen.

What do you see instead?

The string returned by globalPreload() is never executed.

Additional information

On the latest v19 this code works.

$ nvm use v19
Now using node v19.9.0 (npm v9.6.7)
$ node --loader=./loader.mjs
(node:509065) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PRELOAD
BOOTSTRAP
Welcome to Node.js v19.9.0.

On the earliest v20 the returned string never executes.

$ nvm use v20.0.0
Now using node v20.0.0 (npm v9.6.4)
$ node --loader=./loader.mjs
Welcome to Node.js v20.0.0.
Type ".help" for more information.
> (node:516773) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PRELOAD

>

I tried setting a global property in the bootstrap code to ensure it wasn't just a console issue. The property is never set.

export function globalPreload () {
  console.log('PRELOAD')
  return `
globalThis.test = 1
`
}
$ nvm use v19
Now using node v19.9.0 (npm v9.6.7)
$ node --loader=./loader.mjs
(node:523929) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PRELOAD
Welcome to Node.js v19.9.0.
Type ".help" for more information.
> test
1
$ nvm use v20.0.0
Now using node v20.0.0 (npm v9.6.4)
$ node --loader=./loader.mjs
Welcome to Node.js v20.0.0.
Type ".help" for more information.
> (node:521480) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
PRELOAD

> test
Uncaught ReferenceError: test is not defined

Could have sworn I saw this working before on v20, but it fails consistently now. I tried it on 2 different machines, one with fresh install of Node.js, with the same result everywhere.

Would really like to use the loader thread. It seems like they should be isolated. But I've got to stick with v19 until I can run a bootstrap at startup.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.loadersIssues and PRs related to ES module loaders

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions