depgraph

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package depgraph provides the golang.org/x/tools/go/packages integration for building a GoCell dependency graph from live module source.

The core data model (Graph, Node, Stats, layer constants, Classifier, TransitiveImports) is defined in kernel/depgraph. Callers that only need graph data and layer classification should import kernel/depgraph directly to avoid the heavy golang.org/x/tools dependency.

DOT rendering lives here (WriteDOT) rather than in kernel/depgraph, because the Graphviz presentation concern belongs in the tools layer, not in kernel.

This package provides the packages.Load entry points:

  • Load(opts LoadOptions, patterns ...string) (*kernel/depgraph.Graph, error) — self-contained, for CLI or one-shot consumers. Auto-detects module path from go.mod and runs packages.Load with the structural modes only (NeedName | NeedImports | NeedModule). Per-package errors surfaced by packages.Load become a fail-fast error so callers never receive a silently partial graph.
  • FromPackages(modules []string, pkgs) (*kernel/depgraph.Graph) — injection point for callers that already loaded packages (notably archtest, which shares typeseval.SharedResolver across structural and type-level rules). The contract is structural-only: FromPackages reads PkgPath, Imports, and ID from each package and ignores typed fields.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromPackages

func FromPackages(modules []string, pkgs []*packages.Package) *kerneldepgraph.Graph

FromPackages builds a Graph from already-loaded packages classified against the given module set. modules is the set of module import paths the graph spans (one element for a single-module load; every workspace member for a multi-module load). It is the injection point for callers that share a packages.Load with another consumer; archtest uses this to reuse typeseval.SharedResolver's cached load instead of running packages.Load twice per test run — passing the workspace module set from tools/workspace.Modules (its own load omits NeedModule, so it cannot detect the set from packages).

Only structural fields (PkgPath, Imports, ID for test-variant filtering) are read; pkgs is not retained after the call returns.

func Load

func Load(opts LoadOptions, patterns ...string) (*kerneldepgraph.Graph, error)

Load builds a Graph by running packages.Load against patterns in single-module mode (GOWORK=off): it loads one standalone module (the testdata/synth fixture, or any caller-supplied Dir not in the repo go.work `use` set). The module set is auto-detected from the loaded packages' Module fields. For a workspace-wide graph spanning every go.work member, use LoadWorkspace.

func LoadWorkspace

func LoadWorkspace(opts LoadOptions, patterns ...string) (*kerneldepgraph.Graph, error)

LoadWorkspace builds a Graph spanning every module matched by patterns under the ambient go.work workspace (GOWORK on). Callers pass one `<importPath>/...` pattern per workspace member (see tools/workspace.Modules); the module set is auto-detected from the loaded packages' Module fields, so nested modules are classified within their own module (LayerCells, not LayerUnknown) rather than as unknown directories of the core module.

func WriteDOT

func WriteDOT(g *kerneldepgraph.Graph, w io.Writer) error

WriteDOT renders g as Graphviz DOT into w. Nodes are grouped into clusters by Layer; edges are simple directed arrows.

The output is deterministic: nodes within a cluster and clusters themselves are sorted by name. Re-rendering the same Graph produces byte-identical DOT.

Types

type LoadOptions

type LoadOptions struct {
	// IncludeTests, when true, sets packages.Config.Tests so test variants
	// of each package are loaded. Production-only closure analysis still
	// excludes test-variant edges, but TestOnly markings on Node become
	// meaningful (a node is TestOnly if no production package imports it).
	IncludeTests bool

	// BuildTags is joined as `-tags=a,b,c` and passed to packages.Config.
	// Empty means no extra tags.
	BuildTags []string

	// Dir is the directory to run packages.Load from. Empty means the
	// current working directory.
	Dir string
}

LoadOptions configures Load.

Jump to

Keyboard shortcuts

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