-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
If $NODE_PATH
ends with a delimiter, require
will act unexpected
#1488
Conversation
Due to my env issue, I implemented it in a wrong way, this fixes it. More detail: nodejs/node#1488
I suppose a question would be if we want to bother fixing this? (The fix itself is simple enough.) I've never used NODE_PATH before, but it sounds like there are a multitude of other things that could be fed to it where it would break horribly. |
@@ -489,7 +489,7 @@ Module._initPaths = function() { | |||
|
|||
var nodePath = process.env['NODE_PATH']; | |||
if (nodePath) { | |||
paths = nodePath.split(path.delimiter).concat(paths); | |||
paths = nodePath.split(path.delimiter).concat(paths).filter(function (path) { return !!path }); |
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.
style: line should not exceed 80 characters unless you really have to
Yes. Funny enough, this is a breaking change similar to #1356. So it's better to fix it, so people won't rely on this in the future. |
@rlidwka any idea when it broke, out of curiosity? |
@Fishrock123 I don't think anything 'broke' here. The PR looks to fix an unintended resolution of |
Yes, this PR will only affect I create this PR is because there're so many articles encouraging people to use Here's an example why it's dangerous. First, Let's assume there's a module
Without trailing separators, what we get is just an error: So I strongly recommend fixing this issue. |
paths = nodePath.split(path.delimiter).concat(paths); | ||
paths = nodePath.split(path.delimiter).concat(paths).filter(function(path) { | ||
return !!path | ||
}); |
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.
No need to filter paths
here, its entries aren't user-supplied. A small perf gain would be:
paths = nodePath.split(path.delimiter).filter(function(path) {
return !!path;
}).concat(paths);
Also, semicolon.
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.
Also, you could do
return path !== '';
to make it more clear that the intend is to filter the empy string.
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.
I had been thinking path.length !== 0
, but really, !!path
is fine.
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.
Yeah it probably is, given the input is a string it should be quite obvious what it does.
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.
@silverwind @Fishrock123 path !== ''
is slightly faster than !!path
, but !!path
can handle some special situations, e.g. path == null
, in this case, path.length !== 0
might throw an exception.
Using node 1.7.1
!!str x 81,087,829 ops/sec ±1.46% (183 runs sampled)
str !== '' x 88,814,451 ops/sec ±0.98% (184 runs sampled)
str.length !== 0 x 87,047,827 ops/sec ±0.96% (186 runs sampled)
LGTM I've researched the cause and it boils down to @chrisyip if you can, please squash the commits into one and add a commit message and short decription of the problem solved in the commit body as detailed here: https://github.com/iojs/io.js/blob/v1.x/CONTRIBUTING.md#step-3-commit |
If `$NODE_PATH` contains trailing separators, `Module.globalPaths` will contains empty strings. When `Module` try to resolve a module's path, `path.resolve('', 'index.js')` will boil down to `$PWD/index.js`, which makes sub modules can access global modules and get unexpected result.
638ee42
to
00e3423
Compare
If `$NODE_PATH` contains trailing separators, `Module.globalPaths` will contains empty strings. When `Module` try to resolve a module's path, `path.resolve('', 'index.js')` will boil down to `$PWD/index.js`, which makes sub modules can access global modules and get unexpected result. PR-URL: #1488 Reviewed-By: Roman Reiss <me@silverwind.io>
Thanks! merged in 7384ca8 If anyone calls this a breaking change, we can point them to the docs about their invalid NODE_PATH :) |
I'm slightly uneasy about touching |
This is a copy of #1487 that follows contributing guide. Sorry for the trouble.
Here is a repo that explains the difference: https://github.com/chrisyip/NODE_PATH_DEMO.
After separation,
nodePath
will become[ '/usr/local/lib/node_modules', '' ]
(in the above case).I know node should not take the responsibility for env variables, but since
''
is meaningless, I suggest to remove''
fromnodePath
before using it.