DevelopAnalyses
@holtkeeper/depgraph-go
A GraphSource for Go modules and go.work workspaces. See @holtkeeper/depgraph for the model and analyses.
import { analyzeDependencies } from '@holtkeeper/depgraph'import { goSource } from '@holtkeeper/depgraph-go'
const data = await analyzeDependencies({ dir, sources: [goSource({ runner })] })Public API
Section titled “Public API”goSource({ runner?, depth?, timeoutMs? }): GraphSource— namedgo.runnerdefaults tolocalRunner.goEnv— environment of everygocommand.- Pure helpers:
parseGoModGraph(output, modules),goWorkspaceGraph(modules). - Re-exported from
@holtkeeper/go-source, which owns them:parseGoMod(module,go,requirewithindirect,replace,exclude),parseGoWork,findGoModules(dir, depth),readGoModules(dir, dirs)and theGoMod,GoRequire,GoReplace,GoWorkspaceModuletypes.
Layers
Section titled “Layers”- workspaces — with
go.work, itsusedirectives; otherwise everygo.modfound up todepth(default 4) directories deep, skippingnode_modules,vendor,testdataand hidden directories. Nodes are module paths (kind: 'workspace', ecosystemGo); an edge means one workspace modulerequires another. - packages —
go mod graph. Withgo.workit runs once at the root (GOWORKset to that file); otherwise once per module withGOWORK=off, one layer each (go mod graph (<dir>)when there are several), because separate modules are separate builds. Main and workspace modules are workspace nodes whatever version they are referenced by; other modules arepath@versionpackage nodes. Thegoandtoolchainpseudo-modules are dropped.
Errors
Section titled “Errors”The workspaces layer comes from go.work/go.mod files alone, so it is kept when go mod graph fails (no network, a private module the job cannot fetch, a broken go.mod): load throws PartialLoadError from @holtkeeper/depgraph with that layer, the packages layers of modules that did load, and one message per failed run (<dir>: ... when there are several modules). analyzeDependencies keeps those layers, stores the messages in errors and writes them to the job log.
Duplicates
Section titled “Duplicates”go mod graph lists every version any module in the graph requires. Minimal version selection builds with the highest one, so that node gets selected: true and the others selected: false. findDuplicates reports modules with several versions in the graph; reports show which version is selected and which were only requested, with the path that requests each.
Environment
Section titled “Environment”GOTOOLCHAIN=local (never download a toolchain) and GOFLAGS=-mod=mod (resolve even when go.sum or vendor/ is stale; replaces the caller’s GOFLAGS). GOPROXY, GOPRIVATE and credentials come from the caller’s environment.
Limitations
Section titled “Limitations”go mod graphdownloads module metadata (go.modfiles) when the module cache lacks it, and with-mod=modit may updatego.mod/go.sum. Run it on a disposable checkout.- The selected version is computed from the graph (the highest version present), not from
go list -m all;excludeandretractare not taken into account, andreplacedirectives are only reflected through whatgo mod graphprints. - No source-level (package import) layer for Go yet.