Skip to content

[Feature] Automatic hoisting of common dependencies to repo root #334

Closed

Description

@gigabo

This was left out of #237 to reduce its scope, but is still a valuable improvement!

Hoisting external dependencies that are shared across packages:

  • dramatically speeds up bootstrap
  • reduces the storage footprint of a Lerna repo
  • handles the singleton problem where a common dependency (e.g. React) must be shared

Should this be default behavior, or an option (lerna bootstrap --hoist-external)?

It definitely needs a way to opt-out at the dependency level (we have deps internally at Redfin that can't be hoisted). What does that look like? An option like --no-hoist=<externalDep>?

Activity

  1. seansfkelley commented on Sep 8, 2016

    @seansfkelley
    Contributor

    What would be the semantics around different versions of the same dependency scattered around? If I have three packages that depend on foo at ^1.0.0, ^1.1.0 and ^2.0.0, how many versions of foo do I end up with and where are they? I'm assuming a copy of 1.1.0 since it satisfies the first two and one of 2.0.0, both hoisted. This seems to demand the need for some kind of extra-nested fake-node_modules (lerna_modules?) like lerna_modules/foo/1.1.0 and lerna_modules/foo/2.0.0.

  2. gigabo commented on Sep 8, 2016

    @gigabo
    ContributorAuthor

    I think what @rygine originally did in #237 made sense: Hoist the most common version.

    Ideally packages within a repo will generally depend on compatible versions.

    A lerna pin dep@version command would help with that.

  3. laurentdesmet commented on Sep 10, 2016

    @laurentdesmet
    Contributor

    Lerna is a library that is mostly used when you have a lot of packages to manage with a lot of dependencies between them. These kind of repos are the ones that need hoisted dependencies the most, so this is in favor of making it the default option. What are the technical reasons that you can't hoist the dependencies? I can think of dependencies that use the bin property of npm but what are other possible reasons?

  4. rygine commented on Sep 12, 2016

    @rygine
    Contributor

    @laurentdesmet there are potentially a few downsides to hoisting:

    • hoisted modules can conflict with existing modules at the repo root. so, where do the hoisted modules go? with Add support for configurable package locations #332 on the horizon, we can't necessarily guarantee that hoisted modules are located up the directory tree without conflicting with the root.
    • npm outdated becomes broken. this may not be a deal breaker.
    • package.json dependencies may become stale as a result of modules being installed "globally." this is more of a maintenance issue, but it has bitten me.

    the last 2 issues may be solved with https://github.com/maxogden/dependency-check or https://github.com/dylang/npm-check.

    even with the above potential downsides, my team and i were using hoisted modules without any major issues for a few months.

  5. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    I'd rather have consistency, hoisting dependencies has a lot of edge cases that can't be worked around in the npm model.

  6. gigabo commented on Sep 12, 2016

    @gigabo
    ContributorAuthor

    What are the technical reasons that you can't hoist the dependencies?

    Any use of install path.

    For example our module tagger generates logger names such as react-server.core.util.PageUtil using the path to a module relative to the installed location.

    It may be possible to rework that specific package to use working directory, or module.parent, but I doubt we're the only module authors to have relied on the install location in some way.

    even with the above potential downsides, my team and i were using hoisted modules without any major issues for a few months.

    Yeah, we've been manually hoisting deps in our internal (private, not managed by Lerna) repo for a while now.

    The main down-side of manual hoisting is the actual dependency relationship is lost. Which packages need React? How about lodash? Far nicer to maintain dependency relationships per package and automatically factor out the commonality (where possible) at the framework level.

    I'd rather have consistency

    Hi @thejameskyle, welcome back!

    That sounds like an argument for hoisting as an option rather than by default.

    Given the benefits (listed above in the issue description) this is, in my opinion, actually critical for the continued success of Lerna. Long bootstrap time was a key reason for PouchDB dropping Lerna, for example. Another example is Redfin's internal monorepo, which we're hoping to manage with Lerna. Hoisting is a prerequisite for us, both due to performance and enforcement of "singleton" modules such as React.

  7. reopened this on Sep 12, 2016
  8. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    Anything you do here is going to have inconsistency even at an option level. It's the type of feature that you could implement one of ten ways and each person using Lerna is going to want a different thing.

    Lerna insists a certain structure, the more that we remove from it the more confusing the entire experience of using Lerna becomes. If people are unwilling to follow the structure they can go off in their own paths.

    Features like this aren't worthwhile.

  9. laurentdesmet commented on Sep 12, 2016

    @laurentdesmet
    Contributor

    Well, then this means that Lerna is not usable (without tweaking it yourself) for any project that uses singleton modules like React or where each package has a lot of dependencies (resulting in performance issues). Contradictory, these are the projects that need Lerna the most.

    The result of this is that a lot of people that use Lerna, are adding little ad hoc scripts to make it work for them, resulting in a fragmentation. Or others will just give up and leave Lerna altogether. I'm not sure if that will help the project's long-term success.

    Isn't it then the lesser evil to provide such an option to hoist common dependencies?
    Or is it an option that we symlink common modules to some common folder in the root?

  10. gigabo commented on Sep 12, 2016

    @gigabo
    ContributorAuthor

    @thejameskyle What is your vision for the future of Lerna?

    Do you want Lerna to continue to improve in ways that make life easier for the developers who rely on it? Do you want Lerna to continue to live at the heart of diverse and important projects?

    These are the sort of features that are not just worthwhile, but necessary for Lerna to continue to grow and thrive. I understand and appreciate the urge to keep software simple and focused. Part of what makes Lerna such an important tool is that it addresses a single urgent need: Working more effectively in an environment with many node packages. Features that improve that effectiveness do not detract from that focus. They enhance it.

  11. reopened this on Sep 12, 2016
  12. rygine commented on Sep 12, 2016

    @rygine
    Contributor

    Long bootstrap time was a key reason for PouchDB dropping Lerna, for example.

    Our build time went from ~3 mins to ~8mins when we stopped hoisting. It's quickly becoming an issue. We've experimented with keeping node_modules folders around, but that's proving to be error prone when external dependencies need an update even though they meet the dependency requirements.

    Features like this aren't worthwhile.

    I'm not understanding this train of thought when there is clearly a need by the community. I do understand the need for consistency, but it seems that Lerna is being used for more and more use cases and the project should be open to fulfilling those needs.

    If Lerna becomes a Lerna repo, this will be a non-issue as the community can create non-official commands to satisfy its needs.

  13. 2 remaining items

  14. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    The more functionality you add in Lerna the more you remove the ability to easily extend it with additional bits of functionality. When you remove guarantees you make software harder to consume.

  15. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    Solve this by adding subtractive features like skipping the install step and only doing linking. A continuous stream of additive features will make it more difficult to use. Add hooks, not thirty separate feature requests.

  16. laurentdesmet commented on Sep 12, 2016

    @laurentdesmet
    Contributor

    for any project that uses singleton modules like React

    This is not true, we were able to use react across packages on https://github.com/cloudflare/cf-ui. Solve the problem where the problem exists don't push it down stream

    Well, then I'm curious about how you solved it :-). In a normal setting with npm3, npm would flatten everything for you so you end up with exactly one dependency on React (if the semver ranges allow it). But that's not what will happen with the default behavior of Lerna.

    But if I understand you correctly, then you agree on the fact that there are problems to solve relating to performance and singleton modules, but that there are probably better ways than the 'hoisting' solution. Maybe we should rename this issue (or make a new one) to something more general to investigate what possible strategies can be used or provided to fix the performance and singleton issues that users are experiencing right now.

  17. laurentdesmet commented on Sep 12, 2016

    @laurentdesmet
    Contributor

    Btw, forget about my symlinking proposal, because that one doesn't help with the singleton problem.

  18. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    I can't say anything right now, but just wait a bit and the performance problem should be solved soon.

  19. gigabo commented on Sep 12, 2016

    @gigabo
    ContributorAuthor

    Solve the problem where the problem exists

    Isn't Lerna a tool to help solve problems associated with monorepos?

    additive features will make it more difficult to use.

    There is always a tradeoff between power and ease of use.

    Does an option to automatically hoist dependencies cross some threshold?

    What is the answer for devs who want this feature?

    we were able to use react across packages

    We also have solved some of the problems that dependency hoisting addresses. We've manually factored deps into the repo root to improve performance. We've used webpack configuration to make React a singleton in the client bundle. It's a pain. I'd rather not have to deal with that myself. I'd rather the tool I use for managing my repo do it for me.

  20. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    Solve the problem where the problem exists

    The problem of singleton projects doesn't appear in the monorepo. Lerna handles them just fine as they are intended to be installed. If you were working outside of a monorepo in just a bunch of folders it would work exactly this way.

    This issue arises when you start using something like browserify or webpack, those are the tools that need to de-duplicate things. Set it up there and fix your entire build chain. You're just going to have to solve it twice if you push it down into Lerna.

    There is always a tradeoff between power and ease of use.

    That's not true, prioritize low level APIs for extension, don't build high-level APIs. High level APIs are less powerful and less easy to use. Lower level APIs allow for people to build the things that they need. Read the extensible web manifesto if you need inspiration.

    What is the answer for devs who want this feature?

    The answer is find a better solution, build on top of Lerna, if you need a hook request it. Don't push every bit of complexity into one location. You likely already have everything you need:

    $ npm install
    $ lerna bootstrap --ignore react,react-dom
    $ npm install react react-dom

    And then add your resolves to Browserify and Webpack or whatever tool you are using.

    If that doesn't work, think of the minimal lowest level API you need to make it work and request that. It's more likely to be useful to others.

  21. laurentdesmet commented on Sep 12, 2016

    @laurentdesmet
    Contributor

    I investigated lerna bootstrap --ignore but I thought that only applied
    to packages that are part of the Lerna repo.

  22. rygine commented on Sep 12, 2016

    @rygine
    Contributor

    investigated lerna bootstrap --ignore but I thought that only applied to packages that are part of the Lerna repo.

    this is correct.

    This issue arises when you start using something like browserify or webpack, those are the tools that need to de-duplicate things.

    i added a resolver to Webpack with a root of path.join(process.cwd(), 'node_modules') to handle the singleton problem.

  23. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    I investigated lerna bootstrap --ignore but I thought that only applied
    to packages that are part of the Lerna repo.

    Ah, just as easy to add flags to the bootstrap process that supports not installing them. That's a very quick and clean diff that doesn't require any bike-shedding on the exact semantics.

  24. locked and limited conversation to collaborators on Sep 12, 2016
  25. jamiebuilds commented on Sep 12, 2016

    @jamiebuilds
    Contributor

    Locking... if you want to continue this issue in a PR, I'll look at it. If you want to add a flag for skipping install of certain modules I would be happy to add that.

  26. hzoo commented on Sep 13, 2016

    @hzoo
    Contributor

    @thejameskyle I think it might of been better to discuss instead of closing? I think there were some discussions on slack as well and it might of felt weird to just close it and now lock the issue like that (even if you guys disagree). (I don't have an opinion on the particular issue atm)

  27. changed the title [-]Automatic hoisting of common dependencies to repo root[/-] [+][Feature] Automatic hoisting of common dependencies to repo root[/+] on Nov 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions