Don't resolve symlinks when requiring
Currently, Node.js resolves symlinks when requiring and then uses the real location of the package/file as its __filename and __dirname instead of the symlinked one.
This is a problem because symlinked modules don't act the same as locally copied modules.
For example:
app
index.js //require("dep1")
node_modules
dep1
index.js //require("dep2")
dep2
index.js //console.log('fun!'):
works, but
app
index.js //require("dep1")
node_modules
dep1 -> ../../dep1
dep2
index.js
dep1
index.js //require("dep2")
does not, because dep1 does not act like it's located in the node_modules directory of module and thus cannot find dep2.
This is especially a problem when one wants to symlink modules that have peer-dependencies.
Therefore, I suggest changing the behavior to no longer resolve symlinks. What do you think?
@alexanderGugel,
Note that a package that has side effects should certainly check to see if those side effects have already been applied. Similar to a polyfill. What if a different version of the package (on disk) was already loaded? The same problem would arise.
@alexanderGugel ... I definitely agree that the circular dependency is problematic here. Just working through various scenarios to draw this out further. Let's take another modified version of the peer dependency case I describe in my last comment (https://github.com/nodejs/node/issues/3402#issuecomment-215177747)
Let's make baz and optional peer dependency of foo. bar depends on foo and requires baz. myApp4, depends on foo but does not require baz. We have a structure like:
myApp4
/index.js
/node_modules
/foo
/node_modules
/bar --> ../../bar
/bar
/node_modules
/baz
/foo --> ../../foo
myApp4/node_modules/bar/index.js is (to simulate the requirement on baz being present)
module.exports = require('foo');
require('assert')(module.exports.baz);
myApp4/node_modules/foo/index.js is (to simulate baz being optional to foo)
try {
module.exports.baz = require('baz');
} catch (err) {
}
module.exports.bar = require('bar');
Keeping in mind that bar was written with the specific assumption that baz would be present, running this example in v5.11.0 yields:
bash-3.2$ node -v
v5.11.0
bash-3.2$ node index
assert.js:90
throw new assert.AssertionError({
^
AssertionError: undefined == true
at Object.<anonymous> (/Users/james/tmp/symlink/myApp4/node_modules/bar/index.js:3:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/james/tmp/symlink/myApp4/node_modules/foo/index.js:8:22)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
bash-3.2$
Running this example on master yields:
bash-3.2$ ~/node/main/node/node -v
v7.0.0-pre
bash-3.2$ ~/node/main/node/node index
{ bar: { baz: 2, bar: { baz: 2, bar: [Object] } } }
bash-3.2$
Which, again, is what (I think) one would expect.
Sorry @jasnell I just need to ask to revert this PR asap again.
Because I just found it not only affect performance due to cache, but also BREAK all three alternative package managers totally!
Just try install babel-cli in ied, pnpm, npminstall, and run babel-node xxx.js
Result:
pnpm and npminstall just hang until exaust 1.4GB memory limit. (Same as https://github.com/nodejs/node/issues/3402#issuecomment-215166795 case)
ied report error:
> @ test /Users/hax/repo/contributing/test-module-cache/test/node6-ied
> babel-node .
module.js:440
throw err;
^
Error: Cannot find module 'core-js/library/fn/get-iterator'
at Function.Module._resolveFilename (module.js:438:15)
at Function.Module._load (module.js:386:25)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Users/hax/repo/contributing/test-module-cache/test/node6-ied/node_modules/5f08788603a474f19afc575a8394071afd5fbe0c/node_modules/babel-core/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-types/node_modules/babel-traverse/node_modules/babel-typenpm ERR! Test failed. See above for more details.
(Note the last longlong line of the error info...)
@alexanderGugel what comment are you talking about?
@mcollina .. I believe that was a misedit... I think the comment was meant for @mattcollier
@hax Node 6.x is still fresh, there's no one depending on that specific version already. So we have time to talk about this and package managers have time to find ways to support it.
It's also important to keep in mind that reverting the commit requires a PR which would require some review time before it could be landed, which would then sit for a bit while a new release was put together. That's at least a few days process and the earliest we'd put out a new release is likely next monday or tuesday. Yes, we know that some folks would like the commit reverted but please understand that simply reverting the commit today doesn't actually solve the issues. We can afford to take a bit of time to review the issue further and see if there's a better path forward. Reverting is always a possible course if we can't find a reasonable solution to both problems.
@mcollina @jasnell Yes, sorry about that.
Node 6.0.0 module resolution solves a number of historical problems and makes module resolution much more logical and predictable. Please don't assume node is necessarily the code base that must be altered due to user code reliance on poorly specified behavior. Take the process.exit/stdout output truncation case - evidently it was decided that user modules must change.
Guys... can't we just agree that the new behavior is more natural and expected by most devs at this point? Sorry, @alexanderGugel, I know you disagree here, but maybe you're being dishonest to yourself about this because you're being blinded by your wish to revert to the old behavior. It's not meant offinsive, I really respect your opinion, but I see the possibility of it being the case.
You've mentioned a lot of use cases that are not really a problem with the new behavior, as @jasnell already could point out. I also think that your case with the dependency circle is a non-issue. There's no way you would ever encounter such a problem using a properly working package manager. And if you do it by hand, you won't do it for so many packages, that it will be hard to debug.
I mean, if we argue like this, we could also state that we should ban loops and recursive functions from JavaScript because it allows for endless loops. But if you do it right, this won't happen.
How do other languages treat symlinks? I thought it might be interesting to see how other languages treat symlinks and thus did some research:
C/C++: pragma once does load the file multiple times when it's included through different symlinks Python: import does load a module multiple times when it's imported through different symlinks PHP: include does load a file multiple times when it's included through different symlinks
For me, this makes even more clear that this is expected and wanted that way by developers. I know, I've said it multiple times already, but since it's important I say it again: You can always get the real path from the virtual path. But not the other way around. This means we can always work around the new behavior (pretty or not), but it's almost impossible to work around the old behavior.
Also, I've learnt in programming that it's always best to design the code to support as many use cases as possible, so that more specific things can be built on top of it. I think this is the case for the new behavior.
With this said, I strongly recommend to focus on searching ways to fix the other package managers now. I've already shown a possible way of doing it. Maybe there are better ways. Let's discuss!
while the effort that went into this discussion is great, we're heavily using symlinks for all kinds of stuff. I just tried running our app with Node 6.0.0, but it failed for the reasons from above (private packages, etc). I'm really glad we found this issue, because those kind of changes are really hard to track down.
While certainly purer, the current behavior is documented and we've built a on it expecting it would be there. is there any chance it can be kept?
I want to point out the following comment that was made by @bnoordhuis at the beginning of this conversation:
I mean, if we argue like this, we could also state that we should ban loops and recursive functions from JavaScript because it allows for endless loops. But if you do it right, this won't happen.
Reductio ad absurdum. That being said, it seems like this does in fact introduce at least one bug into the current require mechanism.
Sorry, @alexanderGugel, I know you disagree here, but maybe you're being dishonest to yourself about this because you're being blinded by your wish to revert to the old behavior. It's not meant offinsive, I really respect your opinion, but I see the possibility of it being the case.
I am pretty sure I am not. Resolving to the "real" target paths is the only way how you can guarantee the uniqueness of required packages without doing any of the npm3 "flatten tree" magic (see pnpm). That being said, I'm more than happy to be convinced of the opposite.
The litmus test is this: is there a non-zero chance the proposed change is going to break someone's existing application? If the answer is 'yes' (and I think it is), then it should be rejected.
I think after this discussion it is safe to assume that this does in fact break application code.
With this said, I strongly recommend to focus on searching ways to fix the other package managers now. I've already shown a possible way of doing it. Maybe there are better ways. Let's discuss!
I apologise if I missed that, but what path did you propose for those package managers?
@alexanderGugel No problem. That happens fast in a big discussion like this. Here's the comment
Resolving to the "real" target paths is the only way how you can guarantee the uniqueness of required packages without doing any of the npm3 "flatten tree" magic (see pnpm).
We don't need to uniquely identify files by their absolute path. We already have a system to uniquely identify them: the package-name + the file's relative path in that package. That's all that's needed. You don't even have to use the flatten tree magic, you could do it the way i proposed. And there are potentially other ways.
Reductio ad absurdum. That being said, it seems like this does in fact introduce at least one bug into the current require mechanism.
That's not a bug, it's just the way you've programmed it. It's the same as calling while(true) console.log('hello world') a bug. You could do the same with PHP as well by the way. Is it a bug? I don't think so.
The litmus test is this: is there a non-zero chance the proposed change is going to break someone's existing application? If the answer is 'yes' (and I think it is), then it should be rejected.
I think after this discussion it is safe to assume that this does in fact break application code.
Good point. Did @bnoordhuis approve merging this change?
@alexanderGugel No problem. That happens fast in a big discussion like this. Here's the comment
This does not work, since the directory into which the package has been downloaded needs to be shared by multiple dependencies.
This does not work, since the directory into which the package has been downloaded needs to be shared by multiple dependencies.
Can you maybe further explain this?
So - for example - you can no longer require specific files from a package, e.g. require('my-package/some-file.js') won't work. Most popular example might be rxjs.
@alexanderGugel,
The litmus test is this: is there a non-zero chance the proposed change is going to break someone's existing application? If the answer is 'yes' (and I think it is), then it should be rejected.
I think after this discussion it is safe to assume that this does in fact break application code.
That's the litmus test for whether or not the change should be part of a semver-patch bump as a bug fix. Instead, the change went into a semver-major bump, which is appropriate.
@alexanderGugel -- can you think of a way to modify alternative package managers so that they'll work with node 6.x?
I'm not ready to call it for either side of this discussion just yet as I firmly believe there is a path forward that addresses both concerns. As I said, we can afford a bit of time to explore the issue further.
Drawing out the thought exercise even further (bear with me...). Let's assume that rather than using either the realpath or the symlink as the cachekey, we assume some function of a tuple of both... let's call it f(symlink,realpath). Let's walk through it.
Assuming the following structure using a circular path:
myApp4
/index.js
/node_modules
/baz (@version=1)
/foo
/node_modules
/bar --> ../../bar
/bar
/node_modules
/baz (@version=2)
/foo --> ../../foo
(The tuple is f(symlink , realpath))
Instance 1 of foo =
f(myApp4/node_modules/foo, myApp4/node_modules/foo)
In this case Instance 1 of foo will get baz (@version=1)
Specifically, when resolving foo relative to myApp, it is determined that there is currently no instance of foo relative to myApp, so an instance of foo is created.
Instance 1 of bar =
(myApp4/node_modules/bar, myApp4/node_modules/bar)
Specifically, when resolving bar relative to myApp, it is determined that there is currently no instanceof bar relative to myApp, so an instance of bar is created.
Instance 1 of bar, Instance 2 of foo =
(myApp4/node_modules/bar/node_modules/foo, myApp4/node_modules/foo)
Instance 2 of foo would see baz (@version=2)
Specifically, when resolving foo relative to bar relative to myApp, it is determined that an existing instance of bar exists relative to myApp so it is used, however, an instance of foo relative to bar relative to myApp does not exist, so an instance of foo is created.
Instance 1 of foo, Instance 2 of bar =
(myApp4/node_modules/foo/node_modules/bar, myApp4/node_modules/bar)
Specifically, when resolving bar relative to foo relative to myApp, it is determined that an existing instance of foo exists relative to myApp so it is used, however, an instance of bar relative to foo relative to myApp does not exist, so an instance of bar is created.
Instance 1 of foo, Instance 2 of bar, Instance 1 of foo =
(myApp4/node_modules/foo/node_modules/bar/node_modules/foo, myApp4/node_modules/foo)
Here, when resolving foo relative to bar relative to foo relative to myApp, it is determined that an existing instance of foo exists relative to myApp so it is used. It is determined that an existing instance of bar exists relative to foo relative to myApp, so it is also used. It is then determined that an existing instance of foo relative to bar relative to foo exists as an ancestor to bar, so it is used. A new instance of foo is not created.
Instance 1 of bar, Instance 2 of foo, Instance 1 of bar =
(myApp4/node_modules/bar/node_modules/foo/node_modules/bar, myApp4/node_modules/bar)
Here, when resolving bar relative to foo relative to bar relative to myApp, it is determined that an existing instance of bar exists relative to myApp so it is used. It is determined that an existing instance of foo exists relative to bar relative to myApp, so it is also used. It is then determined that an existing instance of bar relative to foo relative to bar exists as an ancestor to foo, so it is used. A new instance of bar is not created.
This can continue out... note that by checking the ancestor, we can determine if a new instance is required or not.
Instance 1 of bar, Instance 2 of foo, Instance 1 of bar, Instance 2 of foo
(myApp4/node_modules/bar/node_modules/foo/node_modules/bar/node_modules/foo, myApp4/node_modules/foo)
Instance 1 of foo, Instance 2 of bar, Instance 1 of foo, Instance 2 of bar
(myApp4/node_modules/foo/node_modules/bar/node_modules/foo/node_modules/bar, myApp4/node_modules/bar)
Going through this, it appears obvious to me that there's an obvious opportunity to switch up the way the cache key is being computed to ensure that the circular path is dealt with and the peer dependencies are handled appropriately.
Let's make it a bit more extreme... and add a zzz module somewhere in the mix:
myApp4
/index.js
/node_modules
/baz (@version=1)
/foo
/node_modules
/bar --> ../../bar
/bar
/node_modules
/baz (@version=2)
/foo --> ../../foo
/zzz
/node_modules
/baz (@version=3)
/foo --> ../../../../foo
(myApp/node_modules/bar/node_modules/zzz/node_modules/foo, myApp/node_modules/foo)
When resolving foo relative to zzz relative to bar relative to myApp, there is no instance of foo in the direct ancestry. There's an instance of foo relative to bar relative to myApp, but there is no instance of foo relative to zzz relative to bar relative to myApp, so a new instance of foo is created.
Following this approach we would need to split the cache in two. One cache that caches the actual code pulled from the realpath location, and a secondary cache that contains the actual instances of the objects generated by that code. This is so we don't have to go hunting for that code again, we already have a copy in memory that we execute again when a new instance of the module is required.
The tl/dr version is: for every module, we locate it on disk and cache the actual code, we then determine if a new instance of that module is required or not based on whether or not there is an existing instance in the direct require ancestry.
Now, obviously this kind of approach would have a performance impact so we'd have to test the heck out of it to verify that it not only met the expectations but actually performed decently enough to use... and I'm quite certain that I've missed some nuanced detail in here somewhere.
(I seriously hope at least some of that made sense ;-) ...)
(written before @jasnell )
No, I think it is impossible to fully support it. I'm not too worried about the specific strategies of pnpm, ied or npm install, but what I'm kind of afraid of is that this basically means that there is no other way other than either super long path names that break under Windows or a flat dependency graph that takes forever to compute to support a sensible installation mechanism that serves identical package versions to different dependencies.
The old behaviour opened the door to alternatives that relied on symlinks. The new algorithm is limiting in that sense. That being said I fully understand and appreciate the reasoning behind this change, but I'm disappointed that the decision was made to break existing packages rather than finding a solution that adds the desired functionality in a backwards compatible manner.