{"record":{"id":"ae1abad4125f8648","repo":"ahmetb/kubectx","slug":"context-with-name-q-not-found-errors-in-files","errorCode":null,"errorMessage":"context with name %q not found (errors in files: %w)","messagePattern":"context with name %q not found \\(errors in files: %w\\)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/contexts.go","lineNumber":60,"sourceCode":"func (k *Kubeconfig) contextNodeWithFileIndex(name string) (*yaml.RNode, int, error) {\n\tvar fileErrors []error\n\tfor i := range k.files {\n\t\tcontexts, err := contextsNodeOf(&k.files[i])\n\t\tif err != nil {\n\t\t\tfileErrors = append(fileErrors, fmt.Errorf(\"file %d: %w\", i, err))\n\t\t\tcontinue\n\t\t}\n\t\tcontext, err := contexts.Pipe(yaml.Lookup(\"[name=\" + name + \"]\"))\n\t\tif err != nil {\n\t\t\tfileErrors = append(fileErrors, fmt.Errorf(\"file %d lookup: %w\", i, err))\n\t\t\tcontinue\n\t\t}\n\t\tif context != nil {\n\t\t\treturn context, i, nil\n\t\t}\n\t}\n\tif len(fileErrors) > 0 {\n\t\treturn nil, -1, fmt.Errorf(\"context with name %q not found (errors in files: %w)\",\n\t\t\tname, errors.Join(fileErrors...))\n\t}\n\treturn nil, -1, fmt.Errorf(\"context with name %q not found\", name)\n}\n\nfunc (k *Kubeconfig) contextNode(name string) (*yaml.RNode, error) {\n\tnode, _, err := k.contextNodeWithFileIndex(name)\n\treturn node, err\n}\n\nfunc (k *Kubeconfig) ContextNames() ([]string, error) {\n\tseen := make(map[string]bool)\n\tvar names []string\n\n\tfor i := range k.files {\n\t\tcontexts, err := k.files[i].config.Pipe(yaml.Get(\"contexts\"))\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to get contexts: %w\", err)","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/contexts.go#L42-L78","documentation":"When no file contains the requested context, contextNodeWithFileIndex returns \"context with name %q not found (errors in files: %w)\" if any per-file errors were collected (joined with errors.Join), otherwise the plain not-found error. The '(errors in files: ...)' variant tells you some kubeconfig files could not even be searched, so absence of the context is not conclusive.","triggerScenarios":"DeleteContextEntry, ModifyContextName, or contextNode called with a context name that matches no entry in any k.files, while at least one file also failed parsing or lookup (errors 126/127 collected in fileErrors).","commonSituations":"Deleting or renaming a context whose name was mistyped, with an additional corrupt file in KUBECONFIG masking the real entry; stale context names after cluster teardown.","solutions":["List available contexts (kubectl config get-contexts) and verify the exact spelling/case of the name.","Fix the per-file errors shown in the joined message (see the embedded 'file %d' details) so all files are searched.","Retry the operation once every KUBECONFIG file parses cleanly.","If the context truly no longer exists, remove references to it instead of deleting/renaming."],"exampleFix":"// before: name mismatch plus corrupt extra file\nkc.DeleteContextEntry(\"prod-ctx-1\") // not found\n// after: use the exact name from kubectl config get-contexts\nkc.DeleteContextEntry(\"prod-context\")","handlingStrategy":"validation","validationCode":"// Shell: confirm the context exists in a fully-parsable KUBECONFIG first\nkubectl config get-contexts -o name | grep -qx \"$CTX\" || { echo \"context $CTX not found\"; exit 1; }","typeGuard":null,"tryCatchPattern":"if err := kc.DeleteContextEntry(name); err != nil {\n\tif strings.Contains(err.Error(), \"not found (errors in files\") {\n\t\tfmt.Fprintln(os.Stderr, \"Fix the per-file errors listed before trusting the not-found result.\")\n\t}\n\treturn err\n}","preventionTips":["Copy context names exactly from kubectl config get-contexts (watch case).","Resolve all per-file parse errors in multi-file KUBECONFIG setups before mutating.","Treat '(errors in files: ...)' as a signal the search was incomplete, not definitive.","Audit context names after cluster teardown scripts run."],"tags":["kubeconfig","context","multi-file"],"backgroundTag":"kubeconfig-context-not-found","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}