Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patching Module.wrapper et al. breaks shebang stripping #36173

Open
jaked opened this issue Nov 19, 2020 · 2 comments
Open

patching Module.wrapper et al. breaks shebang stripping #36173

jaked opened this issue Nov 19, 2020 · 2 comments
Labels
module Issues and PRs related to the module subsystem.

Comments

@jaked
Copy link

jaked commented Nov 19, 2020

  • Version: v14.10.1
  • Platform: Darwin Kernel Version 18.7.0: Mon Aug 31 20:53:32 PDT 2020; root:xnu-4903.278.44~1/RELEASE_X86_64 x86_64
  • Subsystem: internal/modules/cjs

What steps will reproduce the bug?

in file test.js:

#! /foo/bar/baz
console.log('ok')

then

$ node
> const Module = require('module')
> Module.wrapper = Module.wrapper
> require('./test')
/Users/jake/test.js:1
(function (exports, require, module, __filename, __dirname) { #! /foo/bar/baz
                                                              ^

Uncaught SyntaxError: Invalid or unexpected token

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

always

What is the expected behavior?

shebang line is stripped, no syntax error

What do you see instead?

syntax error

Additional information

Shebang lines are supposed to be stripped when loading modules. This was moved from internal/modules/cjs to V8, see 702331be90

Also, loaded modules are wrapped in an IIFE to provide context; this originally used Module.wrapper, but was changed to use an internal contextify module, with a fallback to the old implementation if Module.wrapper is monkey-patched, see 5f8ccecaa2

(I'm using Electron, which monkey-patches Module.wrapper, see electron/electron@a4fcc32799)

However, the shebang change seems to have broken the monkey-patching support. In this case, the shebang line is not stripped in internal/modules/cjs, but the module is wrapped in an IIFE, so the shebang is now inside the IIFE.

@sseide
Copy link

sseide commented Jun 10, 2021

for us it breaks loading plant-uml package to auto-generate uml images while running jsdoc...
No nice images anymore in docs, just a sea of gray :-(

Is there anyone working on it? Its not an electron only problem. We have this too without "electron" or "rewire" trying to create JSDoc documentation from source files.

jaked added a commit to jaked/programmable-matter that referenced this issue Aug 3, 2021
browsersync and dev-ip don't load because they run into

  nodejs/node#36173

in my build I commented out the shebang lines :/. need to remove
browsersync or switch to livereload.
@pduchesne
Copy link

Any news on this?

We are hitting this problem too. It happens whenever your project imports both a module that involves a shebang line, and another module that patches the module loader.
In that situation, as pointed out by @jaked , the patching of the module loader causes the old implementation to kick in, resulting in the shebang line to not be removed by V8.

The stripShebang function should be reinstated in the case of a patched module loader.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module Issues and PRs related to the module subsystem.
Projects
None yet
Development

No branches or pull requests

4 participants