close
Skip to content

Consider hardlinks rather than separate copy of packages per app #499

Closed

Description

@Daniel15

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.

Activity

  1. cpojer commented on Oct 5, 2016

    @cpojer
    Contributor

    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.

  2. dxu commented on Oct 5, 2016

    @dxu
    Contributor

    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).

  3. Daniel15 commented on Oct 5, 2016

    @Daniel15
    MemberAuthor

    Are hardlinks different in this case?

    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.

  4. cpojer commented on Oct 5, 2016

    @cpojer
    Contributor

    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.

  5. Daniel15 commented on Oct 5, 2016

    @Daniel15
    MemberAuthor

    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) 😄

  6. sebmck commented on Oct 5, 2016

    @sebmck
    Contributor

    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.

  7. Daniel15 commented on Oct 5, 2016

    @Daniel15
    MemberAuthor

    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:

    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.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #499 (comment), or mute
    the thread
    https://github.com/notifications/unsubscribe-auth/AAFnHQ9ON3xMLDxpGTj6kr0zMZII1hb6ks5qw5blgaJpZM4KOai0
    .

  8. sebmck commented on Oct 5, 2016

    @sebmck
    Contributor

    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?

  9. yunxing commented on Oct 5, 2016

    @yunxing
    Contributor

    @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.

  10. sebmck commented on Oct 5, 2016

    @sebmck
    Contributor

    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.

  11. yunxing commented on Oct 5, 2016

    @yunxing
    Contributor

    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.

  12. Daniel15 commented on Oct 11, 2016

    @Daniel15
    MemberAuthor

    Going to reopen this for tracking purposes. It should be doable as hardlinked files look identical to the file system. I might prototype it.

  13. 57 remaining items

  14. Artoria2e5 commented on Jun 15, 2020

    @Artoria2e5

    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.

  15. Hecatron commented on Oct 11, 2020

    @Hecatron

    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.

  16. Daniel15 commented on Oct 11, 2020

    @Daniel15
    MemberAuthor
  17. Hecatron commented on Oct 11, 2020

    @Hecatron

    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

  18. dhakehurst commented on Oct 13, 2020

    @dhakehurst

    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.

  19. Hecatron commented on Oct 13, 2020

    @Hecatron

    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 some tools they already have inbuilt support - https://yarnpkg.com/features/pnp#native-support
    • 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.

  20. jeffbski commented on Oct 13, 2020

    @jeffbski

    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.

  21. Artoria2e5 commented on Dec 23, 2020

    @Artoria2e5

    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.

  22. Hecatron commented on Dec 23, 2020

    @Hecatron

    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)

  23. Artoria2e5 commented on Dec 24, 2020

    @Artoria2e5

    @grbd, yes that's how junctions work.

  24. merceyz commented on Jan 3, 2021

    @merceyz
    Member

    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

  25. larixer commented on Aug 22, 2022

    @larixer
    Member

    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_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.

  26. callaginn commented on Feb 23, 2024

    @callaginn

    @larixer Using nmMode: hardlinks-global doesn't seem to be linking to a global store in Yarn 4.

    Right now my .yarnrc.yml file looks like this:

    enableGlobalCache: true
    nmMode: hardlinks-global
    nodeLinker: pnpm

    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.

  27. larixer commented on Feb 27, 2024

    @larixer
    Member

    @callaginn The information I provided was for node-modules linker. The pnpm linker has different behaviours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions