{"record":{"id":"a2bb559bd5795426","repo":"ahmetb/kubectx","slug":"file-d-w","errorCode":null,"errorMessage":"file %d: %w","messagePattern":"file (.+?): %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/contexts.go","lineNumber":47,"sourceCode":"\t}\n\tif contexts == nil {\n\t\treturn nil, errors.New(\"\\\"contexts\\\" entry is nil\")\n\t} else if contexts.YNode().Kind != yaml.SequenceNode {\n\t\treturn nil, errors.New(\"\\\"contexts\\\" is not a sequence node\")\n\t}\n\treturn contexts, nil\n}\n\n// contextNodeWithFileIndex searches for a context by name across all files.\n// Returns the context node and the index of the file that contains it.\n// Files without a valid \"contexts\" sequence are skipped, but if errors occur\n// during lookup they are included in the final error message.\nfunc (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","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/contexts.go#L29-L65","documentation":"contextNodeWithFileIndex wraps an error from contextsNodeOf(&k.files[i]) as \"file %d: %w\" when parsing the contexts node of multi-file kubeconfig number i fails. Each failing file's error is collected in fileErrors and later surfaced in the aggregate not-found error, so one corrupt file doesn't hide which file is broken.","triggerScenarios":"Any lookup path (DeleteContextEntry, ModifyContextName, contextNode -> contextNodeWithFileIndex) iterating k.files where file i fails to yield its contexts node: malformed YAML, missing 'contexts' top-level key handled as an error, or schema violation in that specific file.","commonSituations":"KUBECONFIG lists multiple files and one is hand-edited with broken YAML or lacks a contexts section; a partial merge produced an invalid file; older tool wrote an incompatible format.","solutions":["Identify the failing file from the index in the final error message and run kubectl config view --kubeconfig <file> on it.","Fix the YAML/syntax or add a valid 'contexts' section to that file.","Remove the broken file from the KUBECONFIG list if it is no longer needed.","Restore the file from backup or regenerate it via your cloud provider CLI."],"exampleFix":"// KUBECONFIG=~/.kube/config:~/.kube/extra (extra is corrupt)\n// before\nKUBECONFIG=\"$HOME/.kube/config:$HOME/.kube/extra\" kubens ns delete old-ctx\n// after: repair or drop the bad file\nKUBECONFIG=\"$HOME/.kube/config\" kubens ns delete old-ctx","handlingStrategy":"validation","validationCode":"// Shell: validate every file in KUBECONFIG before multi-file operations\nIFS=':' read -ra files <<< \"${KUBECONFIG:-$HOME/.kube/config}\"\nfor f in \"${files[@]}\"; do\n\tkubectl config view --kubeconfig \"$f\" --raw > /dev/null || { echo \"invalid kubeconfig: $f\"; exit 1; }\ndone","typeGuard":null,"tryCatchPattern":"if err := kc.DeleteContextEntry(name); err != nil {\n\tif strings.Contains(err.Error(), \"errors in files:\") {\n\t\t// parse embedded \"file N: ...\" details to repair the corrupt file\n\t\tfmt.Fprintf(os.Stderr, \"some kubeconfig files failed to parse: %v\\n\", err)\n\t}\n\treturn err\n}","preventionTips":["Validate each KUBECONFIG-listed file with kubectl config view after edits.","Never hand-edit with tabs; use a YAML-aware editor.","Restore broken files from backup instead of shipping multi-file setups with a known-bad member.","Test merges with `kubectl config view --flatten` before relying on them."],"tags":["kubeconfig","yaml","multi-file"],"backgroundTag":"kubeconfig-file-parse-error","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}