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?
(WHOA! I got a thumbs up from @alexanderGugel !! ;-) Maybe these github reactions aren't too bad afterall!)
I don't agree "the new behavior is more natural and expected by most devs".
I will try my best to explain my opinion. (pls forgive my poor english)
Symbol links is NOT equals to copy.
From the start, symbol links naturally have two different aspect, follow the link OR not follow the link. For example, what's the parent directory of a symbol link? It depends on whether you follow or not follow. If not follow, you just treat it like copy. You may think it's more natural. But there are some IMPORTANT cases you must follow. Eg. executing script. That's why there is a special case even in this new behavior -- main script.
I could argue that most devs familiar with the symbol links of main script so why they should change the mind in other place? Such inconsistent is painful.
But I'd like to clarify that I never against this issue, and I also meet the npm link and peerDeps problem (that's why I watch this issue.)
The problem here is we should not ship such changes without carefully thought and testing. In my memory, node core is conservative. For example, it cost several years to discuss whether to add promise-based api though adding api will not break any existing codes. On the other side, this case are too hurry and break all alternative package managers which is valuable part of Node ecosystem. I can not understand those actions are based on coherent rules.
It's harmful and unfair to block the users of alter package managers to upgrade to 6.0. Most alt package users are enterprise users and use it in CI env for performance. They give many useful feedback. For example as my knowledge Alibaba which is one of the biggest node heavy player use tnpm which rely on npminstall. So current situation make Alibaba node devs can not try 6.0 and give the feedbacks.
That's why I suggest this problem should be resolved asap.
Of course I hope we can find a solution satisfy both requirements. But as my experiences in module resolutions I suspect we can achieve this goal very quickly. I hate revert too, but sometimes it's a best choice.
Thank you for hear my voice.
@hax ... we are definitely working to resolve the problem as quickly as possible but it's not something that can be fixed today as it would likely still take several days (up to a week) before a new release could be made. Right now we're discussing options for fixing and reverting the most recent change is definitely one of the options. What I'm trying to do before we decide is take just a bit of time to figure out what the additional options are.
It's about time for me to get the kids from school now so I'm going to drop off for a bit. Will be back on later to continue the discussion / exploration of this.
@jasnell The problem with your approach is that you still get different instances of the module, when using different symlinks, right? If I understand the problem of other package managers correctly, they want to store all packages in one single folder, and symlink them together, so that the hierarchy is completely flat.
@alexanderGugel Thanks, I didn't think about that. But is still think there are ways to do it, but they may be more complex. For example, package managers could add the following to the beginning of every .js file:
var __filenameReal = require("fs").realpathSync(__filename); if(__filenameReal != __filename) return require(__filenameReal);
But that would also just be solving the tip of the iceberg. There are a lot of edge cases, like files with shebangs, files with other endings like coffescript, or files that never get required at all. The package manager would have to know which files are directly required from other packages, which would need a static analysis, like for example browserify does, which would maybe be even worse performance-wise.
But, as I said in the 2nd part of this comment, we could also just conclude that our problems with peer dependencies originate from NPM and not from require and that maybe NPM needs to fix this instead, so that we can revert to the previous behavior.
But then again, it just feels wrong to use the absolute path to uniquely identify a module. Maybe, we could also make it configurable through a command-line option. That might not be the worst of all options we have.
I'm using pnpm and :heart: it as much as the next person.
I still think this new approach of resolving dependecies based on the symlinked path is the preferred behaviour. Anything else is just super confusing (had a really hard time debugging this the first time I encountered it).
I also think that requiring from two different paths -> two instances is preferred behaviour. Having the behaviour change depending on if it's a symlinked file or not is much more confusing.
Both of the old behaviours require implicit knowledge of the inner workings. The new approach provides the better abstraction, less implicit knowledge and less cognitive overhead.
Keep it :+1:
Not quite. With the approach I suggest there are two caches. One that caches the code based on realpath, and a second that caches instances relative to ancester context. A module requiring 'foo' would get either an existing or new instance of foo based on whether or not an instance already exists in its ancestry. If the symlinks point to the same location, the code would come from that same location. On Apr 27, 2016 3:03 PM, "Patrik Stutz" [email protected] wrote:
@jasnell https://github.com/jasnell The problem with your approach is that you still get different instances of the module, when using different symlinks, right? If I understand the problem of other package managers correctly, they want to store all packages in one single folder, and symlink them together, so that the hierarchy is completely flat.
@alexanderGugel https://github.com/alexanderGugel Thanks, I didn't think about that. But is still think there are ways to do it, but they may be more complex. For example, package managers could add the following to the beginning of every .js file: var __filenameReal = require("fs").realpathSync(__filename); if(__filenameReal != __filename) return require(__filenameReal);
But that would also just be solving the tip of the iceberg. There are a lot of edge cases, like files with shebangs, files with other endings like coffescript, or files that never get required at all. The package manager would have to know which files are directly required from other packages, which would need a static analysis, like for example browserify does, which would maybe be even worse performance-wise.
But, as I said in the 2nd part of this https://github.com/nodejs/node/pull/5950#issuecomment-215109810 comment, we could also just conclude that our problems with peer dependencies originate from NPM and not from require and that maybe NPM needs to fix this instead, so that we can revert to the previous behavior.
But then again, it just feels wrong to use the absolute path to uniquely identify a module. Maybe, we could also make it configurable through a command-line option. That might not be the worst of all options we have.
— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/nodejs/node/issues/3402#issuecomment-215244154
@jasnell if this that either or perhaps maybe with multiple caches and a symlink. real clear, easy debugz :scream:
Well I might have another option:
What if we make require's behavior configurable through an additional file, for example ".require_realpath".
Example:
We do require('foo/lib/bar.js');
Now, when require finds the file and is about to load it, it checks if 'bar/.require_realpath' exists. If it does not, the file is required the new way. If it exists, it is loaded the old way.
This would be very easy to implement for package managers and would need very little changes for node.js.
But this sounds too good to be true, what are the drawbacks of this?
FWIW, after having skimmed this thread, resolving using symlinks will be important when using es6 modules. Since the path cannot be resolved at runtime, and when you want to choose between a Debug or Release build for a native module. That's really far ahead, but just throwing it out there.
@trevnorris Can you elaborate on that? Or can you give an example? I'm not sure I understand how ES6 modules would affect this.
@VanCoding I was just about to suggest something similar to your proposal with dot files except at directory granularity. Please correct me if it is the same proposal as yours. If a dot file .node_legacy exists in a directory then any symlinked file in that directory will be followed as node 5.x and earlier versions did. If it's not there then the new non-symlink-following module resolution heuristics would be used. The main script would still follow legacy symlink resolution rules regardless.
I'd also ask for a new node command line flag or environment variable to override the module resolution behavior to be all legacy or the all new way and the dot files would be ignored altogether.
@kzc yes, that's pretty much the same as my proposal. With the only exception that require only looks up the '.node_legacy' file when the path starts with a package name. Absolute and relative paths are always required the new way.
But I'm not sure what would be better. We'd have to elaborate the use cases...
Also, I'm +1 for the additional command line flag.
Also, I'm +1 for the additional command line flag.
An environment variable setting of some sort has the advantage of letting you run executable shebang JS scripts using the node binary already in your path. But at this point, I'd accept either way or both.
I think an environment variable would make more sense, but it might also confuse people even more....
Why I think an environment variable makes more sense: The primary use case for the new require strategy is when you symlink packages for local development. So I would imagine that that's something you only want to do temporarily.... then you should fall back to what is actually installed...
@alexanderGugel One could also argue that the primary use case for the old behavior is to allow package managers to do cache optimization, and for everything else, the new system can be used,
Then, the benefit of having dotfiles would be that you don't have to mess with environment variables or command line arguments because the package manager does it for you.
But honestly, I like the direction this is going. Make it configurable and everyone is happy either way.
@trevnorris ... it's important to point out that the new behavior still allows resolving via symlinks. The difference, however, is that the module instances are cached based on the symlink path rather than the realpath, which means multiple instances of the same code will be loaded based on the symlink.
@alexanderGugel and @VanCoding ... let's walk through the dot file approach a bit and see where things land (let's use .module_legacy for this hypothetical file)
Scenario 1: No .module_legacy
myApp
/index.js
/node_modules
/baz (@version=1)
/foo --> ../../foo
/bar
/node_modules
/baz (@version=2)
/foo --> ../../../../foo
modules
/foo
/node_modules
/bar --> ../../../myApp/node_modules/bar
In this case, there are no .module_legacy files anywhere. The new semantics apply. Each symlinked foo is it's own instance and we end up with the circular dependency huge cache problem. However, the baz peer dependency is correctly resolved by each separate symlinked instance of foo.
Scenario 2: myApp/.module_legacy
myApp
/.module_legacy
/index.js
/node_modules
/baz (@version=1)
/foo --> ../../foo
/bar
/node_modules
/baz (@version=2)
/foo --> ../../../../foo
modules
/foo
/node_modules
/bar --> ../../../myApp/node_modules/bar
In this case, the .module_legacy file exists at the top level of myApp. In this instance, the old rules would apply in all cases. You'd have a single instance of foo loaded loaded avoiding the circular dependency problem but Node would fail to locate the baz peer dependency because it is not in a path relative to the realpath of foo.
Scenario 3: modules/foo/.module_legacy
myApp
/index.js
/node_modules
/baz (@version=1)
/foo --> ../../foo
/bar
/node_modules
/baz (@version=2)
/foo --> ../../../../foo
modules
/foo
/.module_legacy
/node_modules
/bar --> ../../../myApp/node_modules/bar
In this case the .module_legacy file appears only within foo. What we would end up here is a mix of new and old behaviors. The module myApp/node_modules/bar would be loaded under the new rules, while the module/foo would be loaded under the old. It's a bit more difficult to reason about what it happening here because of the circular dependency. For instance, if I do require('bar') from inside myApp/index.js would I get the same instance of bar that I would get if I do require('bar') from inside module/foo/index.js?
Also, which version of baz is foo going to get when it does require('baz').
Scenario 4: myApp/node_modules/bar/.module_legacy
myApp
/index.js
/node_modules
/baz (@version=1)
/foo --> ../../foo
/bar
/.module_legacy
/node_modules
/baz (@version=2)
/foo --> ../../../../foo
modules
/foo
/node_modules
/bar --> ../../../myApp/node_modules/bar
This is similar to Scenario 3 except the .module_legacy file is moved to bar. The same issues apply here as Scenario 3.
Scenario 5: myApp/node_modules/bar/.module_legacy and `modules/foo/.module_legacy
myApp
/index.js
/node_modules
/baz (@version=1)
/foo --> ../../foo
/bar
/.module_legacy
/node_modules
/baz (@version=2)
/foo --> ../../../../foo
modules
/foo
/.module_legacy
/node_modules
/bar --> ../../../myApp/node_modules/bar
In this scenario, both foo and bar have their own .module_legacy files. The app itself uses the new rules. Suppose my myApp/index.js does the following:
const bar = require('bar');
const foo = require('foo');
A single instance of foo should be returned per the old rules, but it will fail to find the peer dependency baz. This ends up suffering the same problem as Scenario 2.
Looking at this, allowing .module_legacy at arbitrary levels gets complicated quickly, and makes it quite difficult to reason about what is happening. Having the .module_legacy file at the top level (or not at all) makes it much easier -- either we're using the new behavior or we're not. A command line flag or environment variable would do the same, but each come with their own cost.
Given this, one possible approach we can take in the short term would be to add a command line flag that, when set, reverts module loading to the prior behavior such that, if a developer does encounter issues with circular dependencies in their dependency graph, they could switch on the flag to revert. I personally think this is a much better approach than using the dot files.
@jasnell
I'm not a big fan of the environment variable / legacy file approach either.
It seems like we're currently talking about two objectives here:
- not breaking application code
- allowing package managers and users that depend on the old semantics to require the resolved path in one way or another
Assuming we're ok with breaking application code (seems like we are pretty ok with it), I want to propose a slightly more radical approach:
Instead of having a single node_modules directory, either have an additional shared_node_modules directory from which symlinks will be resolved according to the old algorithm or somehow indicate that symlinks should be resolved according to the old algorithm using their name.
Approach 1: shared_node_modules
- project
- node_modules
- a -> ../some/where/else (might not even be a symlink)
- shared_node_modules
- a -> ../some/where
Since a is in shared_node_modules, it will be required using the old algorithm.
If node_modules and shared_node_modules are present, shared_node_modules takes precedence.
Approach 2: Prefix
- project
- node_modules
- a -> ../some/_where
_where is prefixed with _, which indicates that _where is internal, meaning it should be resolved according to the old behavior.
Approach 3: Prefix on linkname (probably prettier)
- project
- node_modules
- _a -> ../some/where
_a is prefixed with _, which indicates that _a is internal, meaning it should be resolved according to the old behavior.
Notice that users would still require _a using require('a').
The opposite would also work: We could...
- revert to the old behavior
- resolve symlinks in
extra_node_modulesaccording to the new algorithm.
That way it would be a backwards compatible change, both cases would be supported and it's easier to differentiate between the two strategies. It's also fairly easy to change.
Thoughts?
Thinking more about this I really think approach 3 would be a good compromise. That way we could support both cases, while still allowing package managers (of any kind really) and users that require a more advanced workflow that involves a lot of symlinks (e.g. if you work with a lot of private packages at the same time) to use the original require mechanism.
Would be more than happy to make this change if everyone is ok with it.
Not that this wouldn't conflict with npm package names either: https://github.com/npm/validate-npm-package-name#naming-rules (_ prefix).
cc @rstacruz
@jasnell In the dot file scheme I envisioned the dot file would literally be in the same directory where the symlink lives. If it is present the symlink is followed as in the previous module resolution algorithm, if not then the new resolution scheme would apply. A dot file in a parent directory would not apply to its child directories. Each directory would be independently evaluated as to the resolution scheme used.
Approach 3: Prefix on linkname (probably prettier)
@alexanderGugel this approach look great and easy to follow symlink realpath or not.