golang/go · error

can't request explicit version %q of path %q in main module

Error message

can't request explicit version %q of path %q in main module

What it means

This error occurs when a user specifies a version for a local path pattern (relative paths like './pkg', '../foo', or absolute paths like '/home/user/project'). The query validator detects patternIsLocal (the pattern is a relative or absolute filesystem path) and checks if rawVersion is non-empty. Local paths are part of the main module and cannot have independent versions — they use whatever the main module specifies.

Source

Thrown at src/cmd/go/internal/modget/query.go:183

		pattern:        pattern,
		patternIsLocal: filepath.IsAbs(pattern) || search.IsRelativePath(pattern),
		version:        version,
	}
	if strings.Contains(q.pattern, "...") {
		q.matchWildcard = pkgpattern.MatchPattern(q.pattern)
		q.canMatchWildcardInModule = pkgpattern.TreeCanMatchPattern(q.pattern)
	}
	if err := q.validate(ld); err != nil {
		return q, err
	}
	return q, nil
}

// validate reports a non-nil error if q is not sensible and well-formed.
func (q *query) validate(ld *modload.Loader) error {
	if q.patternIsLocal {
		if q.rawVersion != "" {
			return fmt.Errorf("can't request explicit version %q of path %q in main module", q.rawVersion, q.pattern)
		}
		return nil
	}

	if q.pattern == "all" {
		// If there is no main module, "all" is not meaningful.
		if !ld.HasModRoot() {
			return fmt.Errorf(`cannot match "all": %v`, modload.NewNoMainModulesError(ld))
		}
		if !versionOkForMainModule(q.version) {
			// TODO(bcmills): "all@none" seems like a totally reasonable way to
			// request that we remove all module requirements, leaving only the main
			// module and standard library. Perhaps we should implement that someday.
			return &modload.QueryUpgradesAllError{
				MainModules: ld.MainModules.Versions(),
				Query:       q.version,
			}
		}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Remove the version suffix from local paths: 'go get ./mypkg' instead of 'go get ./mypkg@v1.0.0'.
  2. If you need a specific version of a dependency, use the full module import path (not a relative path): 'go get example.com/mymodule/mypkg@v1.0.0'.
  3. Understand that local paths are resolved relative to the main module and always use the main module's version.

Example fix

# before
$ go get ./internal/handler@v1.0.0
# can't request explicit version "v1.0.0" of path "./internal/handler" in main module

# after: local paths don't take versions
$ go get ./internal/handler
# or if you meant an external module, use the full path
$ go get example.com/mymodule/internal/handler@v1.0.0
Defensive patterns

Strategy: validation

Validate before calling

// Validate that local path arguments don't have version suffixes
func validateLocalPathNoVersion(arg string) error {
    parts := strings.SplitN(arg, "@", 2)
    if len(parts) < 2 { return nil } // no version, fine
    path := parts[0]
    // Check if path is local (relative or absolute)
    isLocal := filepath.IsAbs(path) ||
        strings.HasPrefix(path, "./") ||
        strings.HasPrefix(path, "../") ||
        path == "." || path == ".."
    if isLocal {
        return fmt.Errorf("local path %q cannot have version @%s", path, parts[1])
    }
    return nil
}

Try / catch

if strings.Contains(stderr, "can't request explicit version") && strings.Contains(stderr, "in main module") {
    // Local path with version suffix
    // Suggest: remove @version from local paths
}

Prevention

When it happens

Trigger: Running 'go get ./mypkg@v1.0.0' or 'go get ../sibling@latest'. The pattern is detected as local (starts with ./, ../, or is absolute), and a version suffix was provided via @version.

Common situations: A developer treats a local subpackage as if it were an external module and adds a version. Confusion about the difference between local module paths and external module paths. A script template that always appends @version without checking if the path is local. Migrating from GOPATH mode where local paths might have been versioned differently.

Related errors


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