golang/go · error

invalid %s: %v

Error message

invalid %s: %v

What it means

Returned by checkEnvWrite for CC/CXX when the value (treated as a Go-quoted command string) fails quoted.Split. CC/CXX are stored as a quoted command plus arguments (e.g. "gcc -m64"), so they must obey Go shell-quoting rules.

Source

Thrown at src/cmd/go/internal/envcmd/env.go:683

		}
	case "GOPATH":
		if strings.HasPrefix(val, "~") {
			return fmt.Errorf("GOPATH entry cannot start with shell metacharacter '~': %q", val)
		}
		if !filepath.IsAbs(val) && val != "" {
			return fmt.Errorf("GOPATH entry is relative; must be absolute path: %q", val)
		}
	case "GOMODCACHE":
		if !filepath.IsAbs(val) && val != "" {
			return fmt.Errorf("GOMODCACHE entry is relative; must be absolute path: %q", val)
		}
	case "CC", "CXX":
		if val == "" {
			break
		}
		args, err := quoted.Split(val)
		if err != nil {
			return fmt.Errorf("invalid %s: %v", key, err)
		}
		if len(args) == 0 {
			return fmt.Errorf("%s entry cannot contain only space", key)
		}
		if !filepath.IsAbs(args[0]) && args[0] != filepath.Base(args[0]) {
			return fmt.Errorf("%s entry is relative; must be absolute path: %q", key, args[0])
		}
	}

	if !utf8.ValidString(val) {
		return fmt.Errorf("invalid UTF-8 in %s=... value", key)
	}
	if strings.Contains(val, "\x00") {
		return fmt.Errorf("invalid NUL in %s=... value", key)
	}
	if strings.ContainsAny(val, "\v\r\n") {
		return fmt.Errorf("invalid newline in %s=... value", key)
	}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Wrap the whole command in double quotes if it contains spaces: `go env -w CC="gcc -m64"`.
  2. Avoid mixing single and double quotes; use only Go-style double-quoted strings.
  3. Test-parse the value with strconv.Quoted/quoted.Split before invoking `go env -w`.
  4. Simplify to just the compiler name (no args) if you do not need flags.

Example fix

# before
$ go env -w CC='clang -v unbalanced"'
error: invalid CC: ...
# after
$ go env -w CC="clang -v"
Defensive patterns

Strategy: validation

Validate before calling

import "golang.org/x/tools/go/quoted"

func validateCC(v string) error {
    if v == "" { return nil }
    if _, err := quoted.Split(v); err != nil {
        return fmt.Errorf("CC/CXX is not valid Go-quoted syntax: %w", err)
    }
    return nil
}

Prevention

When it happens

Trigger: `go env -w CC='gcc "unterminated'`, `go env -w CXX='clang % bad'`, or any CC/CXX value with unbalanced quotes or invalid Go-quoted syntax.

Common situations: Users pasting shell-style values with single quotes that are not valid in Go's quoted grammar; missing closing quotes; stray backslashes that break the tokenizer.

Related errors


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