golang/go · error · module.InvalidVersionError

syntax error

Error message

syntax error

What it means

versionToRev converts a requested semantic version into a VCS revision. If semver.IsValid(version) is false, it returns a module.InvalidVersionError wrapping "syntax error", signaling the version string is not well-formed semantic versioning. Callers see this as an invalid version for the module.

Source

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

		}
		if semver.Build(rev) == "+incompatible" {
			rev = rev[:len(rev)-len("+incompatible")]
		}
		if r.codeDir == "" {
			return rev
		}
		return r.codeDir + "/" + rev
	}
	return rev
}

func (r *codeRepo) versionToRev(version string) (rev string, err error) {
	if !semver.IsValid(version) {
		return "", &module.ModuleError{
			Path: r.modPath,
			Err: &module.InvalidVersionError{
				Version: version,
				Err:     errors.New("syntax error"),
			},
		}
	}
	return r.revToRev(version), nil
}

// findDir locates the directory within the repo containing the module.
//
// If r.pathMajor is non-empty, this can be either r.codeDir or — if a go.mod
// file exists — r.codeDir/r.pathMajor[1:].
func (r *codeRepo) findDir(ctx context.Context, version string) (rev, dir string, gomod []byte, err error) {
	rev, err = r.versionToRev(version)
	if err != nil {
		return "", "", nil, err
	}

	// Load info about go.mod but delay consideration
	// (except I/O error) until we rule out v2/go.mod.

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Use a fully valid semver version such as `v1.2.3` (include the leading v and three numeric components).
  2. For a non-tagged commit, use a canonical pseudo-version or a raw commit hash instead of a malformed semver.
  3. Double-check the v prefix and component count against the module's published tags.

Example fix

// before
//   go get example.com/foo@1.2      // syntax error
// after
//   go get example.com/foo@v1.2.0
Defensive patterns

Strategy: validation

Validate before calling

// Validate a version before passing it to the go command:
//   import "golang.org/x/mod/semver"
//   if !semver.IsValid(version) { /* reject or canonicalize */ }

Prevention

When it happens

Trigger: Passing a non-semver version string such as "1.2", "v1", "latest-stable", or a tag with disallowed characters to versionToRev (e.g. via `go get module@<bad-version>`).

Common situations: Typos in `go get foo@version`; tags used without the required `v` prefix; mixing VCS tag names with module version syntax; forgetting the patch component (v1.2 instead of v1.2.0).

Related errors


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