golang/go · error

mismatched repo: found %s for %s

Error message

mismatched repo: found %s for %s

What it means

newCodeRepo asserts that the module path starts with the codeRoot (the import path of the repository root). If hasPathPrefix fails, the codeRoot was computed for a different module than the one requested. The %s pair is codeRoot and the module path.

Source

Thrown at src/cmd/go/internal/modfetch/coderepo.go:68

	// pathPrefix is the prefix of modPath that excludes pathMajor.
	// It is used only for logging.
	pathPrefix string

	// pseudoMajor is the major version prefix to require when generating
	// pseudo-versions for this module, derived from the module path. pseudoMajor
	// is empty if the module path does not include a version suffix (that is,
	// accepts either v0 or v1).
	pseudoMajor string
}

// newCodeRepo returns a Repo that reads the source code for the module with the
// given path, from the repo stored in code.
// codeRoot gives the import path corresponding to the root of the repository,
// and subdir gives the subdirectory within the repo containing the module.
// If subdir is empty, the module is at the root of the repo.
func newCodeRepo(code codehost.Repo, codeRoot, subdir, path string) (Repo, error) {
	if !hasPathPrefix(path, codeRoot) {
		return nil, fmt.Errorf("mismatched repo: found %s for %s", codeRoot, path)
	}
	pathPrefix, pathMajor, ok := module.SplitPathVersion(path)
	if !ok {
		return nil, fmt.Errorf("invalid module path %q", path)
	}
	if codeRoot == path {
		pathPrefix = path
	}
	pseudoMajor := module.PathMajorPrefix(pathMajor)

	// Compute codeDir = bar, the subdirectory within the repo
	// corresponding to the module root.
	//
	// At this point we might have:
	//	path = github.com/rsc/foo/bar/v2
	//	codeRoot = github.com/rsc/foo
	//	pathPrefix = github.com/rsc/foo/bar
	//	pathMajor = /v2

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Inspect the go-import meta tag at the import path's host and correct the repository root it advertises.
  2. Check the replace directive in go.mod — it must point at a repo whose root path is a prefix of the module.
  3. Use GOPROXY to bypass vanity resolution and fetch the canonical zip.

Example fix

<!-- before — meta tag root does not contain the requested module -->
<meta name="go-import" content="example.org/a git github.com/a/a">
$ go get example.org/b/m
// error: mismatched repo: found example.org/a for example.org/b/m

<!-- after -->
<meta name="go-import" content="example.org/b git github.com/b/b">
Defensive patterns

Strategy: validation

Validate before calling

func codeRootContainsPath(codeRoot, path string) bool {
    return hasPathPrefix(path, codeRoot)
}

Prevention

When it happens

Trigger: newCodeRepo called with a codeRoot that is not a path-prefix of path — e.g. codeRoot=github.com/a/b but path=github.com/x/y. Indicates a misrouted resolution: the vanity server returned the wrong repo for the import path.

Common situations: A vanity go-import meta tag advertises one repository root but the import path being resolved belongs to a different module; a malformed replace directive; a codehost lookup table returning the wrong repo record.

Related errors


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