fix(bootstrap): reject-cycles when using workspaces
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)
Deploy Preview for lerna-docs failed.
| Name | Link |
|---|---|
| Latest commit | 4f9dabcd5bc97ee33eb27b0b6874fd3eefe345df |
| Latest deploy log | https://app.netlify.com/sites/lerna-docs/deploys/62a477e37e6fea00091b9e4b |
Oh wow, this feature would be quite helpful! I want to detect cycles for our workflow.
@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
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
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.
Thanks for your patience on this folks!
And thank you to @tkloht for submitting the fix 🙏