{"record":{"id":"605c6b3696c97dd8","repo":"ahmetb/kubectx","slug":"cannot-determine-kubeconfig-path-w","errorCode":null,"errorMessage":"cannot determine kubeconfig path: %w","messagePattern":"cannot determine kubeconfig path: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/kubeconfigloader.go","lineNumber":42,"sourceCode":")\n\nvar (\n\tDefaultLoader Loader = new(StandardKubeconfigLoader)\n)\n\ntype StandardKubeconfigLoader struct{}\n\ntype kubeconfigFile struct {\n\t*os.File\n\tpath string\n}\n\nfunc (kf *kubeconfigFile) Path() string { return kf.path }\n\nfunc (*StandardKubeconfigLoader) Load() ([]ReadWriteResetCloser, error) {\n\tpaths, err := kubeconfigPaths()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"cannot determine kubeconfig path: %w\", err)\n\t}\n\n\tvar files []ReadWriteResetCloser\n\tfor _, p := range paths {\n\t\tf, err := os.OpenFile(p, os.O_RDWR, 0)\n\t\tif err != nil {\n\t\t\tif os.IsNotExist(err) {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\treturn nil, fmt.Errorf(\"failed to open file %q: %w\", p, err)\n\t\t}\n\t\tfiles = append(files, &kubeconfigFile{File: f, path: p})\n\t}\n\tif len(files) == 0 {\n\t\treturn nil, fmt.Errorf(\"kubeconfig file not found: %w\",\n\t\t\t&os.PathError{Op: \"open\", Path: paths[0], Err: os.ErrNotExist})\n\t}\n\treturn files, nil","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/kubeconfigloader.go#L24-L60","documentation":"StandardKubeconfigLoader.Load resolves which kubeconfig files to open by calling kubeconfigPaths(), which uses the KUBECONFIG env var or falls back to $HOME/.kube/config. This error wraps a failure of that resolution — currently the only cause is the \"HOME or USERPROFILE environment variable not set\" error returned when no home directory can be determined.","triggerScenarios":"Calling Load (or anything that loads a kubeconfig: Kubeconfig construction, switchNamespace, clearContext, etc.) when the KUBECONFIG env var is unset AND cmdutil.HomeDir() returns an empty string (neither HOME nor USERPROFILE set, and no drop-in home-dir helper applies).","commonSituations":"Running in a container or daemon (systemd service, CI job, cron, Docker ENTRYPOINT) where HOME is not set; running as a user with no passwd entry; misconfigured su/sudo environment stripping HOME.","solutions":["Set the KUBECONFIG environment variable to the kubeconfig path(s): export KUBECONFIG=/path/to/config","Set HOME to the user's home directory: export HOME=/home/user (or USERPROFILE on Windows)","If running as a service/container, set HOME in the unit file / Dockerfile (ENV HOME=/root)","Check that the process runs as the intended user, not one lacking a home"],"exampleFix":"// before\nexec.Command(\"kubectl-ctx\", \"list\") // HOME unset in service\n// after\ncmd := exec.Command(\"kubectl-ctx\", \"list\")\ncmd.Env = append(os.Environ(), \"HOME=/home/user\")","handlingStrategy":"validation","validationCode":"// Go\nif os.Getenv(\"KUBECONFIG\") == \"\" && os.Getenv(\"HOME\") == \"\" && os.Getenv(\"USERPROFILE\") == \"\" {\n    return errors.New(\"set KUBECONFIG or HOME before loading kubeconfig\")\n}","typeGuard":null,"tryCatchPattern":"files, err := loader.Load()\nif err != nil && strings.Contains(err.Error(), \"cannot determine kubeconfig path\") {\n    return fmt.Errorf(\"environment incomplete: %w (set KUBECONFIG or HOME)\", err)\n}","preventionTips":["Always set HOME in containers, CI jobs, and systemd units","Prefer setting KUBECONFIG explicitly in scripts and services","Test commands under `env -i` to catch missing-env failures early","In Docker, add ENV HOME=/root to the image"],"tags":["kubeconfig","environment","home-dir","go"],"backgroundTag":"missing-env-var","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}