{"record":{"id":"d739062662724d89","repo":"ahmetb/kubectx","slug":"file-d-lookup-w","errorCode":null,"errorMessage":"file %d lookup: %w","messagePattern":"file (.+?) lookup: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/contexts.go","lineNumber":52,"sourceCode":"\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\nfunc (k *Kubeconfig) contextNode(name string) (*yaml.RNode, error) {\n\tnode, _, err := k.contextNodeWithFileIndex(name)\n\treturn node, err\n}\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/contexts.go#L34-L70","documentation":"contextNodeWithFileIndex wraps an error from contexts.Pipe(yaml.Lookup(\"[name=\" + name + \"]\")) as \"file %d lookup: %w\". The YAML field lookup for the context name fails structurally in file i — not 'not found', but an error evaluating the lookup expression over the contexts node.","triggerScenarios":"During contextNodeWithFileIndex iteration over k.files, Pipe(yaml.Lookup(...)) returns an error for file i, usually when the contexts node items are not the expected mapping shape (e.g. a context entry is not a map, or the list contains scalars), making the name-field lookup invalid.","commonSituations":"A contexts list containing malformed entries (missing name field, wrong types) after manual editing or a bad merge; a file where 'contexts' is a map instead of a list.","solutions":["Open the kubeconfig file at the reported index and inspect each entry under 'contexts' for a proper 'name' field.","Fix or remove malformed context entries so each is a mapping with name/cluster/user.","Validate with kubectl config view --kubeconfig <file> until it parses cleanly.","Re-merge multi-file KUBECONFIG inputs with a tool that produces valid entries."],"exampleFix":"// before (invalid entry breaks lookup)\ncontexts:\n  - just-a-string\n// after\ncontexts:\n  - name: my-ctx\n    context:\n      cluster: my-cluster\n      user: my-user","handlingStrategy":"validation","validationCode":"// Go: verify context entries are well-formed maps before lookup operations\ntype contextEntry struct {\n\tName    string `yaml:\"name\"`\n\tContext struct {\n\t\tCluster string `yaml:\"cluster\"`\n\t\tUser    string `yaml:\"user\"`\n\t} `yaml:\"context\"`\n}\n// each item in 'contexts' must unmarshal into this shape before Pipe(Lookup) can succeed","typeGuard":null,"tryCatchPattern":"if err := kc.ModifyContextName(old, new); err != nil {\n\tif strings.Contains(err.Error(), \"lookup\") {\n\t\tfmt.Fprintln(os.Stderr, \"A contexts entry is malformed; ensure every context is a mapping with a 'name' field.\")\n\t}\n\treturn err\n}","preventionTips":["Ensure every entry under 'contexts' is a mapping with a 'name' key.","Run kubectl config view --kubeconfig <file> to catch structural breakage early.","Avoid raw string merges of kubeconfig files; use --flatten through kubectl.","Schema-check generated kubeconfigs in CI."],"tags":["kubeconfig","yaml","lookup"],"backgroundTag":"yaml-lookup-failed","analyzedSha":"12ad6fb22e8c546ee2b54e7de38aa51c906832f7","analyzedAt":"2026-09-02T12:23:10.107Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T16:17:10.729Z"}