This tool helps identify circular dependencies in the codebase, which can lead to various issues including:
- Difficulty understanding and maintaining the code
- Potential memory leaks
- Initialization order problems
- Slower build times
# Run with Bun
./detect-circular-deps.ts
# Or explicitly with Bun
bun detect-circular-deps.tsThe script analyzes the import statements in all TypeScript and JavaScript files within the packages/ directory. It:
- Builds a dependency graph between files
- Uses depth-first search to detect cycles in the graph
- Reports all discovered circular dependencies
- Provides a summary of circular dependencies grouped by package
When circular dependencies are found, consider these strategies to resolve them:
- Extract Shared Code: Move the shared functionality to a new module that both original modules can import.
- Dependency Inversion: Create an interface that both modules can use.
- Restructure Components: Consider if your component architecture can be reorganized.
- Use Dynamic Imports: In some cases, using dynamic imports can break cycles.
Scanning for circular dependencies...
Scanned 250 files and found 236 with dependencies.
Found 3 circular dependencies:
--------------------------------------------------------------------------------
1. Circular dependency involving packages/dota/src/dota/server.ts:
packages/dota/src/dota/server.ts → packages/dota/src/dota/clearCacheForUser.ts → packages/dota/src/dota/server.ts
--------------------------------------------------------------------------------
2. Circular dependency involving packages/dota/src/db/watcher.ts:
packages/dota/src/db/watcher.ts → packages/dota/src/dota/clearCacheForUser.ts → packages/dota/src/db/watcher.ts
--------------------------------------------------------------------------------
3. Circular dependency involving packages/shared-utils/src/index.ts:
packages/shared-utils/src/index.ts → packages/shared-utils/src/logger.ts → packages/shared-utils/src/index.ts
--------------------------------------------------------------------------------
Circular dependencies by package:
- dota: 2 circular dependencies
- shared-utils: 1 circular dependencies
Analysis completed in 0.534 seconds.