node icon indicating copy to clipboard operation
node copied to clipboard

Don't resolve symlinks when requiring

Open VanCoding opened this issue 11 years ago • 204 comments

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?

VanCoding avatar Oct 16 '15 12:10 VanCoding

https://github.com/nodejs/node/pull/5950/files#diff-d1234a869b3d648ebfcdce5a76747d71R119 It's broken all faster node package installers. I think module api is locked long ago, why we need to chagne this behavior?

fengmk2 avatar Apr 27 '16 11:04 fengmk2

@hax @fengmk2 Well, it's a new major version, so it's allowed (and expected) to introduce breaking changes. If you're relying on the old behavior, there are still the old versions, including LTS.

Also please do not forget, that the previous behavior was not documented in any way, so packages that now are broken made use of undocumented behavior, which you always have to expect to break.

I think me and others made very clear why this change was needed.

The good thing is: There should be ways to fix the now broken packages to work with Node 6.x again, since you can always get the real path from the virtual path. It was not possible the other way around.

VanCoding avatar Apr 27 '16 12:04 VanCoding

It needs changing because resolving symlinks like this is wrong and broken.

Can someone please explain to me why package managers are having issues with this? It just sounds to me like they're using the same fragile behaviour of resolving symlinks when they shouldn't. If they were doing it right already I don't think they would have any issues... regardless of which way node is doing it.

SystemParadox avatar Apr 27 '16 12:04 SystemParadox

Also please do not forget, that the previous behavior was not documented in any way, so packages that now are broken made use of undocumented behavior, which you always have to expect to break.

It is actually documented.

Since Node.js looks up the realpath of any modules it loads (that is, resolves symlinks), and then looks for their dependencies in the node_modules folders as described here, this situation is very simple to resolve with the following architecture:

Source: Addenda: Package Manager Tips

We're heavily relying on symlinks in ied. So do a lot of other projects. This change will result into hard to debug bugs and the benefit is IMO very limited. The previous behaviour is still well-documented and it's even described under Addenda: Package Manager Tips. If not on that, what can we rely on when writing a package manager?

cc @TheAlphaNerd

alexanderGugel avatar Apr 27 '16 12:04 alexanderGugel

And module API is Stability: 3 - Locked https://nodejs.org/api/modules.html#modules_modules, it should be keep very stable.

image

If we lost the module cache, there is no way to make npm install faster and efficient.

fengmk2 avatar Apr 27 '16 12:04 fengmk2

@alexanderGugel wow, you're right. I didn't know this exists.

But still, it's not documented clearly. Actually, it only states that it resolves symlinks when searching for a module. But It does not state that __dirname and __filename in the loaded module will also have the resolved path. It also does not state, that requires from such modules will originate from the resolved path.

So, basically, we could keep the documented behavior and still break your code ;)

@fengmk2 only documented behavior is locked, I think. Changes are still allowed, as long as it follows the spec.

VanCoding avatar Apr 27 '16 12:04 VanCoding

But still, it's not documented clearly.

Since Node.js looks up the realpath of any modules it loads (that is, resolves symlinks), and then looks for their dependencies in the node_modules folders as described here, this situation is very simple to resolve with the following architecture:

I would argue it is extremely clear and breaks a locked API.

alexanderGugel avatar Apr 27 '16 12:04 alexanderGugel

This change will result into hard to debug bugs and the benefit is IMO very limited.

In addition to being poorly documented the previous behavior of symlinked modules was counter-intuitive, ran counter to the common practise of UNIX symlinks and prevented developers from easily testing their modules without copying and installing.

kzc avatar Apr 27 '16 12:04 kzc

@kzc That's a different question and the original reasoning behind the change. It does not, however, justify breaking a locked API that was mentioned under an agenda for package managers.

alexanderGugel avatar Apr 27 '16 12:04 alexanderGugel

@alexanderGugel This new behavior is consistent with my interpretation of the documented module spec.

What breaks in these other package managers exactly? Perhaps we help can find a fix.

kzc avatar Apr 27 '16 12:04 kzc

@VanCoding @kzc

As my understanding, changes are allowed but not in this manner.

This change is too rush (just 2 days before 6.0 release) and lack the careful thought and testing. I already pointed out current behavior is not an undocumented and give the document link, and warned the possible breakings in the comment 29 days before.

Now it breaks all alternative package installers. :rage1:

hax avatar Apr 27 '16 13:04 hax

@hax,

Now it breaks all alternative package installers.

When you say it "breaks" them, do you mean they don't function properly or do you mean they are now less efficient?

As I said here:

https://github.com/nodejs/node/pull/5950#issuecomment-215101726

This was actually broken behavior for another use case, with no recourse, prior to the fixing of this bug.

dlongley avatar Apr 27 '16 14:04 dlongley

Thanks for the heads up on this. Indeed this will break pnpm's structure, bummer! On Apr 27, 2016 9:48 PM, "HE Shi-Jun" [email protected] wrote:

@VanCoding https://github.com/VanCoding @kzc https://github.com/kzc

As my understanding, changes are allowed but not in this manner.

This change is too rush (just 2 days before 6.0 release) and lack the careful thought and testing. I already pointed out current behavior is not an undocumented and give the document link, and warned the possible breakings in the comment 29 days https://github.com/nodejs/node/issues/3402#issuecomment-202921652.

Now it breaks all alternative package installers [image: :rage1:]

— 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-215088696

rstacruz avatar Apr 27 '16 14:04 rstacruz

Since Node.js looks up the realpath of any modules it loads (that is, resolves symlinks)

https://nodejs.org/download/release/v6.0.0/docs/api/modules.html#modules_addenda_package_manager_tips v6.0.0 module document still tell us will resolves symlinks.

fengmk2 avatar Apr 27 '16 14:04 fengmk2

cc @nodejs/ctc

fengmk2 avatar Apr 27 '16 14:04 fengmk2

@fengmk2,

v6.0.0 module document still tell us will resolves symlinks.

For the main script, it will dereference, otherwise it will not.

dlongley avatar Apr 27 '16 14:04 dlongley

@dlongley any modules it loads it's meaning every module, not only the main script.

fengmk2 avatar Apr 27 '16 14:04 fengmk2

and then looks for their dependencies in the node_modules folders...

It still looks for dependencies the same way. If you symlink to a module that has a node_modules subdir in its realpath, it will look there for dependencies. This behavior has not changed. What it will not do is change the identifier for the module to the realpath, thereby breaking module resolution behavior when moving up the path. That documentation does not say what path will be used to identify the module you loaded, it just says how it will find dependencies.

dlongley avatar Apr 27 '16 14:04 dlongley

When looking for dependencies, the most sane, expected behavior is to look in any local node_modules directories and then move up the path -- from where the module was required, not from its dereferenced location on disk. If you do use the dereferenced path, you won't pull in modules that are specified in your project, you'll pull things from somewhere else, which is unexpected and you can't do anything to fix it. If you specify a framework in your project, that is the one that should load your plugin, not some other framework elsewhere on disk.

dlongley avatar Apr 27 '16 14:04 dlongley

I've been thinking about this more in the context of caching package managers and the package manager tips. Perhaps I am missing something, but shouldn't that still work as described even with this change?

Maybe someone with package manager issues could explain in more detail what's going wrong? @rstacruz, how does this break pnpm? I looked at your .store layout and I can't see why that would have any issues with this fix (on a side note, pnpm looks awesome! I am definitely going to try it).

SystemParadox avatar Apr 27 '16 15:04 SystemParadox