pkgresolver

package
v1.37.0 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package pkgresolver maps Go package import paths to on-disk directories.

It is the shared workhorse for the framework's source-scanning generators (cmd/gentopology, cmd/gencreds) which need to walk the *api package of every service in a bundle. A naive implementation shells out to `go list -json -e <pkg>` once per call - ~150-300ms each - and a typical bundle has dozens of services × several *api imports, so the cost adds up to tens of seconds per run.

Two optimizations cut that cost:

  • In-module shortcut: for any path inside the resolver's own module (computed from the nearest go.mod walking up from the start dir), the directory is derived by string math against the module root, no subprocess needed. Verified with os.Stat so a misnamed import or a replace directive still falls through to go list.
  • Cross-service memo: lookups are cached on the Resolver, so the second through Nth service importing the same *api package is free.

DisableShortcut on Resolver forces every lookup through go list, even in-module ones. Reserved for tests that need to verify the fallback path or assert shortcut/fallback parity; do not flip it in production code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GoListDir

func GoListDir(pkgPath, fromDir string) (string, error)

GoListDir runs `go list -json -e <pkgPath>` from fromDir and returns the resolved package directory. With the -e flag set, `go list` itself succeeds for missing packages (the returned Dir is empty), so a non-nil error here always represents a real toolchain failure (broken module, missing `go`, malformed go.mod) rather than a missing dependency.

Types

type Resolver

type Resolver struct {

	// DisableShortcut forces every Dir call through `go list`, bypassing the
	// in-module string-math fast path. Test-only knob for verifying the
	// fallback or asserting shortcut/fallback parity.
	DisableShortcut bool
	// contains filtered or unexported fields
}

Resolver maps Go package paths to on-disk directories. Construct with New. Safe for sequential use; concurrent calls require external synchronization.

func New

func New(startDir string) *Resolver

New constructs a Resolver anchored at the nearest go.mod walking up from startDir. When no go.mod is found, the resolver still works - the in-module shortcut is disabled and every Dir call falls through to `go list`.

func (*Resolver) Dir

func (r *Resolver) Dir(pkgPath, fromDir string) (string, error)

Dir returns the on-disk directory of pkgPath. Tries the in-module shortcut first (skipped when DisableShortcut is set), falls back to `go list` for paths outside the module. Results are memoized. A nil receiver disables both shortcut and cache - every call shells out to `go list` - so the receiver stays usable in code paths that may not own a resolver.

func (*Resolver) ModulePath

func (r *Resolver) ModulePath() string

ModulePath returns the module path declared in the resolved go.mod, or "" if no go.mod was found.

func (*Resolver) ModuleRoot

func (r *Resolver) ModuleRoot() string

ModuleRoot returns the on-disk directory of the resolved go.mod, or "" if no go.mod was found.

Jump to

Keyboard shortcuts

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