golang/go · error

%w %q in: %s

Error message

%w %q in:
	%s

What it means

Sentinel-wrapped 'package not found' error from (*Module).Package. The method binary-searches the module's package directory table via sort.Find; if the requested path is not present, it returns an IndexPackage pre-populated with an error that wraps errCannotFindPackage (the %w). The message format is `<sentinel> <path> in: <abs path>`, so errors.Is(err, errCannotFindPackage) succeeds.

Source

Thrown at src/cmd/go/internal/modindex/read.go:832

	modroot string

	// Source files
	sourceFiles []*sourceFile
}

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

// Package and returns finds the package with the given path (relative to the module root).
// If the package does not exist, Package returns an IndexPackage that will return an
// appropriate error from its methods.
func (m *Module) Package(path string) *IndexPackage {
	defer unprotect(protect(), nil)

	i, ok := sort.Find(m.n, func(i int) int {
		return strings.Compare(path, m.pkgDir(i))
	})
	if !ok {
		return &IndexPackage{error: fmt.Errorf("%w %q in:\n\t%s", errCannotFindPackage, path, filepath.Join(m.modroot, path))}
	}
	return m.pkg(i)
}

// pkg returns the i'th IndexPackage in m.
func (m *Module) pkg(i int) *IndexPackage {
	r := m.d.readAt(m.pkgOff(i))
	p := new(IndexPackage)
	if errstr := r.string(); errstr != "" {
		p.error = errors.New(errstr)
	}
	p.dir = r.string()
	p.sourceFiles = make([]*sourceFile, r.int())
	for i := range p.sourceFiles {
		p.sourceFiles[i] = &sourceFile{
			d:   m.d,
			pos: r.int(),
		}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Verify the path exists on disk under the module root with `ls <modroot>/<path>`.
  2. Check spelling and case of the import path; Go import paths are case-sensitive.
  3. Use (*Module).Walk to enumerate the exact package paths the index knows about.
  4. If the package should exist, ensure it contains at least one non-test .go file so the indexer records it.
Defensive patterns

Strategy: type-guard

Validate before calling

// Check existence before calling Package by enumerating known dirs via Walk.
func packageExists(m *modindex.Module, path string) bool {
    found := false
    m.Walk(func(p string) {
        if p == path { found = true }
    })
    return found
}

Type guard

// Distinguish the sentinel from other IndexPackage errors.
import "errors"

func isPackageNotFound(err error) bool {
    return errors.Is(err, modindex.ErrCannotFindPackage)
}

Prevention

When it happens

Trigger: Calling (*Module).Package(path) where path does not match any directory recorded in the module index — e.g. a typo'd import path, a subpackage that has no .go files, or a path that lives in a different module.

Common situations: Importing a path that doesn't exist under the module root; refactoring that moved a package; case-sensitivity mismatches on case-insensitive filesystems; querying the wrong module index.

Related errors


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