or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

apidiff.mdconstraints.mdebnf.mderrors.mdevent.mdgorelease.mdindex.mdio-i2c.mdio-spi.mdjsonrpc2.mdmaps.mdmmap.mdmodgraphviz.mdrand.mdshiny.mdslices.mdslog.mdstats.mdsumdb.mdtrace.mdtxtar.mdtypeparams.mdutf8string.md
tile.json

modgraphviz.mddocs/

modgraphviz

modgraphviz converts "go mod graph" output into Graphviz's DOT language, enabling visualization and analysis of Go module dependency graphs. It serves as a bridge between Go's module system and the Graphviz visualization toolkit.

Package Information

  • Package Name: modgraphviz
  • Package Type: Go command-line tool
  • Language: Go
  • Installation: go install golang.org/x/exp/cmd/modgraphviz@latest
  • Repository: golang.org/x/exp
  • Location: golang.org/x/exp/cmd/modgraphviz

Overview

modgraphviz is a command-line tool that transforms module dependency data from Go's module system into the DOT language format used by Graphviz. This enables you to create visual representations of your Go project's module dependency graph, making it easier to understand and analyze complex dependency relationships.

The tool automatically distinguishes between selected and non-selected module versions by coloring them differently:

  • Green nodes: The version selected by Go's minimal version selection algorithm (in the final build list)
  • Grey nodes: Other versions not included in the final build (filtered out by minimal version selection)

Basic Usage

The simplest way to use modgraphviz is to pipe the output of go mod graph directly to it:

go mod graph | modgraphviz > graph.dot

The tool reads the graph format generated by go mod graph on standard input and writes DOT language output to standard output. It requires no options or arguments.

Core Functionality

Input/Output Format

modgraphviz operates as a pipeline tool with a single responsibility: convert Go module graph format to Graphviz DOT format.

Input:  "go mod graph" output (module dependency edges)
Output: DOT language format (Graphviz-compatible graph definition)

Capabilities

Convert Module Graph to DOT Format

Transforms Go module dependency information into Graphviz's DOT language format for visualization.

go mod graph | modgraphviz > graph.dot

Description: Reads module dependencies from standard input and outputs DOT format to standard output. No command-line options or arguments are supported.

Visualize Dependencies as PNG

Pipe the DOT output directly to the dot command to generate image files.

go mod graph | modgraphviz | dot -Tpng -o graph.png

Description: Creates a PNG image of the dependency graph. The -Tpng option specifies PNG format, and -o graph.png sets the output filename. Other Graphviz output formats (svg, pdf, ps) can be used by changing the -T option.

Visualize Dependencies as SVG

Generate scalable vector graphics for web-friendly dependency visualization.

go mod graph | modgraphviz | dot -Tsvg -o graph.svg

Description: Creates an SVG image with selectable text and scalable quality. SVG is ideal for web pages and documents requiring zoom capability.

Usage Examples

Example 1: Generate DOT File

Convert your Go module graph to DOT format for inspection or further processing:

cd /path/to/your/go/project
go mod graph | modgraphviz > dependencies.dot
cat dependencies.dot

Output format:

digraph {
  "github.com/user/moduleA" [color=green];
  "github.com/user/moduleB" [color=green];
  "github.com/user/moduleC" [color=grey];
  "github.com/user/moduleA" -> "github.com/user/moduleB";
  "github.com/user/moduleB" -> "github.com/user/moduleC";
}

Example 2: Generate PNG Image

Create a PNG visualization of your module dependencies:

go mod graph | modgraphviz | dot -Tpng -o dependencies.png

The resulting PNG shows:

  • Green nodes: Selected module versions (in build list)
  • Grey nodes: Non-selected versions (filtered by minimal version selection)
  • Arrows: Dependency relationships between modules

Example 3: Interactive Visualization with dotty

Use Graphviz's interactive viewer for real-time exploration:

go mod graph | modgraphviz | dotty

This opens an interactive window where you can navigate the dependency graph, zoom, pan, and inspect individual nodes.

Example 4: Analyze Graph Structure with sccmap

Find strongly connected components (circular dependencies) in your module graph:

go mod graph | modgraphviz | sccmap

This identifies any circular dependency patterns that might need resolution.

Example 5: Multiple Output Formats

Generate multiple visualization formats in sequence:

go mod graph | modgraphviz > deps.dot

# PNG for presentations
dot -Tpng -o deps.png deps.dot

# SVG for web
dot -Tsvg -o deps.svg deps.dot

# PDF for documentation
dot -Tpdf -o deps.pdf deps.dot

Example 6: Large Project Analysis

For large projects, save the intermediate DOT file for repeated analysis:

# Generate DOT once
go mod graph | modgraphviz > project_deps.dot

# Experiment with different layouts
dot -Tpng -Kcirco -o circular_layout.png project_deps.dot
dot -Tpng -Kfdp -o organic_layout.png project_deps.dot
dot -Tpng -Kdot -o hierarchical_layout.png project_deps.dot

Key Concepts

Minimal Version Selection (MVS)

Go uses the Minimal Version Selection algorithm to choose which versions of dependencies to include in builds. modgraphviz visualizes this by:

  • Green nodes: Versions selected by MVS (included in the build)
  • Grey nodes: Versions available but not selected (excluded from the build)

This helps identify which versions are actually used versus which are available alternatives.

Module Dependency Graph Format

The input format is produced by go mod graph and contains module paths and versions:

module@version dependency@version

For example:

github.com/user/myapp@v1.0.0 github.com/user/dep@v1.2.0
github.com/user/dep@v1.2.0 github.com/other/lib@v2.0.0

modgraphviz converts this into DOT format with proper node and edge definitions.

DOT Language

The DOT language is Graphviz's native format for describing graphs. It specifies:

  • Nodes (modules) with properties like color
  • Edges (dependencies) between nodes
  • Graph properties like direction and layout

Example DOT output:

digraph {
  "github.com/example/mod@v1.0.0" [color=green];
  "github.com/example/mod@v1.0.0" -> "github.com/dependency/mod@v2.0.0";
}

Related Tools

modgraphviz is most useful when combined with other tools:

  • Graphviz tools: dot, dotty, sccmap, and others for visualization and analysis
  • golang.org/x/tools/cmd/digraph: General queries and analysis of module graphs (see digraph documentation)
  • go mod graph: The source command that generates input for modgraphviz

Common Workflows

Workflow 1: Dependency Investigation

When investigating how your modules depend on each other:

go mod graph | modgraphviz | dot -Tpng -o deps.png
# Open deps.png in your image viewer

Workflow 2: Circular Dependency Detection

To find and visualize circular dependencies:

go mod graph | modgraphviz | sccmap | dot -Tpng -o circles.png

Workflow 3: Documentation

Include dependency graphs in your project documentation:

go mod graph | modgraphviz > docs/dependencies.dot
dot -Tsvg -o docs/dependencies.svg docs/dependencies.dot

Configuration and Options

modgraphviz has no command-line options or arguments. Configuration is entirely through piping and Graphviz's own options on subsequent tools. All behavior is determined by the input from go mod graph and the Graphviz tools used for visualization.

Requirements and Dependencies

  • Go: Must be installed to use go mod graph command
  • Graphviz: Required for visualization (get from graphviz.org)
  • Standard input: modgraphviz reads from stdin (piped data)
  • Standard output: modgraphviz writes to stdout (can be redirected or piped)

Advanced Topics

Layout Algorithms

Graphviz supports multiple layout algorithms. Try different ones with your DOT file:

dot -Kneato -Tpng -o layout1.png deps.dot    # Spring model
dot -Kfdp -Tpng -o layout2.png deps.dot      # Force-directed placement
dot -Kcirco -Tpng -o layout3.png deps.dot    # Circular layout
dot -Ktwopi -Tpng -o layout4.png deps.dot    # Radial layout

Filtering Large Graphs

For projects with many dependencies, filter the graph before visualization:

# Save DOT file
go mod graph | modgraphviz > all_deps.dot

# Filter nodes manually in the DOT file or use grep
grep "specific-module" all_deps.dot > filtered_deps.dot

Custom Graphviz Styling

Modify the DOT output for custom styling:

go mod graph | modgraphviz | \
  sed 's/\[color=grey\]/[color=lightgrey, style=dashed]/' | \
  dot -Tpng -o styled_deps.png

Notes

  • modgraphviz is optimized for pipeline usage; it's designed to integrate smoothly with standard Unix tools
  • The tool provides no feedback or progress indication; it silently processes input to output
  • For module graphs with hundreds of modules, consider saving the intermediate DOT file for repeated analysis rather than regenerating
  • Version information is included in node labels in the DOT output, helping distinguish between different versions of the same module

References