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 ¶
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 ReadWorkUseDirs ¶
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 ¶
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 ¶
This section is empty.