Skip to content

Avoid relying on Node’s internals#6398

Merged
sokra merged 2 commits into
webpack:masterfrom
addaleax:no-binding
Feb 1, 2018
Merged

Avoid relying on Node’s internals#6398
sokra merged 2 commits into
webpack:masterfrom
addaleax:no-binding

Conversation

@addaleax

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Future-proofing / being a good OSS citizen.

Did you add tests for your changes?

No, but Webpack doesn’t seem to test current verions of Node in CI right now anyway.
Once Node 9 or above is in there, this is going to be tested automatically.

If relevant, link to documentation update:

N/A

Summary

process.binding() is not a public API, and should
not be used.

Luckily, Node recently introduced an API that does
exactly what Webpack needs:
https://nodejs.org/api/modules.html#modules_module_builtinmodules

So use that instead and keep the old path as a fallback.

Does this PR introduce a breaking change?

No.

Other information

Nope. :)

`process.binding()` is not a public API, and should
not be used.

Luckily, Node recently introduced an API that does
exactly what Webpack needs:
https://nodejs.org/api/modules.html#modules_module_builtinmodules

So use that instead and keep the old path as a fallback.
@jsf-clabot

jsf-clabot commented Jan 27, 2018

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@addaleax

Copy link
Copy Markdown
Contributor Author

I am not sure why that Windows build failed, on Node 8 there really shouldn’t be any difference here, and certainly not any OS-dependent one. (I can still try to take a look on Windows later, but it might be nice to know whether this is a known issue or not.)

@ooflorent ooflorent closed this Jan 28, 2018
@ooflorent ooflorent reopened this Jan 28, 2018
@sokra

sokra commented Jan 30, 2018

Copy link
Copy Markdown
Member

I can reproduce the CI failures locally, but don't understand why this is happening... It's pretty weird.

It gets even weirder, because adding a console.log actually fixes the problem:

  class NodeTargetPlugin {
  	apply(compiler) {
+ 		console.log();
  		new ExternalsPlugin("commonjs", builtins).apply(compiler);
  	}
  }

@addaleax

Copy link
Copy Markdown
Contributor Author

Just tried it on Windows and got the same issue - this is really weird. Also, this only seems to happen with Node >= 8.0.0, so there's a decent chance this is a bug in Node.

A quick bisect using nightlies shows that the offending commit in Node would be somewhere in nodejs/node@54d3431...0b5191f

I'll try to investigate a bit more!

@addaleax

addaleax commented Jan 30, 2018

Copy link
Copy Markdown
Contributor Author

Oh, fun; this makes the test pass too:

 class NodeTargetPlugin {
        apply(compiler) {
+               const d = new Date().getTime()
+               while (new Date().getTime() - d < 100);
                new ExternalsPlugin("commonjs", builtins).apply(compiler);
        }
 }

So it's probably an existing race condition in webpack or its tests.

Okay, this is at least partly a Node bug: nodejs/node#18463

@sokra

sokra commented Jan 30, 2018

Copy link
Copy Markdown
Member

Great work on investigating this bug @addaleax

@webpack-bot

Copy link
Copy Markdown
Contributor

@addaleax Thanks for your update.

I labeled the Pull Request so reviewers will review it again.

@ooflorent Please review the new changes.

@addaleax

Copy link
Copy Markdown
Contributor Author

@sokra Thanks! I think I have a proper workaround here (that would be good to include, regardless of the original patch). Let's see what CI has to say.

@ooflorent

ooflorent commented Jan 30, 2018

Copy link
Copy Markdown
Contributor

Wow... I wasn't expected this PR to result into node's bugfix! Great job 👍
However, I'm not sure if webpack should ship a workaround. If so, maybe we should add a TODO to remove the workaround some day.

@addaleax

Copy link
Copy Markdown
Contributor Author

@ooflorent I'm happy with whatever you prefer. Adding a TODO seems fine to me, and I agree that the workaround can be removed once the fix has made its way into Node 8, but without any kind of workaround I don't think this would pass CI. :/

@ooflorent

Copy link
Copy Markdown
Contributor

master must be compatible with node 4.

Work around nodejs/node#18463 by
detecting the conditions under which the bug occurs and
performing a simple operation that resets the error state
if necessary.
@addaleax

Copy link
Copy Markdown
Contributor Author

@ooflorent Yup. I've updated with that + a TODO comment, let me know if that's fine with you

@ooflorent ooflorent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

@webpack-bot

Copy link
Copy Markdown
Contributor

Thank you for your pull request! The most important CI builds succeeded, we’ll review the pull request soon.

@sokra sokra merged commit 9323ee6 into webpack:master Feb 1, 2018
@sokra

sokra commented Feb 1, 2018

Copy link
Copy Markdown
Member

Thanks

@addaleax addaleax deleted the no-binding branch February 1, 2018 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants