{"record":{"id":"399cc3c0c4e20454","repo":"ahmetb/kubectx","slug":"kubeconfig-file-not-found-w","errorCode":null,"errorMessage":"kubeconfig file not found: %w","messagePattern":"kubeconfig file not found: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/kubeconfigloader.go","lineNumber":57,"sourceCode":"func (*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\n}\n\nfunc (kf *kubeconfigFile) Reset() error {\n\tif err := kf.Truncate(0); err != nil {\n\t\treturn fmt.Errorf(\"failed to truncate file: %w\", err)\n\t}\n\tif _, err := kf.Seek(0, 0); err != nil {\n\t\treturn fmt.Errorf(\"failed to seek in file: %w\", err)\n\t}\n\treturn nil\n}\n\nfunc kubeconfigPaths() ([]string, error) {\n\t// KUBECONFIG env var\n\tif v := os.Getenv(\"KUBECONFIG\"); v != \"\" {","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/kubeconfigloader.go#L39-L75","documentation":"After Load skips all non-existent candidate paths, it checks whether any file was actually opened. If none were, it fails with this error carrying an os.ErrNotExist PathError constructed for the first candidate path. It means the loader found no kubeconfig at any of the configured locations.","triggerScenarios":"Calling Load when KUBECONFIG points only to non-existent paths, or KUBECONFIG is unset and $HOME/.kube/config does not exist (no cluster ever created, kubeadm/colima/minikube never initialized, fresh machine or CI container).","commonSituations":"Fresh developer machine or CI runner without a cluster config; KUBECONFIG set to a path that was deleted or never created; typo'd filename in KUBECONFIG (e.g. config.yml vs config.yaml); running under a different HOME than the one with the config.","solutions":["Create the kubeconfig: run a cluster bootstrap (minikube start / kind create cluster / kubeadm init) or copy a valid config to ~/.kube/config","Verify the path exists: ls -l $(echo $KUBECONFIG) and correct the KUBECONFIG value if wrong","If HOME changed (sudo, service user), point KUBECONFIG at the real config file explicitly","Check whether the file was deleted by a cleanup job and restore from backup"],"exampleFix":"// before\nexport KUBECONFIG=~/.kube/configs/prod.yaml  # file does not exist\n// after\nexport KUBECONFIG=~/.kube/config  # or create the referenced file","handlingStrategy":"validation","validationCode":"// Go\np := os.Getenv(\"KUBECONFIG\")\nif p == \"\" { p = filepath.Join(os.Getenv(\"HOME\"), \".kube\", \"config\") }\nif _, err := os.Stat(p); os.IsNotExist(err) {\n    return fmt.Errorf(\"no kubeconfig at %s: create one with minikube/kind/kubectl config\", p)\n}","typeGuard":null,"tryCatchPattern":"if err := run(); err != nil {\n    if errors.Is(err, os.ErrNotExist) {\n        return fmt.Errorf(\"kubeconfig missing — run 'minikube start' or set KUBECONFIG: %w\", err)\n    }\n    return err\n}","preventionTips":["Bootstrap a cluster (minikube start / kind create cluster) before tools that need a kubeconfig","Verify KUBECONFIG paths exist in shell startup scripts: test -f \"$KUBECONFIG\"","On CI, provision the kubeconfig as a secret mounted at a known path","Watch for HOME differences under sudo — sudo -E preserves it"],"tags":["kubeconfig","file-not-found","environment","go"],"backgroundTag":"file-not-found","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}