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

Make behavior consistent with require.resolve() for "shadowed" core modules #148

Merged
merged 5 commits into from
Apr 8, 2018

Conversation

searls
Copy link
Contributor

@searls searls commented Jan 16, 2018

Resolves #147

  • Add a failing test
  • @ljharb confirms the test looks OK
  • Fix the issue
  • Deal with whatever breaks.

Hows this look for a start?

@ljharb
Copy link
Member

ljharb commented Jan 16, 2018

Test looks great (along with the appropriate fixtures).

@searls
Copy link
Contributor Author

searls commented Jan 18, 2018

I'm a bit stumped on how to proceed. Internally to Node's require and require.resolve functions is this check which invokes NativeModule.nonInternalExists, which isn't (AFAICT) publicly available.

As a result, short of shipping a white list of known-to-be-internal module names, I'm unsure of any solution that wouldn't reach into Node internals, which (I presume) would defeat the purpose of this module for use in browserify'd code.

Is there anything in browserify to detect and intercept native modules, or is it actually relying on this behavior (of preferring shadowed modules over internals) in order to accomplish that?

@ljharb
Copy link
Member

ljharb commented Jan 19, 2018

We already have a whitelist of known-to-be-core module names; what's an example of an "internal" module name you'd want to use?

@searls
Copy link
Contributor Author

searls commented Jan 19, 2018

Well, the example I've been using all along is "util", which I'm sure you have covered. I'd also expect not-publicly-documented-but-publicly-accessible modules like 'module' would be covered by it.

Where is this list so this condition can use it?

Given 'util' as an example native module which is also installed as an
npm package, then in order for browserify/resolve to act consistently
with native `require.resolve`, these should be true:

```
resolve.sync('util') // returns 'util'
resolve.sync('util/') // returns 'node_modules/util/index.js'
```
@searls
Copy link
Contributor Author

searls commented Mar 23, 2018

I got this new test passing, but identified what I think will be a problem in that this new behavior conflicts with #25. It's my opinion that #25 was essentially incomplete/incorrect, because even as far back as 0.10.48, require.resolve behaved the way specified in this PR's test (that is to say, 'punycode' would always return the core module wherewas 'punycode/' would return the shadowed one).

Since it's duplicative, I would recommend deleting the tests @michaelficarra added in #25. (which I did here: d00883c )

This does raise a decision point, however, which is that surely there are people who are depending on this behavior. Perhaps they want to shadow, maybe even transitively, a native module with an npm package and don't want to or cannot change the string passed to require() to append a '/' character.

Thoughts?

These are obviated by the shadowed_core test added in browserify#148
searls added a commit to searls/resolve that referenced this pull request Mar 23, 2018
These are obviated by the shadowed_core test added in browserify#148
@ljharb
Copy link
Member

ljharb commented Mar 23, 2018

Given the comments on that issue, I think you're right - let's make sure we're testing the proper thing, and if this is a breaking change, so be it (there's others I can make at the same time).

If someone's doing require('punycode') and relying on it resolving to node_modules/punycode rather than the core module, then I think they need to add the slash. resolve already exports isCore, so they have the tools to work around it.

Copy link
Member

@ljharb ljharb left a comment

Choose a reason for hiding this comment

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

Looks good - since this is breaking, I'm going to let it sit a bit before merging.

@ljharb ljharb self-assigned this Mar 23, 2018
@searls
Copy link
Contributor Author

searls commented Mar 24, 2018

Thanks @ljharb -- how will you know that it's sat long enough? I've got a few users waiting on this to land, because it's causing an upstream bug in testdouble.js

@ljharb
Copy link
Member

ljharb commented Mar 28, 2018

I'm traveling atm, but after I'm back in town, I plan to try to merge any outstanding non-breaking items, and cut a release, and then i'll take another look at this one.

@searls
Copy link
Contributor Author

searls commented Mar 29, 2018 via email

@ljharb ljharb force-pushed the 147-shadowed-core-dependencies branch from 60a5568 to b2e51ef Compare April 5, 2018 06:04
ljharb pushed a commit to searls/resolve that referenced this pull request Apr 5, 2018
These are obviated by the shadowed_core test added in browserify#148
@ljharb ljharb force-pushed the 147-shadowed-core-dependencies branch from b2e51ef to 7faf457 Compare April 5, 2018 06:32
ljharb pushed a commit to searls/resolve that referenced this pull request Apr 5, 2018
These are obviated by the shadowed_core test added in browserify#148
@ljharb ljharb force-pushed the 147-shadowed-core-dependencies branch from 7faf457 to ce2ebb1 Compare April 5, 2018 20:09
ljharb pushed a commit to searls/resolve that referenced this pull request Apr 5, 2018
These are obviated by the shadowed_core test added in browserify#148
@ljharb ljharb force-pushed the 147-shadowed-core-dependencies branch from ce2ebb1 to 9e3104b Compare April 7, 2018 22:31
@ljharb ljharb merged commit 197288e into browserify:master Apr 8, 2018
@searls
Copy link
Contributor Author

searls commented Apr 8, 2018

Yay! As soon as you cut a release, I can fix the downstream issues my users were having

@searls searls deleted the 147-shadowed-core-dependencies branch April 8, 2018 12:40
@searls
Copy link
Contributor Author

searls commented Apr 16, 2018

Looks like this landed in 1.7.1 -- Thanks @ljharb!

@ljharb
Copy link
Member

ljharb commented May 16, 2019

This has not yet actually landed in v1; I'll try to backport it.

ljharb added a commit that referenced this pull request May 16, 2019
@ljharb
Copy link
Member

ljharb commented May 16, 2019

Done in 4c9b260; it'll be in the next v1 release.

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

Successfully merging this pull request may close these issues.

When core modules are shadowed, behavior differs from require.resolve
2 participants