{"record":{"id":"7b274ec85ed04343","repo":"derailed/k9s","slug":"unable-to-retrieve-kubeconfig-current-context-q","errorCode":null,"errorMessage":"unable to retrieve kubeconfig current context %q: %w","messagePattern":"unable to retrieve kubeconfig current context %q: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/config/config.go","lineNumber":108,"sourceCode":"\tif flags == nil {\n\t\treturn nil\n\t}\n\n\tif !isStringSet(flags.Timeout) {\n\t\tif d, err := time.ParseDuration(c.K9s.APIServerTimeout); err == nil {\n\t\t\tsetK8sTimeout(flags, d)\n\t\t} else {\n\t\t\tsetK8sTimeout(flags, client.DefaultCallTimeoutDuration)\n\t\t}\n\t}\n\tif isStringSet(flags.Context) {\n\t\tif _, err := c.K9s.ActivateContext(*flags.Context); err != nil {\n\t\t\treturn fmt.Errorf(\"k8sflags. unable to activate context %q: %w\", *flags.Context, err)\n\t\t}\n\t} else {\n\t\tn, err := cfg.CurrentContextName()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to retrieve kubeconfig current context %q: %w\", n, err)\n\t\t}\n\n\t\tif n != \"\" {\n\t\t\t_, err = c.K9s.ActivateContext(n)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"unable to activate context %q: %w\", n, err)\n\t\t\t}\n\t\t} else {\n\t\t\tslog.Debug(\"No context set, skipping context activation\")\n\t\t}\n\t}\n\tif c.K9s.ActiveContextName() != \"\" {\n\t\tslog.Debug(\"Using active context\", slogs.Context, c.K9s.ActiveContextName())\n\t}\n\n\tvar ns string\n\tswitch {\n\tcase k9sFlags != nil && IsBoolSet(k9sFlags.AllNamespaces):","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/derailed/k9s/blob/2d3ccc6ba2ce98c3781bfc441bb3e884f072774f/internal/config/config.go#L90-L126","documentation":"Returned from internal/config/config.go:108 when no --context flag is set and cfg.CurrentContextName() fails while trying to derive the implicit context. The underlying cause is a RawConfig failure (kubeconfig unreadable/unparseable), which surfaces here wrapped. Note a formatting quirk: the %q prints the empty name n, because the failure occurs before any name resolves — the name in the message is always \"\".","triggerScenarios":"Starting k9s without --context with a broken kubeconfig: malformed YAML in a KUBECONFIG file, unreadable file permissions, or an invalid multi-file merge. CurrentContextName's only failure mode in this branch is the rawconfig load.","commonSituations":"Hand-edited kubeconfig with syntax errors; concurrent write by kubelogin/oidc tools corrupting the file; KUBECONFIG referencing a removed path with bad content restored from a partial backup.","solutions":["Reproduce with kubectl config view to get the exact parse error and file","Fix or remove the offending file from $KUBECONFIG (yamllint helps pinpoint the line)","Restore the kubeconfig from a backup or regenerate it with kubectl config set-* commands","Workaround: pass --context explicitly to bypass implicit resolution — but fix the kubeconfig regardless"],"exampleFix":"# before\nk9s  # 'unable to retrieve kubeconfig current context \"\": ...'\n\n# after: find and fix the corrupt file\nkubectl config view --raw 2>&1 | head   # shows parse error + path\nyamllint ~/.kube/config                  # fix reported line\nk9s","handlingStrategy":"validation","validationCode":"func kubeconfigPreflight() error {\n\tout, err := exec.Command(\"kubectl\", \"config\", \"view\", \"-o\", \"jsonpath={.current-context}\").CombinedOutput()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"kubeconfig broken: %v: %s\", err, out)\n\t}\n\treturn nil\n}","typeGuard":null,"tryCatchPattern":"if err := cfg.Init(nil); err != nil {\n\tif strings.Contains(err.Error(), \"unable to retrieve kubeconfig current context\") {\n\t\t// root cause is the wrapped rawconfig error: broken kubeconfig file\n\t\t// run kubectl config view to locate it\n\t}\n}","preventionTips":["Note the message always shows an empty name (\"\") — do not try to interpret it as a real context name","Add kubectl config view to onboarding health checks for machines with hand-built kubeconfigs","Prevent partial writes: use atomic save (write temp + rename) in tools that generate kubeconfigs"],"tags":["kubernetes","kubeconfig","yaml","context","startup"],"backgroundTag":null,"analyzedSha":"2d3ccc6ba2ce98c3781bfc441bb3e884f072774f","analyzedAt":"2026-08-15T16:09:14.432Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}