If $NODE_PATH ends with a delimiter, require will act unexpected#1488
If $NODE_PATH ends with a delimiter, require will act unexpected#1488chrisyip wants to merge 1 commit intonodejs:masterfrom
$NODE_PATH ends with a delimiter, require will act unexpected#1488Conversation
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. |
lib/module.js
Outdated
There was a problem hiding this comment.
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. |
lib/module.js
Outdated
There was a problem hiding this comment.
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.
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.
I had been thinking path.length !== 0, but really, !!path is fine.
There was a problem hiding this comment.
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.
@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,
nodePathwill 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''fromnodePathbefore using it.