gomodutil

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: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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 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

This section is empty.

Jump to

Keyboard shortcuts

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