如何可视化Cabal项目的包依赖图?
ghc-pkg dot) Got it, I totally get the frustration with ghc-pkg dot—it’s great for inspecting the global/user GHC package database, but it doesn’t reflect the per-project dependency tree you’re actually working on with Cabal. Here are the most reliable tools and workflows to generate accurate dependency graphs tailored to your Cabal project:
1. Use cabal-plan (The Go-To Tool)
cabal-plan is purpose-built to parse Cabal’s build plans and generate dependency graphs that match your project’s exact constraints and resolved versions. It’s the gold standard for this use case.
- First, install it (if you haven’t already):
cabal install cabal-plan - Generate a DOT file of your project’s full dependency graph:
cabal-plan dot > project-deps.dot - Convert the DOT file to a visual image (you’ll need Graphviz installed—most package managers offer it as
graphviz):# Generate a PNG for quick viewing dot -Tpng project-deps.dot -o project-deps.png # Or an SVG for sharper, scalable output dot -Tsvg project-deps.dot -o project-deps.svg - Pro Tip: If the full graph is too cluttered, focus only on your project and its direct dependencies (prune transitive ones):
Replacecabal-plan dot --prune=none --focus=your-project-name > focused-deps.dotyour-project-namewith the name of your Cabal package (from your.cabalfile).
2. If You Use Stack: stack dot
If your Cabal project is managed with Stack, it has a built-in command to generate dependency graphs without extra tools:
- Generate the DOT file, focusing only on project-specific dependencies:
stack dot --project-only > project-deps.dot - Convert to a visual with Graphviz using the same commands as above.
3. Bonus: Nix Users Can Use nix-deps-graph
If you’re using Nix to manage your Cabal project’s dependencies, nix-deps-graph can generate a detailed graph that includes Nix-specific dependency details:
nix-build -A haskellPackages.your-project-name.dependencyGraph
This will output a DOT file you can render with Graphviz.
Quick Graphviz Tweaks for Better Readability
- Use different layout engines to reduce clutter:
# Neato for a force-directed, compact layout dot -Tsvg -Kneato project-deps.dot -o project-deps-neato.svg # FDP for another force-directed style that handles dense graphs well dot -Tsvg -Kfdp project-deps.dot -o project-deps-fdp.svg
内容的提问来源于stack exchange,提问作者Cigarette Smoking Man

