golang/go · error

%s: %v

Error message

%s: %v

What it means

Wrapped error from getConstraints when parsing build-constraint comments (//go:build, // +build, or //go:binary-only-package) out of a file's leading comment header during module-index construction. The %s is the bare file name (no dir) and %v is the underlying constraint-parsing error such as 'multiple //go:build comments' or a malformed constraint expression.

Source

Thrown at src/cmd/go/internal/modindex/build.go:280

	// when we create the index file?
	var ignoreBinaryOnly bool
	if strings.HasSuffix(name, ".go") {
		err = readGoInfo(f, info)
		if strings.HasSuffix(name, "_test.go") {
			ignoreBinaryOnly = true // ignore //go:binary-only-package comments in _test.go files
		}
	} else {
		info.header, err = readComments(f)
	}
	f.Close()
	if err != nil {
		return nil, fmt.Errorf("read %s: %v", info.name, err)
	}

	// Look for +build comments to accept or reject the file.
	info.goBuildConstraint, info.plusBuildConstraints, info.binaryOnly, err = getConstraints(info.header)
	if err != nil {
		return nil, fmt.Errorf("%s: %v", name, err)
	}

	if ignoreBinaryOnly && info.binaryOnly {
		info.binaryOnly = false // override info.binaryOnly
	}

	return info, nil
}

func cleanDecls(m map[string][]token.Position) ([]string, map[string][]token.Position) {
	all := make([]string, 0, len(m))
	for path := range m {
		all = append(all, path)
	}
	sort.Strings(all)
	return all, m
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Open the named file and locate the //go:build / // +build comment block in its header (must be before the package clause, separated by a blank line).
  2. Ensure there is exactly one //go:build line; remove any duplicates (errMultipleGoBuild).
  3. Validate the expression with `go vet ./...` after fixing; vet reports build-constraint syntax errors.
  4. Move the constraint comments above the `package` declaration with a blank line separating them from code.

Example fix

// before (two constraints)
//go:build linux
//go:build amd64
package foo
// after
//go:build linux && amd64
package foo
Defensive patterns

Strategy: validation

Validate before calling

// Use go/build/constraint to lint every //go:build comment before build.
import "go/build/constraint"

func lintBuildConstraints(header []byte) error {
    for _, line := range bytes.Split(header, []byte("\n")) {
        if bytes.HasPrefix(bytes.TrimSpace(line), []byte("//go:build")) {
            if _, err := constraint.Parse(string(line)); err != nil {
                return err
            }
        }
    }
    return nil
}

Prevention

When it happens

Trigger: A source file in the module has two or more //go:build lines, an unparseable //go:build expression, or another error surfaced by getConstraints. getFileInfo wraps it as `<name>: <err>`.

Common situations: Editing a file and accidentally leaving a duplicate //go:build line after a merge; using old-style '// +build' syntax that violates the parsing rules; copy-pasting a constraint that contains a typo such as `//go:build go1.21.`.

Related errors


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