golang/go · error

matches packages in but not : specify a different version…

Error message

%v matches packages in %v but not %v: specify a different version for module %s

What it means

This error occurs during module version resolution when a query pattern matches packages in a module at its currently-selected version (curM), but after querying a different version (rev.Version), the pattern no longer matches any packages. This typically happens when a module reorganizes its package structure between versions — packages are moved, renamed, or deleted. The error suggests specifying a different version of the module to resolve the conflict.

Solutions

  1. Check the module's changelog or release notes for package relocations between versions.
  2. Pin to a version that still contains the package: 'go get example.com/m@v1.5.0' (the last version before the reorganization).
  3. Update your import paths to match the new module structure: if the module moved to /v2, update imports to 'example.com/m/v2/pkg/foo'.
  4. Use 'go mod graph' and 'go list -m -versions example.com/m' to see available versions and find one that contains your package.

Example fix

# before
$ go get example.com/m/pkg/foo@latest
# query "example.com/m/pkg/foo@latest" matches packages in
# example.com/m@v1.5.0 but not v2.0.0: specify a different version for module example.com/m

# after: pin to last compatible version
$ go get example.com/m@v1.5.0
# or update import path for v2
$ sed -i 's|example.com/m/|example.com/m/v2/|g' *.go
$ go get example.com/m/v2@latest
Defensive patterns

Strategy: try-catch

Validate before calling

// Before upgrading a dependency, check if packages still exist at the target version
func checkPackagesAtVersion(modPath, version, pkgPattern string) error {
    // Download the version and list packages
    cmd := exec.Command("go", "mod", "download", fmt.Sprintf("%s@%s", modPath, version))
    if err := cmd.Run(); err != nil { return err }
    cmd = exec.Command("go", "list", fmt.Sprintf("-m=%s@%s", modPath, version), pkgPattern)
    out, err := cmd.Output()
    if err != nil || len(strings.TrimSpace(string(out))) == 0 {
        return fmt.Errorf("package %s not found in %s@%s", pkgPattern, modPath, version)
    }
    return nil
}

Try / catch

if strings.Contains(stderr, "matches packages in") && strings.Contains(stderr, "but not") {
    // Package moved or removed between versions
    // Extract module path and try listing available versions
    // Suggest: go list -m -versions <module> to find compatible version
    // Suggest: check module changelog for package relocation
}

Prevention

When it happens

Trigger: During go get with a query that requires upgrading/downgrading a dependency. queryModule succeeds at the new version, but matchInModule at rev.Version finds zero packages matching the query pattern. The code constructs a descriptive error showing: the query (q), the original module (curM), the version that was tried, and the module path, then tells the user to specify a different version.

Common situations: A module moved packages between versions (e.g., v1.0.0 had 'example.com/m/pkg/foo' but v2.0.0 moved it to 'example.com/m/v2/pkg/foo'). A module deleted a package in a new release. A major version bump changes the module path, making the old import path invalid. An upgrade pulls a version where the requested sub-package was refactored into a different path.

Related errors


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

Appendix: source

Thrown at src/cmd/go/internal/modget/get.go:1018

				continue // curM already matches q.
			}

			if !q.matchesPath(curM.Path) {
				m := module.Version{Path: curM.Path, Version: rev.Version}
				packages, err := r.matchInModule(ld, ctx, q.pattern, m)
				if err != nil {
					reportError(q, err)
					continue
				}
				if len(packages) == 0 {
					// curM at its original version contains a path matching q.pattern,
					// but at rev.Version it does not, so (somewhat paradoxically) if
					// we changed the version of curM it would no longer match the query.
					var version any = m
					if rev.Version != q.version {
						version = fmt.Sprintf("%s@%s (%s)", m.Path, q.version, m.Version)
					}
					reportError(q, fmt.Errorf("%v matches packages in %v but not %v: specify a different version for module %s", q, curM, version, m.Path))
					continue
				}
			}

			// Since queryModule succeeded and either curM or one of the packages it
			// contains matches q.pattern, we should have either selected the version
			// of curM matching q, or reported a conflict error (and exited).
			// If we're still here and the version doesn't match,
			// something has gone very wrong.
			reportError(q, fmt.Errorf("internal error: selected %v instead of %v", curM, rev.Version))
		}
	}
}

// performPathQueries populates the candidates for each query whose pattern is
// a path literal.
//
// The candidate packages and modules for path literals depend only on the

View on GitHub (pinned to b6b368adc5)