{"record":{"id":"bc602d6d4940c055","repo":"golang/go","slug":"s-can-only-be-set-using-the-os-environment","errorCode":null,"errorMessage":"%s can only be set using the OS environment","messagePattern":"(.+?) can only be set using the OS environment","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/go/internal/envcmd/env.go","lineNumber":647,"sourceCode":"\treturn \"\"\n}\n\nfunc checkEnvWrite(key, val string) error {\n\tswitch key {\n\tcase \"GOEXE\",\n\t\t\"GOGCCFLAGS\",\n\t\t\"GOHOSTARCH\",\n\t\t\"GOHOSTOS\",\n\t\t\"GOMOD\",\n\t\t\"GOROOT\",\n\t\t\"GOTELEMETRY\",\n\t\t\"GOTELEMETRYDIR\",\n\t\t\"GOTOOLDIR\",\n\t\t\"GOVERSION\",\n\t\t\"GOWORK\":\n\t\treturn fmt.Errorf(\"%s cannot be modified\", key)\n\tcase \"GOENV\", \"GODEBUG\":\n\t\treturn fmt.Errorf(\"%s can only be set using the OS environment\", key)\n\t}\n\n\t// To catch typos and the like, check that we know the variable.\n\t// If it's already in the env file, we assume it's known.\n\tif !cfg.CanGetenv(key) {\n\t\treturn fmt.Errorf(\"unknown go command variable %s\", key)\n\t}\n\n\t// Some variables can only have one of a few valid values. If set to an\n\t// invalid value, the next cmd/go invocation might fail immediately,\n\t// even 'go env -w' itself.\n\tswitch key {\n\tcase \"GO111MODULE\":\n\t\tswitch val {\n\t\tcase \"\", \"auto\", \"on\", \"off\":\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"invalid %s value %q\", key, val)\n\t\t}","sourceCodeStart":629,"sourceCodeEnd":665,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/envcmd/env.go#L629-L665","documentation":"Returned by checkEnvWrite for GOENV and GODEBUG when set via `go env -w`. GOENV names the env file itself (chicken-and-egg) and GODEBUG must take effect before the go command reads its env file, so both are restricted to the OS environment only.","triggerScenarios":"`go env -w GOENV=/path/to/env` or `go env -w GODEBUG=...`.","commonSituations":"Users trying to relocate the go env config file with `go env -w GOENV=...`; users wanting to tweak GODEBUG flags persistently via the env file rather than the OS env.","solutions":["For GOENV: export it in your shell/CI environment (`export GOENV=$HOME/.config/go/env-custom`).","For GODEBUG: export it in the OS environment (`export GODEBUG=...`) or set it per-invocation.","Update dotfiles (e.g. ~/.zshrc, ~/.bashrc) or your CI runner's env injection to set these at the process level."],"exampleFix":"# before\n$ go env -w GODEBUG=panicnil=1\nerror: GODEBUG can only be set using the OS environment\n# after\n$ export GODEBUG=panicnil=1 && go build ./...","handlingStrategy":"validation","validationCode":"var osOnly = map[string]bool{\"GOENV\": true, \"GODEBUG\": true}\nfunc setEnv(key, val string) error {\n    if osOnly[key] {\n        return fmt.Errorf(\"%s must be set in the OS environment\", key)\n    }\n    return runGoEnvW(key, val)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Centralise GODEBUG/GOENV configuration in shell rc files or CI env injection.","Never let config-management scripts route GOENV or GODEBUG through `go env -w`.","Cross-check against `go env -json` — GOENV/GODEBUG are not listed as writable."],"tags":["config","env","go-command","validation"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}