Adopting it in an existing codebase
On a large codebase the first run usually reports dozens of cycles. Fixing them all before turning on the check is rarely practical. Instead, stop new cycles first, then reduce the old ones over time.
Measure
Section titled “Measure”Get the numbers per package:
jscycles --json > cycles.json || [ $? -eq 1 ]jq -r '.file_cycles[] | select(.cycles | length > 0) | "\(.cycles | length)\t\(.name)"' cycles.json | sort -rn14 @acme/web3 @acme/core1 @acme/billingEach count is the number of separate tangles in the package, not every overlapping path, so it is a fair measure of the work left.
Then decide what counts. If only runtime problems matter to you, measure with
--skip-type-imports (see type-only imports):
cycles made only of import type usually disappear, often a large share.
Choose a starting point
Section titled “Choose a starting point”Pick whichever of these is closest to passing today, and enforce it in CI.
Package cycles only
Section titled “Package cycles only”Package cycles are usually few, and they matter most for builds:
jscycles --outerOnly the clean packages
Section titled “Only the clean packages”Check every package except those that still have cycles, and remove packages from the list as they are fixed:
jscycles --exclude @acme/web --exclude @acme/coreOr the reverse, for a small set you want to protect:
jscycles --only "@acme/shared-*"Everything except known files
Section titled “Everything except known files”Leave specific files out of the analysis, per package:
packages: "@acme/web": ignore: - src/legacy/** - src/store/index.tsAn ignored file is not analyzed at all, so a new cycle through it goes unnoticed. Keep the list short and fix entries over time.
Ratchet the count down
Section titled “Ratchet the count down”To allow the existing cycles but fail on any increase, keep the current count in the repository and compare it in CI:
#!/usr/bin/env bashset -euo pipefailjscycles --json > cycles.json || [ $? -eq 1 ]count=$(jq '([.file_cycles[].cycles[]] | length) + (.package_cycles | length)' cycles.json)baseline=$(cat .cycles-baseline)echo "cycles: $count (baseline $baseline)"if [ "$count" -gt "$baseline" ]; then echo "New circular imports; run jscycles to see them." >&2 exit 1fiif [ "$count" -lt "$baseline" ]; then echo "Fewer cycles than the baseline: update .cycles-baseline to $count." >&2 exit 1fiecho 17 > .cycles-baselineThe second check makes whoever fixes a cycle lower the baseline in the same
change, so it never drifts back up. A count can stay the same while one cycle
is fixed and another added; once the count reaches zero, replace the script
with a plain jscycles.
- Add the check to CI: see use in CI.
- Work through the remaining cycles with fixing cycles.
- Once a package is clean, take it off the
--excludelist orignoreand let the full check protect it.