golang/go · error
invalid semantic version %q in range %q
Error message
invalid semantic version %q in range %q
What it means
Returned by newQueryMatcher via the badVersion closure when a range-style query (<=, <, >=, >) contains a version operand that fails gover.ModIsValid for the module path. The matcher refuses to interpret an invalid semver as a range bound.
Source
Thrown at src/cmd/go/internal/modload/query.go:430
prefix string
filter func(version string) bool
allowed AllowedFunc
canStat bool // if true, the query can be resolved by repo.Stat
preferLower bool // if true, choose the lowest matching version
mayUseLatest bool
preferIncompatible bool
}
var errRevQuery = errors.New("query refers to a non-semver revision")
// newQueryMatcher returns a new queryMatcher that matches the versions
// specified by the given query on the module with the given path.
//
// If the query can only be resolved by statting a non-SemVer revision,
// newQueryMatcher returns errRevQuery.
func newQueryMatcher(path string, query, current string, allowed AllowedFunc) (*queryMatcher, error) {
badVersion := func(v string) (*queryMatcher, error) {
return nil, fmt.Errorf("invalid semantic version %q in range %q", v, query)
}
matchesMajor := func(v string) bool {
_, pathMajor, ok := module.SplitPathVersion(path)
if !ok {
return false
}
return module.CheckPathMajor(v, pathMajor) == nil
}
qm := &queryMatcher{
path: path,
allowed: allowed,
preferIncompatible: strings.HasSuffix(current, "+incompatible"),
}
switch {
case query == "latest":View on GitHub (pinned to b6b368adc5)
Solutions
- Correct the bound to a canonical semver: vMAJOR.MINOR.PATCH (e.g. '<=v1.5.0', '<v2.0.0').
- Validate the version string with semver.IsValid before constructing the query.
- Use a plain '@<version>' or '@latest' instead of a range if you only need one version.
Example fix
// before $ go get example.com/pkg@<=1.5 // error: invalid semantic version "1.5" in range "<=1.5" // after $ go get example.com/pkg@<=v1.5.0
Defensive patterns
Strategy: validation
Validate before calling
// Validate a range bound before constructing the query:
// if !gover.ModIsValid(path, bound) {
// return fmt.Errorf("invalid version bound %q", bound)
// } Type guard
func isValidRangeBound(path, v string) bool {
return gover.ModIsValid(path, v)
} Prevention
- Always use canonical semver (vMAJOR.MINOR.PATCH) in range queries.
- Sanitize user-supplied version strings through semver.IsValid before use.
- Prefer '@<version>' over ranges unless a range is genuinely needed.
When it happens
Trigger: Issuing a version-range query like 'go get example.com/pkg@<=foo' or '@<v1.x' where the bound is not a valid semantic version for the module path.
Common situations: Typos in a version range passed to 'go get'/'go list -m -versions'; scripting that interpolates an unvalidated string into a range query; mixing branch names into semver range bounds.
Related errors
- invalid previous version %v@%v
- can't query specific version (%q) of standard-library module
- ambiguous semantic version %q in range %q
- revision is longer than canonical (expected %s)
- revision is shorter than canonical (expected %s)
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/834c9e5276687ed7.
Report an issue: GitHub.