golang/go · error

cannot find package

Error message

cannot find package

What it means

errMissing is the loader's generic sentinel raised when no package can be found for a requested import path: not in the standard library, not in the module graph, and not resolvable from source. It is the bottom-of-the-stack answer for an unresolvable import.

Source

Thrown at src/cmd/go/internal/modload/load.go:1066

func (af *atomicLoadPkgFlags) has(cond loadPkgFlags) bool {
	return loadPkgFlags(af.bits.Load())&cond == cond
}

// isTest reports whether pkg is a test of another package.
func (pkg *loadPkg) isTest() bool {
	return pkg.testOf != nil
}

// fromExternalModule reports whether pkg was loaded from a module other than
// the main module.
func (pkg *loadPkg) fromExternalModule(ld *Loader) bool {
	if pkg.mod.Path == "" {
		return false // loaded from the standard library, not a module
	}
	return !ld.MainModules.Contains(pkg.mod.Path)
}

var errMissing = errors.New("cannot find package")

// loadFromRoots attempts to load the build graph needed to process a set of
// root packages and their dependencies.
//
// The set of root packages is returned by the params.listRoots function, and
// expanded to the full set of packages by tracing imports (and possibly tests)
// as needed.
func loadFromRoots(ld *Loader, ctx context.Context, params loaderParams) *packageLoader {
	pld := &packageLoader{
		loaderParams: params,
		work:         par.NewQueue(runtime.GOMAXPROCS(0)),
	}

	if pld.requirements.pruning == unpruned {
		// If the module graph does not support pruning, we assume that we will need
		// the full module graph in order to load package dependencies.
		//
		// This might not be strictly necessary, but it matches the historical

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Add the package: `go get <importpath>`.
  2. Check the import path spelling and casing.
  3. For private repos set GOPRIVATE (and GOPROXY/NOPROXY) appropriately.
  4. Verify any replace/exclude directives aren't hiding the package.

Example fix

// before
import "example.com/nothing"   // cannot find package

// after
$ go get example.com/nothing
Defensive patterns

Strategy: validation

Validate before calling

// Sanity-check that an import path resolves before building.
func importResolves(path string) error {
    return exec.Command("go", "list", path).Run()
}

Type guard

// Narrow a sentinel 'cannot find package' error.
import "errors"

func isMissingPackage(err error) bool {
    return errors.Is(err, errMissing) // exported equivalent in your wrapper
}

Try / catch

if err := importer.Default().Import(path); err != nil {
    if isMissingPackage(err) { /* go get it, then retry */ }
}

Prevention

When it happens

Trigger: Loading/building/importing a path that does not exist, cannot be downloaded, is excluded, or is not in any active module's dependency graph.

Common situations: Import-path typos; missing `go get`; private modules without GOPRIVATE/GONOSUMDB/GOPROXY config; network/proxy outages; replace directives pointing at deleted paths.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/ab99424de3472157. Report an issue: GitHub.