gomodutil

package
v0.0.0-...-043add5 Latest Latest
Warning

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

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

Documentation

Overview

Package gomodutil is the single shared go.mod module-path reader used by codegen, scaffold, the gocell CLI, and archtest. It replaces three divergent hand-rolled scanners (cellgen, cmd/gocell, archtest) with one robust parser.

It returns plain errors (fmt.Errorf) so low-level build-phase callers stay free of the pkg/errcode dependency; callers that need a typed error wrap at their own boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadModulePath

func ReadModulePath(root string) (string, error)

ReadModulePath reads root/go.mod and returns its declared module path (e.g. "github.com/ghbvf/gocell"). It uses golang.org/x/mod/modfile.ModulePath, the canonical extractor — robust against leading comments and surrounding whitespace, replacing three divergent hand-rolled line-prefix scanners.

Returns an error when go.mod is absent/unreadable or has no module directive (fail-closed: callers must not proceed with an empty module path).

func ReadReplaceExclude

func ReadReplaceExclude(root string) ([]ReplaceDirective, []ExcludeDirective, error)

ReadReplaceExclude reads root/go.mod and returns its replace and exclude directives. A module carrying either is NOT cleanly consumable as an external dependency: a downstream `go get`/`go build` ignores the directives (so a replace pointing at a local path references code the consumer cannot resolve), and `go install pkg@version` of any package in the module is rejected outright by the toolchain when the module's go.mod contains replace or exclude directives.

It uses golang.org/x/mod/modfile (the canonical parser), so it is robust against comments and block/single-line forms. Returns an error when go.mod is absent/unreadable or malformed (fail-closed: callers must not treat a parse failure as "no directives").

func ReadWorkUseDirs

func ReadWorkUseDirs(root string) ([]string, error)

ReadWorkUseDirs reads root/go.work and returns the disk paths of its `use` directives, each filepath.Clean'd and kept relative as written (e.g. "." or "mdm" or "examples/ssobff"). It uses golang.org/x/mod/modfile.ParseWork -- the canonical go.work parser the Go toolchain itself uses -- so it is robust against comments, block (`use (...)`) and single-line forms.

go.work `use` is the authoritative set of Go modules the toolchain compiles in workspace mode; archtest's workspace enumeration derives its production scan set from it (so a module extracted into go.work is auto-covered).

Returns an error when go.work is absent/unreadable or malformed (fail-closed: callers must not proceed with a guessed module set). Three path-traversal guards reject a use directive whose Module.Dir would escape the workspace and flow into LoadProductionPackages (`go list ./<dir>/...` scanning packages outside the repo): absolute paths, ".." segments, and SYMLINKS that resolve outside the root (e.g. `use ./linked` where ./linked -> /outside — which string-cleaning alone cannot catch). The symlink guard is existence-agnostic: a use dir that does not exist on disk is left as-is (a not-yet-created member cannot be a symlink escape), preserving this parser's existence-agnostic contract.

func ValidateModulePath

func ValidateModulePath(s string) error

ValidateModulePath checks that s is a valid Go module path suitable for use as the prefix of generated import paths. It delegates to the canonical validator the Go toolchain itself uses (golang.org/x/mod/module) rather than a hand-rolled regex — the regex silently admitted malformed paths (trailing slash, double slash) that would corrupt the import statements emitted into generated files.

It uses CheckImportPath, not CheckPath: the value is joined with the module-relative package suffix to form a Go *import path*, and CheckImportPath accepts single-segment local/test module paths like "foo" (which CheckPath rejects with "missing dot in first path element"). CheckImportPath rejects empty strings, "." / ".." path elements, backslashes, whitespace, control characters, leading/trailing slashes, and double slashes.

Types

type ExcludeDirective

type ExcludeDirective struct {
	Path    string
	Version string
}

ExcludeDirective is one `exclude` directive from a go.mod ("path@version").

type ReplaceDirective

type ReplaceDirective struct {
	Old string // replaced module, "path" or "path@version"
	New string // replacement: a filesystem path, or "path@version"
}

ReplaceDirective is one `replace` directive from a go.mod, rendered for human-readable diagnostics (e.g. "github.com/ghbvf/gocell => ../../").

Jump to

Keyboard shortcuts

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