golang/go · error
invalid newline in =... value
Error message
invalid newline in %s=... value
What it means
Returned by checkEnvWrite when the value written via `go env -w` contains any vertical tab, carriage return, or newline (\v \r \n). The go env config file is one line per entry, so embedded newlines would corrupt the file structure.
Solutions
- Trim trailing newlines from command substitution: assign then strip, or use `${var%$'\n'}`.
- Reject multiline values at your config layer with strings.ContainsAny(v, "\v\r\n").
- Use single-line equivalents (e.g. semicolons instead of newlines in compiler flags).
Example fix
# before $ go env -w GOPROXY=$(printf 'a\nb') error: invalid newline in GOPROXY=... value # after — strip newlines first $ go env -w GOPROXY=$(printf 'a\nb' | tr -d '\r\n')
Defensive patterns
Strategy: validation
Validate before calling
import "strings"
if strings.ContainsAny(val, "\v\r\n") {
return fmt.Errorf("value for %s contains newlines", key)
} Prevention
- Trim trailing newlines from command substitution before persisting.
- Reject multiline values at config-ingestion time.
- Prefer single-line representations in env-driven configs.
When it happens
Trigger: Any `go env -w KEY=...` value containing \v, \r, or \n — typically multiline text pasted from a terminal, or values captured from `$(cmd)` whose output ends in a newline.
Common situations: Pasting multiline strings; command substitution that includes trailing newlines; copy-paste from documentation with line breaks; values containing Windows CRLF line endings.
Related errors
- invalid NUL in =... value
- GOMODCACHE entry is relative; must be absolute path
- GOPATH entry cannot start with shell metacharacter '~
- GOPATH entry is relative; must be absolute path
- invalid
AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12).
Data as JSON: /api/errors/b24b3911a8ae016a.
Report an issue: GitHub.
Appendix: source
Thrown at src/cmd/go/internal/envcmd/env.go:700
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)
}
return nil
}
func readEnvFileLines(mustExist bool) []string {
file, _, err := cfg.EnvFile()
if file == "" {
if mustExist {
base.Fatalf("go: cannot find go env config: %v", err)
}
return nil
}
data, err := os.ReadFile(file)
if err != nil && (!os.IsNotExist(err) || mustExist) {
base.Fatalf("go: reading go env config: %v", err)
}
lines := strings.SplitAfter(string(data), "\n")
if lines[len(lines)-1] == "" {View on GitHub (pinned to b6b368adc5)