You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider hardlinks rather than separate copy of packages per app #499
This was touched on in a comment on #480, but I thought it's worth pulling into its own separate issue.
Currently, each app that uses Yarn (or npm) has its own node_modules directory with its own copies of all the modules. This results in a lot of duplicate files across the filesystem. If I have 10 sites that use the same version of Jest or React or Lodash or whatever else you want to install from npm, why do I need 10 identical copies of that package's contents on my system?
We should instead consider extracting packages into a central location (eg. ~/.yarn/cache) and hardlinking them. Note that this would be a hardlink rather than symlink, so that deleting the cache directory does not break the packages.
Reacted by 8573, Pauan, busybeaver, Sebastian Volland, Vladimir Krivosheev, Christophe S., Zoltan Kochan, K3A, Evan Sangaline, Sean Vieira and 87 more
Reacted by Yiu, Chris, Chiahao Lin (Hao) and Haoqun Jiang
Reacted by Limon Monte, Nikhil, Davin Kevin, Michael Neu, Nicolas Beaussart, RandallFlagg, Swoy Derossi, Markus Padourek, Haoqun Jiang, Chris and 2 more
Yarn initially used to use symlinks and we changed it because our internal tooling (watchman etc.) doesn't work well with symlinks. Are hardlinks different in this case? If yes, that might be worth doing.
I think the initial release should continue to use the copy approach; it is more consistent with the rest of the ecosystem and we should evaluate this behavior for a future major release.
Upon thinking about it further, another issue that might come up is that people may try to modify their local node_modules for local debugging purposes or testing purposes, and not expect that they're actually modifying the node module linked to everywhere else. I don't know how often this happens with others, but I've definitely done it (though rarely) in the past. Apart from that, hardlinks seems to make sense. I'd guess that the tooling would be fine, since it should be the same as any other file.
The primary issue this was intended to address was the cache causing issues with hardcoded paths that result from building packages (#480).
Reacted by Rafael Turk, ewnd9, Markus Padourek and Dan Dascalescu
Not sure, might be worth asking @wez whether Watchman can handle hardlinks.
another issue that might come up is that people may try to modify their local node_modules for local debugging purposes or testing purposes, and not expect that they're actually modifying the node module linked to everywhere else
I think this is the use case for npm link or Yarn's equivalent though, right? You're never supposed to directly modify files in node_modules. We could have an option to make a local copy if people want to do this though.
Reacted by Markus Padourek and Charlike Mike Reagent
I totally agree with @dxu and actually wanted to write the same thing. I do this often: I manually add some debugging code into a random node_module (that I don't have checked out locally). Once I'm done, I wipe it away and do npm install. It would be a mental change for me to remember it would also affect other projects.
Yeah, that's a use case I didn't really think about... Hmm...
Oh well, we can still hold on to this idea. Maybe it could be an optional configuration setting for people that don't directly edit node_modules and would like Yarn to run faster (less copying of the same data = less disk IO = stuff is faster and disk cache efficiency is improved) 😄
Going to close this since we decided long ago to go away from symlinks. It's required for compatibility with the existing ecosystem as even projects like ESLint rely on this directory structure to load rules etc. There's also a lot of problems with existing tooling not supporting them. For example when Yarn initially used Jest and it would fail and produce extremely long paths. Jest is much better now and the bug is likely fixed but small issues like this exist in a lot of tools.
Reacted by Charlike Mike Reagent
Reacted by Forrest Hopkins, Vlad Bokov, Hélio, xknight, Stamatios Stamou Jr and Chad Faragher
Going to close this since we decided long ago to go away from symlinks.
It's required for compatibility with the existing ecosystem as even
projects like ESLint rely on this directory structure to load rules etc.
There's also a lot of problems with existing tooling not supporting them.
For example when Yarn initially used Jest and it would fail and produce
extremely long paths. Jest is much better now and the bug is likely fixed
but small issues like this exist in a lot of tools.
@kittens haven't really tested hardlinks. But once you hardlink a file, in theory from the filesystem's perspective, it should be exactly same as the original file -- you can remove the original file and the hardlinked file will still work.
This is different from symlinks, whose content is just a pointer to the original file.
You can have cycles though which is extremely problematic if tools aren't designed to handle them (most JavaScript tools aren't, and how would they?). Hardlinks and symlinks on Windows both require admin privileges (NTFS junctions don't but they're more synonymous with symlinks) which is a non-starter for a lot of environments.
Reacted by Dan Church, Dessalines and Rohan Nolan Lasrado
Going to reopen this for tracking purposes. It should be doable as hardlinked files look identical to the file system. I might prototype it.
Reacted by Veit Lehmann, 8573, Erik Arvidsson, Hawken Rives, Pauan, Matthew Pietz, Andrew Sun, Brian M Hunt, Daniel Bayley, Fernando Montoya and 18 more
Yeah, that thing gets passed down to libuv, but it only supports Linux for now. And oops I have two stale PRs (libuv/libuv#2577, libuv/libuv#2578)... maybe I should check the reviewer comments and stuff.
Any news on if this is a thing in yarn? (hard links), while I like some of yarn's features there's no way I can move away from pnpm for my laptop with the amount of disk space that ends up being used otherwise.
There is an even better option.
Use a virtual filesystem.
The virtual FS can map the real directories to whatever location is needed, as many times as wanted with multiple versions. The modules exist once on the real FS.
That wouldn't be a viable option for a lot of people, or at the very least would be very awkward to setup, if your developing under Windows for example with a ntfs filesystem or Linux with an ext4 filesystem going through the hassle of mounting a block of storage as a "special" filesystem just for developing wouldn't be practical for a lot of folks.
Most people tend to follow the easiest path to a solution.
I'm thinking of giving Plug’n’Play a go to see if there are any compatibility issues with the code I'm running.
On Sun, Oct 11, 2020, 8:28 AM Hecatron ***@***.***> wrote:
Any news on if this is a thing in yarn? (hard links), while I like some of
yarn's features there's no way I can move away from pnpm for my laptop with
the amount of disk space that ends up being used otherwise.
There is an even better option.
Use a virtual filesystem.
The virtual FS can map the real directories to whatever location is
needed, as many times as wanted with multiple versions. The modules exist
once on the real FS.
That wouldn't be a viable option for a lot of people, or at the very least
would be very awkward to setup, if your developing under Windows for
example with a ntfs filesystem or Linux with an ext4 filesystem going
through the hassle of mounting a block of storage as a "special" filesystem
just for developing wouldn't be practical for a lot of folks.
Most people tend to follow the easiest path to a solution.
I'm thinking of giving Plug’n’Play a go to see if there are any
compatibility issues with the code I'm running.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#499 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAWOHJKFM4VQXFWX7VUTA3SKHFJBANCNFSM4CRZVC2A>
.
I've just been playing around with pnp under yarn and I do like it.
I think I'll be moving to that since it's a bit quicker at installing / using depends now than pnpm given the way it works by generating a .pnp.js file
it also only requires some minimal changes to the webpack configs to use it, so I'm kind of sold on that now
That wouldn't be a viable option for a lot of people, or at the very least would be very awkward to setup, if your developing
under Windows for example with a ntfs filesystem or Linux with an ext4 filesystem going through the hassle of mounting a
block of storage as a "special" filesystem just for developing wouldn't be practical for a lot of folks.
Most people tend to follow the easiest path to a solution.
I was not suggesting it as a user solution.
It is something that should be implemented inside yarn.
That wouldn't be a viable option for a lot of people, or at the very least would be very awkward to setup, if your developing
under Windows for example with a ntfs filesystem or Linux with an ext4 filesystem going through the hassle of mounting a
block of storage as a "special" filesystem just for developing wouldn't be practical for a lot of folks.
Most people tend to follow the easiest path to a solution.
I was not suggesting it as a user solution.
It is something that should be implemented inside yarn.
In a way this is what they have sort of done with yarn pnp
Typically a package manager such as yarn, npm or pnpm will download files to a node_modules directory local to the project, the obvious problem is this can use up a lot of disk space.
Lets say you implement a virtual filing system inside of yarn, this doesn't fix javascript being run by node, or being sourced by webpack since they wouldn't know anything about this virtual filesystem without some kind of wrapper.
pnpm's approach is to have a single global directory on the disk to store all the libs, then create a node_modules directory in the project and create hardlinks from there to the global store to save on disk space. This works but I've found can be a bit slower than yarn's pnp approach.
The yarn pnp approach is to download the libs as compressed zips into ether a single global directory store (like pnpm) or local to the project .yarn\releases
depending on which setting you've got configured. It then generates a .pnp.js file
You then have a wrapper that reads in a generated .pnp.js file from yarn.
The wrapper makes it look like your accessing files from a node_modules directory, but in actual fact your accessing .pnp.js which then in turn reads from the files in the zips without the need to store them uncompressed on the disk.
The type of wrapper depends on the tool in use.
For webpack there's a plugin to the resolver called PnpWebpackPlugin
For tools that don't have native support (like the typescript compiler) you can use a tool called pnpify - such as "yarn pnpify tsc" for running the typescript compiler.
I think there may be some edge cases where it doesn't work, but I always have pnpm for that.
npm's approach at something similar is called "tink" I think but doesn't seem to be getting much development at the moment.
Hard links work well to reduce the usage on disk and they work fine on linux, mac, and windows. I haven't found any disadvantages of using them. I currently use a package I wrote called pkglink to create them on my node_modules directories, but it would sure be nice if this was integrated into yarn so one didn't have to run things separately. But you can start using them today with pkglink https://github.com/jeffbski/pkglink Just run it on your js repos or even give it the folder above all of your repos and it will create hard links on the duplicate node_modules files, it verifies versions and file sizes, and dates before linking to make sure files are the same and can be linked.
one thing to keep in mind is that since hardlinks pretend to be the file system so well, deleting them usually deletes way more files than you're expecting
I know I am replying to something very old, but just for the record (since @also is only saying it vaguely) hardlinks should not cause accidental deletions at all. When anything is "deleted" on a filesystem with an inode table (including NTFS w/ MFT), all that happens is the inode gets one fewer reference count; it only gets removed when the count reaches 0.
The only case for accidental deletion to happen is with directory hard links. Almost nobody besides macOS supports that, so I can't care to test. For the record, the n problem stems from a symlink: rm only removes the symlink by default, but when you are asking to delete to something under the symlink it will find it.
So I suspect if you deleted the files inside a junction directory it would also delete them from the directory the junction pointed to (similar to a symlinked directory under Linux)
Closing as PnP is a thing now and the most space efficient option. If this were to be implemented it would happen in v2 and is tracked here yarnpkg/berry#1845
When enabled, the project files inside node_modules will be hardlinked into central content addressable storage at ~/yarn/berry/store, thus occupying disk space only once across all the projects that have this option enabled.
Reacted by XTY and Forrest Hopkins
Reacted by Kogia-sima, Forrest Hopkins and Jaeseok Kang
Is there something I'm missing? It appears to be linking to a hidden ".store" directory within my local node_modules folder, which isn't what I expected.
Activity
cpojer commentedon Oct 5, 2016
Yarn initially used to use symlinks and we changed it because our internal tooling (watchman etc.) doesn't work well with symlinks. Are hardlinks different in this case? If yes, that might be worth doing.
I think the initial release should continue to use the copy approach; it is more consistent with the rest of the ecosystem and we should evaluate this behavior for a future major release.
dxu commentedon Oct 5, 2016
Upon thinking about it further, another issue that might come up is that people may try to modify their local node_modules for local debugging purposes or testing purposes, and not expect that they're actually modifying the node module linked to everywhere else. I don't know how often this happens with others, but I've definitely done it (though rarely) in the past. Apart from that, hardlinks seems to make sense. I'd guess that the tooling would be fine, since it should be the same as any other file.
The primary issue this was intended to address was the cache causing issues with hardcoded paths that result from building packages (#480).
Daniel15 commentedon Oct 5, 2016
Not sure, might be worth asking @wez whether Watchman can handle hardlinks.
I think this is the use case for
npm linkor Yarn's equivalent though, right? You're never supposed to directly modify files innode_modules. We could have an option to make a local copy if people want to do this though.cpojer commentedon Oct 5, 2016
I totally agree with @dxu and actually wanted to write the same thing. I do this often: I manually add some debugging code into a random node_module (that I don't have checked out locally). Once I'm done, I wipe it away and do
npm install. It would be a mental change for me to remember it would also affect other projects.Daniel15 commentedon Oct 5, 2016
Yeah, that's a use case I didn't really think about... Hmm...
Oh well, we can still hold on to this idea. Maybe it could be an optional configuration setting for people that don't directly edit
node_modulesand would like Yarn to run faster (less copying of the same data = less disk IO = stuff is faster and disk cache efficiency is improved) 😄sebmck commentedon Oct 5, 2016
Going to close this since we decided long ago to go away from symlinks. It's required for compatibility with the existing ecosystem as even projects like ESLint rely on this directory structure to load rules etc. There's also a lot of problems with existing tooling not supporting them. For example when Yarn initially used Jest and it would fail and produce extremely long paths. Jest is much better now and the bug is likely fixed but small issues like this exist in a lot of tools.
Daniel15 commentedon Oct 5, 2016
Sebastian, this task is for hardlinks not symlinks. Hardlinks shouldn't
have any of the problems you mentioned.
Sent from my phone.
On Oct 5, 2016 5:24 AM, "Sebastian McKenzie" notifications@github.com
wrote:
sebmck commentedon Oct 5, 2016
Hardlinks have the exact same problems and are semantically the same in this scenario. Why do you think they don't have any of the same issues?
yunxing commentedon Oct 5, 2016
@kittens haven't really tested hardlinks. But once you hardlink a file, in theory from the filesystem's perspective, it should be exactly same as the original file -- you can remove the original file and the hardlinked file will still work.
This is different from symlinks, whose content is just a pointer to the original file.
sebmck commentedon Oct 5, 2016
You can have cycles though which is extremely problematic if tools aren't designed to handle them (most JavaScript tools aren't, and how would they?). Hardlinks and symlinks on Windows both require admin privileges (NTFS junctions don't but they're more synonymous with symlinks) which is a non-starter for a lot of environments.
yunxing commentedon Oct 5, 2016
Good point of Windows. We can have platform specific logic maybe if we decide to go down this path.
How do you create a cycle in hardlink? Note that there is no hardlink for directories.
Daniel15 commentedon Oct 11, 2016
Going to reopen this for tracking purposes. It should be doable as hardlinked files look identical to the file system. I might prototype it.
57 remaining items
Artoria2e5 commentedon Jun 15, 2020
Yeah, that thing gets passed down to libuv, but it only supports Linux for now. And oops I have two stale PRs (libuv/libuv#2577, libuv/libuv#2578)... maybe I should check the reviewer comments and stuff.
Hecatron commentedon Oct 11, 2020
Any news on if this is a thing in yarn? (hard links), while I like some of yarn's features there's no way I can move away from pnpm for my laptop with the amount of disk space that ends up being used otherwise.
That wouldn't be a viable option for a lot of people, or at the very least would be very awkward to setup, if your developing under Windows for example with a ntfs filesystem or Linux with an ext4 filesystem going through the hassle of mounting a block of storage as a "special" filesystem just for developing wouldn't be practical for a lot of folks.
Most people tend to follow the easiest path to a solution.
I'm thinking of giving Plug’n’Play a go to see if there are any compatibility issues with the code I'm running.
Daniel15 commentedon Oct 11, 2020
Hecatron commentedon Oct 11, 2020
I've just been playing around with pnp under yarn and I do like it.
I think I'll be moving to that since it's a bit quicker at installing / using depends now than pnpm given the way it works by generating a .pnp.js file
it also only requires some minimal changes to the webpack configs to use it, so I'm kind of sold on that now
dhakehurst commentedon Oct 13, 2020
I was not suggesting it as a user solution.
It is something that should be implemented inside yarn.
Hecatron commentedon Oct 13, 2020
In a way this is what they have sort of done with yarn pnp
Typically a package manager such as yarn, npm or pnpm will download files to a node_modules directory local to the project, the obvious problem is this can use up a lot of disk space.
Lets say you implement a virtual filing system inside of yarn, this doesn't fix javascript being run by node, or being sourced by webpack since they wouldn't know anything about this virtual filesystem without some kind of wrapper.
pnpm's approach is to have a single global directory on the disk to store all the libs, then create a node_modules directory in the project and create hardlinks from there to the global store to save on disk space. This works but I've found can be a bit slower than yarn's pnp approach.
The yarn pnp approach is to download the libs as compressed zips into ether a single global directory store (like pnpm) or local to the project .yarn\releases
depending on which setting you've got configured. It then generates a .pnp.js file
You then have a wrapper that reads in a generated .pnp.js file from yarn.
The wrapper makes it look like your accessing files from a node_modules directory, but in actual fact your accessing .pnp.js which then in turn reads from the files in the zips without the need to store them uncompressed on the disk.
The type of wrapper depends on the tool in use.
I think there may be some edge cases where it doesn't work, but I always have pnpm for that.
npm's approach at something similar is called "tink" I think but doesn't seem to be getting much development at the moment.
jeffbski commentedon Oct 13, 2020
Hard links work well to reduce the usage on disk and they work fine on linux, mac, and windows. I haven't found any disadvantages of using them. I currently use a package I wrote called pkglink to create them on my node_modules directories, but it would sure be nice if this was integrated into yarn so one didn't have to run things separately. But you can start using them today with pkglink https://github.com/jeffbski/pkglink Just run it on your js repos or even give it the folder above all of your repos and it will create hard links on the duplicate node_modules files, it verifies versions and file sizes, and dates before linking to make sure files are the same and can be linked.
Artoria2e5 commentedon Dec 23, 2020
I know I am replying to something very old, but just for the record (since @also is only saying it vaguely) hardlinks should not cause accidental deletions at all. When anything is "deleted" on a filesystem with an inode table (including NTFS w/ MFT), all that happens is the inode gets one fewer reference count; it only gets removed when the count reaches 0.
The only case for accidental deletion to happen is with directory hard links. Almost nobody besides macOS supports that, so I can't care to test. For the record, the
nproblem stems from a symlink:rmonly removes the symlink by default, but when you are asking to delete to something under the symlink it will find it.Hecatron commentedon Dec 23, 2020
Typically pnpm uses junctions instead of hardlinks for directory pointers / links
So I suspect if you deleted the files inside a junction directory it would also delete them from the directory the junction pointed to (similar to a symlinked directory under Linux)
Artoria2e5 commentedon Dec 24, 2020
@grbd, yes that's how junctions work.
merceyz commentedon Jan 3, 2021
Closing as PnP is a thing now and the most space efficient option. If this were to be implemented it would happen in v2 and is tracked here yarnpkg/berry#1845
larixer commentedon Aug 22, 2022
Update. This feature is supported starting from Yarn 3, via
nmMode: hardlinks-global:https://yarnpkg.com/configuration/yarnrc#nmMode
When enabled, the project files inside
node_moduleswill be hardlinked into central content addressable storage at~/yarn/berry/store, thus occupying disk space only once across all the projects that have this option enabled.callaginn commentedon Feb 23, 2024
@larixer Using
nmMode: hardlinks-globaldoesn't seem to be linking to a global store in Yarn 4.Right now my .yarnrc.yml file looks like this:
Is there something I'm missing? It appears to be linking to a hidden ".store" directory within my local node_modules folder, which isn't what I expected.
larixer commentedon Feb 27, 2024
@callaginn The information I provided was for
node-moduleslinker. Thepnpmlinker has different behaviours.