{"record":{"id":"1efd97c9e6aa2a88","repo":"golang/go","slug":"goenv-off","errorCode":null,"errorMessage":"GOENV=off","messagePattern":"GOENV=off","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/cmd/go/internal/cfg/cfg.go","lineNumber":352,"sourceCode":"var OrigEnv []string\n\n// CmdEnv is the new environment for running go tool commands.\n// User binaries (during go test or go run) are run with OrigEnv,\n// not CmdEnv.\nvar CmdEnv []EnvVar\n\nvar envCache struct {\n\tonce   sync.Once\n\tm      map[string]string\n\tgoroot map[string]string\n}\n\n// EnvFile returns the name of the Go environment configuration file,\n// and reports whether the effective value differs from the default.\nfunc EnvFile() (string, bool, error) {\n\tif file := os.Getenv(\"GOENV\"); file != \"\" {\n\t\tif file == \"off\" {\n\t\t\treturn \"\", false, fmt.Errorf(\"GOENV=off\")\n\t\t}\n\t\treturn file, true, nil\n\t}\n\tdir, err := os.UserConfigDir()\n\tif err != nil {\n\t\treturn \"\", false, err\n\t}\n\tif dir == \"\" {\n\t\treturn \"\", false, fmt.Errorf(\"missing user-config dir\")\n\t}\n\treturn filepath.Join(dir, \"go/env\"), false, nil\n}\n\nfunc initEnvCache() {\n\tenvCache.m = make(map[string]string)\n\tenvCache.goroot = make(map[string]string)\n\tif file, _, _ := EnvFile(); file != \"\" {\n\t\treadEnvFile(file, \"user\")","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/go/internal/cfg/cfg.go#L334-L370","documentation":"Returned by cfg.EnvFile() when GOENV is explicitly set to \"off\". This is a legitimate, intentional setting that disables loading/writing the persistent go-env file (~/.../go/env); EnvFile surfaces it as an error so callers can distinguish \"disabled\" from \"default path\". It is informational rather than a fault.","triggerScenarios":"GOENV=off is exported and a code path calls cfg.EnvFile() expecting a usable file path. The error signals that no env file should be read or written.","commonSituations":"Reproducible/locked-down CI that pins all config via real env vars; sandboxed builds that forbid writes outside the workdir; users following hardening guides.","solutions":["Treat the returned error as expected when GOENV=off is intended; do not propagate it as fatal.","If env-file behavior is needed, unset GOENV (or set it to a path) instead of off.","Callers should check for this specific error string/value before failing."],"exampleFix":"// before: any EnvFile error is fatal\nf, changed, err := cfg.EnvFile()\nif err != nil { return err }\n\n// after: tolerate the documented GOENV=off case\nf, changed, err := cfg.EnvFile()\nif err != nil && err.Error() != \"GOENV=off\" { return err }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"func isGoenvOff(err error) bool {\n    return err != nil && err.Error() == \"GOENV=off\"\n}","tryCatchPattern":"f, changed, err := cfg.EnvFile()\nif err != nil {\n    if err.Error() == \"GOENV=off\" {\n        // env file intentionally disabled; proceed without it\n        return\n    }\n    return err\n}","preventionTips":["Document GOENV=off as an expected, non-fatal condition in callers.","Centralize EnvFile() handling in one helper so the off-case is handled once.","Test the GOENV=off path explicitly."],"tags":["configuration","environment","goenv"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}