depgraph

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package depgraph builds a directed import graph from a slice of golist.Package values and exposes reverse-transitive closure traversal.

All operations are pure — no io, no syscalls. Tests are table-driven over synthetic graphs.

API stability: pre-v1.0, package surface may change. See repo README.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph is a directed import graph constructed from a slice of golist.Package values. Edges run importer → imported, mirroring `go list -deps`'s view. The Graph also stores reverse edges internally so RevDepClosure runs in O(V + E) without rebuilding.

A Graph is immutable after Build returns. Methods are safe for concurrent reads from multiple goroutines.

API stability: pre-v1.0, the Graph type is opaque (no exported fields, no exported methods beyond those documented). Internal representation may change without notice.

func Build

func Build(pkgs []golist.Package) *Graph

Build constructs a Graph from the given slice of packages.

Edge semantics: for each Package P, the union of P.Imports + P.TestImports + P.XTestImports forms P's outgoing-edge set. This merging reflects affected-set intent — if a package's tests import X and X changes, the package needs re-testing.

Build never fails. Empty input yields an empty Graph (zero nodes, zero edges). Duplicate ImportPath entries are treated as a single node, with the last entry's edge set winning.

Build does not validate that every imported path appears as a node in pkgs. Edges to absent nodes are recorded in the forward map but don't add nodes — Has on an absent path returns false.

func (*Graph) DirectImporters

func (g *Graph) DirectImporters(path string) []string

DirectImporters returns the import paths that directly import path, sorted lexicographically. Returns nil if path is not in the graph.

The returned slice is a copy; callers may freely store or mutate it without affecting the Graph's internal state.

func (*Graph) DirectImports

func (g *Graph) DirectImports(path string) []string

DirectImports returns the import paths directly imported by path — the union of Imports + TestImports + XTestImports of the underlying Package, sorted lexicographically. Returns nil if path is not in the graph.

The returned slice is a copy; callers may freely store or mutate it without affecting the Graph's internal state.

func (*Graph) Has

func (g *Graph) Has(path string) bool

Has reports whether path is a node in the graph. A path is a node iff it appeared as a Package.ImportPath in Build's input; paths that appear only as edge targets (e.g. stdlib imports the caller did not include in pkgs) are not nodes.

func (*Graph) RevDepClosure

func (g *Graph) RevDepClosure(seeds []string) []string

RevDepClosure returns the seeds plus every package that transitively imports any of them — the reverse-transitive closure under the imports relation. The output is sorted lexicographically for deterministic CI behaviour.

Seeds not present in the graph are silently skipped. Cycle-safe via a visited set. Complexity: O(V + E) over the reachable subgraph, plus O(k log k) for the final sort on result size k.

func (*Graph) Stats

func (g *Graph) Stats() Stats

Stats returns a summary of the graph's shape. The returned value is a copy; callers may freely store or modify it.

type Stats

type Stats struct {
	// Nodes is the count of distinct ImportPath values that appeared
	// as Package.ImportPath in Build's input.
	Nodes int

	// Edges is the count of forward edges (importer → imported) across
	// the graph. Reverse edges are not double-counted. An edge from a
	// known node to an absent node (e.g. a stdlib import the caller did
	// not include in pkgs) contributes one to this count.
	Edges int

	// MaxInDegree is the largest reverse-edge count across known nodes
	// — the most-depended-on node's importer count. Zero on an empty
	// graph or a graph with no edges between known nodes.
	MaxInDegree int

	// MaxOutDegree is the largest forward-edge count across known nodes
	// — the most-importing node's import count. Zero on an empty graph.
	MaxOutDegree int
}

Stats summarises the shape of a Graph. Returned by Graph.Stats.

Fields are computed once at Build time and cached on the Graph; Stats() is a cheap accessor that returns a copy callers may freely store or modify.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL