golang/go · error

internal error: MatchPackages: %s is not a valid package pat

Error message

internal error: MatchPackages: %s is not a valid package pattern

What it means

Internal error added to a Match via MatchPackages when the match's pattern is a local/filesystem pattern (IsLocal() true). MatchPackages only handles non-local patterns ('all', 'std', 'cmd', or '...' import-path patterns), so a local pattern reaching it means the wrong matcher was dispatched — a programming error in the caller, not a user config issue.

Source

Thrown at src/cmd/go/internal/search/search.go:106

	}
	return fmt.Sprintf("pattern %s: %v", e.Match.Pattern(), e.Err)
}

func (e *MatchError) Unwrap() error {
	return e.Err
}

// MatchPackages sets m.Pkgs to a non-nil slice containing all the packages that
// can be found under the $GOPATH directories and $GOROOT that match the
// pattern. The pattern must be either "all" (all packages), "std" (standard
// packages), "cmd" (standard commands), or a path including "...".
//
// If any errors may have caused the set of packages to be incomplete,
// MatchPackages appends those errors to m.Errs.
func (m *Match) MatchPackages() {
	m.Pkgs = []string{}
	if m.IsLocal() {
		m.AddError(fmt.Errorf("internal error: MatchPackages: %s is not a valid package pattern", m.pattern))
		return
	}

	if m.IsLiteral() {
		m.Pkgs = []string{m.pattern}
		return
	}

	match := func(string) bool { return true }
	treeCanMatch := func(string) bool { return true }
	if !m.IsMeta() {
		match = pkgpattern.MatchPattern(m.pattern)
		treeCanMatch = pkgpattern.TreeCanMatchPattern(m.pattern)
	}

	have := map[string]bool{
		"builtin": true, // ignore pseudo-package that exists only for documentation
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. If you call search.Match directly, route local patterns to MatchDirs, not MatchPackages.
  2. Use search.NewMatch + the correct matcher by checking m.IsLocal() first.
  3. Report as a Go toolchain bug if it surfaces from a normal 'go' command.

Example fix

// before
m := search.NewMatch("./pkg")
m.MatchPackages() // error: internal error: MatchPackages: ./pkg is not a valid package pattern

// after
m := search.NewMatch("./pkg")
m.MatchDirs(modRoots) // correct matcher for local patterns
Defensive patterns

Strategy: type-guard

Validate before calling

// Route by pattern locality before matching:
//   if m.IsLocal() { m.MatchDirs(modRoots) } else { m.MatchPackages() }

Type guard

func matchCorrectly(m *search.Match, modRoots []string) {
    if m.IsLocal() {
        m.MatchDirs(modRoots)
    } else {
        m.MatchPackages()
    }
}

Prevention

When it happens

Trigger: Calling search.Match.MatchPackages() on a Match constructed with a local pattern such as './pkg' or an absolute path — the dispatcher that should have routed to MatchDirs misrouted instead.

Common situations: Seen only in tooling/tests that drive search.Match directly with a local pattern via the wrong method; not produced by the standard CLI invocation paths.

Related errors


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