lerna icon indicating copy to clipboard operation
lerna copied to clipboard

fix(bootstrap): reject-cycles when using workspaces

Open tkloht opened this issue 3 years ago • 1 comments

Description

when the --reject-cycles flag is set and the repo is using workspaces, run collapseCycles() to detect cycles before installing root package dependencies.

Motivation and Context

Currently the --reject-cycles flag does not have any effect when using workspaces. Without workspaces it is evaluated during runLifecycleInPackages(), but as far as I can tell this code-path is never reached with workspaces. This would also fix the open issue 2776

How Has This Been Tested?

added a test-case including fixture for yarn workspace with cyclic dependency for local testing I have published to verdaccio with e2e:local-publish. output before:

❯ npx lerna bootstrap --reject-cycles
lerna notice cli v5.1.1
lerna info bootstrap root only

up to date, audited 604 packages in 754ms

52 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

output after my change:

❯ npx lerna bootstrap --reject-cycles
lerna notice cli v999.9.20
lerna ERR! ECYCLE Dependency cycles detected, you should fix these!
lerna ERR! ECYCLE one -> two -> one

with this Lerna config:

❯ cat lerna.json
{
  "packages": [
    "packages/*"
  ],
  "version": "0.0.0",
  "useWorkspaces": true
}

Types of changes

  • [x] Bug fix (non-breaking change which fixes an issue)
  • [ ] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • [x] My code follows the code style of this project.
  • [ ] My change requires a change to the documentation.
  • [ ] I have updated the documentation accordingly.
  • [x] I have read the CONTRIBUTING document.
  • [x] I have added tests to cover my changes.
  • [ ] All new and existing tests passed. (I had issues with some snapshot tests locally that seem unrelated to my changes)

tkloht avatar Jun 11 '22 11:06 tkloht

Deploy Preview for lerna-docs failed.

Name Link
Latest commit 4f9dabcd5bc97ee33eb27b0b6874fd3eefe345df
Latest deploy log https://app.netlify.com/sites/lerna-docs/deploys/62a477e37e6fea00091b9e4b

netlify[bot] avatar Jun 11 '22 11:06 netlify[bot]

Oh wow, this feature would be quite helpful! I want to detect cycles for our workflow.

cds-amal avatar Oct 07 '22 12:10 cds-amal

@cds-amal I ended up writing a small CLI tool to do what the reject-cycles flag does, since there seems to be no-one from nrwl willing to review PRs and I don't think this will ever be fixed in Lerna. You can use it like npx cyclic-dependencies --reject, should be the exact same behaviour as lerna bootstrap --reject-cycles (without the bootstrapping part). Code is over here https://github.com/tkloht/cyclic-dependencies

tkloht avatar Oct 08 '22 08:10 tkloht

The thing is that lerna bootstrap should be deprecated in the future, so I'm not sure they want to invest time or feature on that command, see comment from Lerna maintainer here

ghiscoding avatar Oct 08 '22 15:10 ghiscoding

I think I agree with evocateur that it should be deprecated, and I think checking for cyclic dependencies doesn't need to be part of yarn/npm/pnpm either. In my opinion a small, dedicated program like the one I wrote is better for that.

All that said, currently bootstrap still exists and has a flag to check for cyclic dependencies. I would expect this to work, but it does not. This PR fixes the reject-cycles flag and adds test coverage, it's frustrating that it's not getting reviewed.

tkloht avatar Oct 08 '22 17:10 tkloht

Thanks for your patience on this folks!

JamesHenry avatar Oct 09 '22 20:10 JamesHenry

And thank you to @tkloht for submitting the fix 🙏

JamesHenry avatar Oct 09 '22 20:10 JamesHenry