Generating Dependency Graphs
Why Dependency Graphs?¶
Understanding how packages relate to each other is the first step in
onboarding, refactoring, and architecture review. ctx dep generates
dependency graphs from source code so you can see the structure at a
glance instead of tracing imports by hand.
Quick Start¶
# Auto-detect ecosystem and output Mermaid (default)
ctx dep
# Table format for a quick terminal overview
ctx dep --format table
# JSON for programmatic consumption
ctx dep --format json
Ecosystem Detection¶
ctx dep looks for manifest files in this order:
- Go:
go.mod - Node.js:
package.json - Python:
pyproject.toml,setup.py,requirements.txt - Rust:
Cargo.toml
First match wins. To override detection, use --type:
Output Formats¶
Mermaid (default)¶
Produces a Mermaid graph definition you can paste into GitHub PRs, Obsidian notes, or any Mermaid-compatible renderer.
graph TD
internal/cli --> internal/config
internal/cli --> internal/memory
internal/config --> internal/entry
Table¶
Flat two-column view for quick terminal scanning.
PACKAGE DEPENDS ON
internal/cli internal/config, internal/memory
internal/config internal/entry
internal/memory internal/index
JSON¶
Machine-readable output for scripts and pipelines.
Including External Dependencies¶
By default, only internal (first-party) dependencies are shown. Add
--external to include third-party packages:
This is useful when auditing transitive dependencies or checking which packages pull in heavy external libraries.
When to Use It¶
- Onboarding. Generate a Mermaid graph and drop it into the project wiki. New contributors see the architecture before reading code.
- Refactoring. Before moving packages, check what depends on them.
Combine with
ctx driftto find stale references after the move. - Architecture review. Table format gives a quick overview; Mermaid format goes into design docs and PRs.
- Pre-commit. Run in CI to detect unexpected new dependencies between packages.
Combining with Other Commands¶
Refactoring with ctx drift¶
# See the dependency structure before refactoring
ctx dep --format table
# After moving packages, check for broken references
ctx drift
Feeding architecture maps¶
Use JSON output as input for context files or architecture documentation:
# Generate a dependency snapshot for the context directory
ctx dep --format json > .context/deps.json
# Or pipe into other tools
ctx dep --format mermaid >> docs/architecture.md
Monorepos and Multi-Ecosystem Projects¶
In a monorepo with multiple ecosystems, ctx dep picks the first
manifest it finds (Go beats Node.js beats Python beats Rust). Use
--type to target a specific ecosystem:
For separate subdirectories, run from each root:
Tips¶
- Start with table format. It is the fastest way to get a mental model of the dependency structure. Switch to Mermaid when you need a visual for documentation or a PR.
- Pipe JSON to jq. Filter for specific packages, count edges, or extract subgraphs programmatically.
- Skip
--externalunless you need it. Internal-only graphs are cleaner and load faster. Add external deps when you are specifically auditing third-party usage. - Force
--typein CI. Auto-detection is convenient locally, but explicit types prevent surprises when the repo structure changes.