-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
module: use compileFunction over Module.wrap #21573
Conversation
i assume you're still planning to add in the setters for _wrapper? |
@devsnek we could talk about that in this thread, I guess. I still am not 100% onboard on the idea that we'd have to somewhat complicate core just because people decided to monkey-patch internal core modules. |
@ryzokuken i generally feel the same way but this feels like a really big change... @ChALkeR would you be able to run a gzemnid for Module._wrapper and Module.wrap? |
@ryzokuken I think it’s worth going that extra distance for compatibility |
@devsnek definitely +1 for gzemnid. I'd love to dive deeper into what the community is monkey-patching If somebody is prepending code or something like that, I don't know how I feel about that though. Maybe @jdalton will be able to give some insights on this? |
Not sure what nodejs/modules can contribute - nodejs/modules is a team focused on CJS/ESM sompatibility and defining + landing stable ESM. (Not criticism of the ping - just setting expectations for the feedback the team can provide). +1 on doing this by the way! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned here (in a commit collapsed discussion) the use of Module.wrap
and Module.wrapper
to create the CJS module wrapping has existed since the start of 2011. I want to be careful here since the functionality being removed was easily discoverable and totally usable to customize the CJS wrapper.
I floated out a couple examples of how to tackle this. The one folks seemed to dig was putting setters on .wrap
, .wrapper
, wrapper[0]
, and .wrapper[1]
that would revert to the old (current) behavior. That said, other approaches would be welcomed.
I'm all for this improvement, however I've repeatedly asked for caution around changing anything on the CJS-side. So I cannot approve this PR without steps taken to ensure interop and migration for CJS.
@jdalton as I proposed,
I'm quite open to provide temporary setters and deprecating Stating my personal opinion, I'm actually not thrilled by the idea of supporting monkey-patches because I believe that they promote such reckless behavior, and it feels a lot like digging ourselves a hole, but I do understand that the CJS community might not have had too many options, and am willing to support the setters if the use-case is reasonable enough. If you could help me with a few examples from userland (how and why exactly are people monkey-patching |
I think offering a legacy mode on the old code path is reasonable. |
List of code I found on GitHub that monkey-patched
|
CJS is incredibly flexible and tweakable (some say to a fault and others say to one of its biggest strengths). In the case here folks could tweak the wrapper of CJS to do things like make it an async function and allow top-level |
@jdalton Adding strict mode, changing the default arguments, and adding top level await all seem not only common from my quick joyride throughout GitHub, I think they are reasonable enough to have fallback setters for them until we finally deprecate and remove Regarding the ability to prepend/append arbitrary code though is something I have mixed thoughts on. |
Ignore the polyfill on mine, usage is ~0 and I don't encourage people to use it. |
Rock! |
I see two parts of this. The Note: Electron customizes |
Interesting! I'll talk to a few others in electron and work this out.
Sounds fair. |
|
@jdalton since they're actually patching the source of core i don't think it is a problem. |
Ideally Electron should patch Node directly less, so working with them on an alternative approach is a good thing. That said, I can sense this derailing here so feel free to take it up with me privately or create an Electron issue to track. |
@hashseed @addaleax @Trott, what's our reasoning for landing this in v11 but not v10 (if we're saying it's not a breaking change). I ask because it would make it makes it easier for c8 to support both versions of Node.js (since it wouldn't have to juggle two different wrapper lengths in 9 months when v10, v11, and v12 are the active branches). |
If there's a compelling reason to land it in v10, I'm OK with that. |
@bcoe relying on the wrapper length is currently hard as any application could currently switch to using the old version in case any code monkey patches the wrapper / the wrap function. Both is quite likely. We likely have overlooked that other applications might also rely on the wrapper length without manipulating the wrapper. AFAIK it is currently impossible to know what offset is actually used depending on the application code. |
@BridgeAR if the backport isn't too much of a pain, I'm just thinking that it's a lot less painful if V10 and V11 both use If user-land modules then provide an alternative wrapper, it would be up to them to provide this information to other tools (perhaps via source-maps) -- the fact that v11/v12 differ in their behavior from v10, which will be the lts, seems like a potential headache going forward.... if the backport is an absolute pain, I understand not doing it. |
I don't have a strong opinion about back porting. I guess that depends on the complexity of the back port. |
Perhaps we should add a If backporting will improve compatibility, then lets do it. When is the latest we can tackle the backport to ensure we have enough data on real-world usage here to be sure about any backport risk? |
PR-URL: nodejs#21573 Fixes: nodejs#17396 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Use vm.compileFunction (which is a binding for v8::CompileFunctionInContext) instead of Module.wrap internally in Module._compile for the cjs loader. Fixes: #17396 Backport-PR-URL: #27124 PR-URL: #21573 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Use vm.compileFunction (which is a binding for v8::CompileFunctionInContext) instead of Module.wrap internally in Module._compile for the cjs loader. Fixes: #17396 Backport-PR-URL: #27124 PR-URL: #21573 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Use vm.compileFunction (which is a binding for v8::CompileFunctionInContext) instead of Module.wrap internally in Module._compile for the cjs loader. Fixes: #17396 Backport-PR-URL: #27124 PR-URL: #21573 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Use vm.compileFunction (which is a binding for v8::CompileFunctionInContext) instead of Module.wrap internally in Module._compile for the cjs loader. Fixes: nodejs/node#17396 PR-URL: nodejs/node#21573 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Use vm.compileFunction (which is a binding for
v8::CompileFunctionInContext) instead of Module.wrap internally in
Module._compile for the cjs loader.
Fixes: #17396
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes/cc @nodejs/modules-active-members @hashseed @addaleax