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?
@jkrems,
Each file is executed exactly once.
A symlink is a file. With the new behavior, this statement is now actually true.
@jkrems that depends on your definition of a file. Simply said - a symlink is a file that points to another file. The question is now: Should the symlink be resolved and should the resolved path used for caching, module lookup etc. or should the unresolved symlink be used instead?Half of the posts in this issue are exactly about that question.
About your specific problem: I don't think this affects the ecosystem, but rather only local installations. Simple reason: A lot of modules are under git version control and git doesn't allow symlinks. But that's just my personal view. I might be wrong.
Edit: What @dlongley said 😄
@benurb git does support symlinks (at least when you only care about *nix, not sure about support on Windows). And I know of quite a few projects that check symlinks into git. And that do depend on the fact that the require semantics are "same file, as in you-edit-one-and-both-change". It was part of the contract for all this time, after all.
To be clear: I'm not saying "never ever make this change". I'm saying: making a change to a locked part of the API contract of node without deprecation and/or opt-in transition doesn't look good.
@jkrems Windows doesn't support them afaik. But that's another topic. Sorry for bringing this up. Back to the main topic: "same file, as in you-edit-one-and-both-change". As @dlongley and I pointed out this is not the case. If you edit the content of the symlink target only its content changes. Changing the content of a symlink itself means changing the path the link points to. The link and the target are technically two files. You can see that e.g. from the fact how rm behaves. You don't need to call rm -r even if a symlink points towards a directory. Simply because it's a file.
In *nix a file consists of a name and an inode (its content). As both, the symlink and its target, have a name and a content they are in fact two different files.
@benurb I don't think a philosophical argument about "what's the content of a symlink / what's in a name" is furthering the discussion. All I said was: There are apps that depend on the current behavior and it was well-known to be part of a locked API node provided. And, again, I'm not saying that it's better or worse than the new behavior. I'm saying changing it without any process around rollout isn't in line with how "locked" is described in the docs.
Yeah, sorry for the lengthy explanation. We're on the same page here as I agree the change didn't happen as smooth as it should have. The thing is that this change fixes a problem I experienced in a lot of projects since I started using peer deps regularly. I like the new behavior and don't think it should be reverted. On the other side I see that this causes problems with linked modules, but these can probably be fixed with the solutions provided above (_ dirname, etc.). But linking files instead of modules/directories is something completely different and imho behaves correctly the way you observed it.
@jkrems, as hashed out earlier in this thread, the documentation is inconsistent on this issue. This led to people having mixed expectations and surprises -- and some cases simply not working at all. Because the old behavior could be considered a bug, an original proposal was to make this change without changing semver-major, but that was rejected because it was determined that it was possible that some people were explicitly relying on the old behavior. This was also before it came to light that alternative package managers used it to achieve greater efficiency. So the change was made as a semver-major change instead.
I agree that, if possible, we should try to provide a means by which people can use the old behavior as needed -- and I think most people on this thread support that idea and have proposed ways forward to achieve that.
@jasnell The problem with using the _ prefix version is that we essentially need to stat twice the number of files, which might result into significant performance regressions for require().
Therefore - just as another idea - what if we choose the appropriate mechanism based on the target of the link, rather than the linkname?
If the symlink points to an absolute path, use the old mechanism, otherwise use the new one.
If the symlink points to an absolute path, use the old mechanism, otherwise use the new one.
Using symilnks pointing to absolute paths is legitimate in the new resolution scheme. So I'd prefer we come up with another explicit mechanism.
Here's yet another unifying module resolution proposal that's a variation of the _ prefix idea:
If a symlinked file or directory has a file of the same name in the same directory with a @ suffix then the old realpath resolution scheme is used on that link, otherwise the new resolution scheme is used.
New resolution scheme:
foo1.js --> ../../foo1.js
bar1 --> /usr/local/whatever1/
Old (realpath) resolution scheme:
foo2.js --> ../../foo.js
foo2.js@
bar2 --> /usr/local/whatever2/
bar2@
It has the advantage of keeping the symlink name intact as well as being simple to understand. Symlinks within the same directory can either use the new or old module resolution scheme on a case by case basis.
@kzc I like this version as well. Would be even simpler to implement actually (could be done by additional stat in readFilePath). Are you working on a PR for this? Or can I pick that up?
I just submitted a PR for the _ solution, but @ as a suffix would be trivial to add. I think both solutions would be fine.
@alexanderGugel I saw your PR which already does most of the work. I'd be happy if you picked it up. Thanks!
The additional @-suffixed-files beside symlinks indicating old realpath behavior has the advantage that it would work with old-style symlink installs using node versions 5.x and prior.
While I think the solution with the @-files is way better that prefixing symlinks with "_", I still believe we're on the wrong road now.
I again want to point out the following scenario:
/a/b/c/d/e -> /e
/e/f/g/h/j.js
now let's do require('/a/b/c/d/e/f/g/h/j.js');
Currently, that's absolutely no problem, just read the file and that's it. With your proposal, we'd have to do the following checks first:
isSymlink('/a')
isSymlink('/a/b')
isSymlink('/a/b/c')
isSymlink('/a/b/c/d')
isSymlink('/a/b/c/d/e')
isSymlink('/a/b/c/d/e/f')
isSymlink('/a/b/c/d/e/f/g')
isSymlink('/a/b/c/d/e/f/g/h')
isSymlink('/a/b/c/d/e/f/g/h/j.js')
And then when we've discovered that /a/b/c/d/e is a symlink, we'd also have to check if /a/b/c/d/e@ exists.
A lot of checks to load a single file, don't you think?
But it gets even more complicated when a path consists of 2 symlinks:
/a/b/c -> /c
/c/d/e -> /e
/e/f/g.js
In this case, we'd have to check for the existence of /a/b/c@ and /c/d/e@ and to decide which one has precedence over the other. Even better, maybe we would have to resolve to some semi-real path. If for example /a/b/c@ exists, but /c/d/e@ does not, the semi-realpath could be /c/d/e/f/g.js.
One mor case:
/a/b/c -> /c
/c -> /d
/d/e/f.js
This is a dangerous situation, because /c@ would not be visible through the virtual path. So we would have to realpath('/a/b/c') first to then check for the existence of /c@, because checking for the existence of /a/b/c@ is a different thing.
I think this approach is just too complex and potentially means a big performance loss.
I know that this approach would give us the maximum flexibility. But do we really need it? Isn't our goal here to keep the new bahavior and find the most lightweight & elegant solution to still support the other package managers?
What I've proposed earlier would exaclty do that. With as little extra work as possible. The only thing that would be even more lightweight would be the environment and command line options. Maybe I should also do a PR so you can see how lightweight it is.
@VanCoding you're talking about your .module_legacy file proposal, right? Maybe it would also be an option to apply a property like "_loadingBehavior": "realpath" to the package.json.
@benurb exactly! Putting it into the package.json file would also be possible, since there always would be a package.json file where we would put a ".module_legacy" file.
It would be faster to just look up the existence of a file than parsing a json file an check if a key exists, though. But you definitely got the concept :)
@benurb 👎 On doing this on a module-by-module rather than symlink-by-symlink basis. It's less flexible and @alexanderGugel's PR shows that the impact on performance is very limited: https://github.com/nodejs/node/pull/6460#issuecomment-215617902
The _-solution does not require an additional check if a file exists, only if the file (or symlink) could not be found in the first place.
On a side note, I would not call it module_legacy, since requiring via the real path of a package should not be deprecated due to the flexibility it provides.
The comparison with PHP above is not really a valid argument IMO, since Node's require is significantly different in the first place. One could also argue that falling back to the parent's node_modules directory is "wrong" from a user's perspective, which is definitely not the case and enables e.g. circular dependencies in the first place.
@VanCoding
With your proposal, we'd have to do the following checks first:
isSymlink('/a') isSymlink('/a/b') isSymlink('/a/b/c') isSymlink('/a/b/c/d') isSymlink('/a/b/c/d/e') isSymlink('/a/b/c/d/e/f') isSymlink('/a/b/c/d/e/f/g') isSymlink('/a/b/c/d/e/f/g/h') isSymlink('/a/b/c/d/e/f/g/h/j.js')
I suggest that parent directory symlinks are not followed to keep the scheme simpler if the alternate package manager folks do not have issue with it. Only the final module directory or JS file link is followed in the presence of a @-suffixed file of the same name in the same directory.
Edit: I previously quoted the wrong section. Fixed.
And then when we've discovered that /a/b/c/d/e is a symlink, we'd also have to check if /a/b/c/d/e@ exists.
That's correct. Let's wait for @alexanderGugel's @-suffix symlink peer file PR before we make any performance judgements.
Yet another variation on the _-prefix and '@' file proposals...
List the symlink names in a file (let's say .module_realpath) in each directory that you wish to use the old realpath symlink module resolution behavior. This would result in fewer stat() calls than the "@ file" proposal.
Still another variation...
Node would only read in a single .module_realpath file at startup located in the same directory where the realpath main script lives and only those named symlinks with be realpath'ed. The location of this .module_realpath file could be optionally overridden by a command line flag --realpath-symlink-file to point to a different file. That way all decisions whether to realpath or not can be done in memory and be completely explicit. (Edit: --module-realpath-file for the flag name may be better.)