{"record":{"id":"7296c8f1ef17b94d","repo":"ahmetb/kubectx","slug":"context-with-name-q-not-found","errorCode":null,"errorMessage":"context with name %q not found","messagePattern":"context with name %q not found","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/kubeconfig/contexts.go","lineNumber":63,"sourceCode":"\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)\n\t\t}\n\t\tif contexts == nil {\n\t\t\tcontinue","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/ahmetb/kubectx/blob/12ad6fb22e8c546ee2b54e7de38aa51c906832f7/internal/kubeconfig/contexts.go#L45-L81","documentation":"The clean no-file-errors variant of the lookup failure: no kubeconfig file errored, but none contained a context with the given name. Raised by contextNodeWithFileIndex when fileErrors is empty, meaning the search was complete and the context name genuinely does not exist.","triggerScenarios":"DeleteContextEntry, ModifyContextName, or contextNode called with a context name absent from all parsed kubeconfig files, with all files parsed successfully.","commonSituations":"Typo in the context name or wrong casing; operating against the wrong KUBECONFIG file than the one holding the context; context removed by a concurrent tool or cluster teardown script.","solutions":["List contexts and copy the exact name: kubectl config get-contexts (or kubens).","Confirm KUBECONFIG points at the file that actually contains the context.","Create the context if it should exist: kubectl config set-context <name> --cluster=... --user=...","Guard the operation in code by checking existence before delete/modify."],"exampleFix":"// before\nif err := kc.DeleteContextEntry(\"stg-context\"); err != nil { return err }\n// after: check existence first\nnode, err := kc.ContextNode(\"stg-context\") // or equivalent existence check\nif err != nil {\n\treturn fmt.Errorf(\"context %q does not exist, nothing to delete\", \"stg-context\")\n}","handlingStrategy":"validation","validationCode":"// Go: check existence before delete/modify to avoid the not-found path\nnode, err := kc.ContextNode(name) // uses the same lookup internally\nif err != nil {\n\treturn fmt.Errorf(\"context %q does not exist: %w\", name, err)\n}","typeGuard":null,"tryCatchPattern":"if err := kc.DeleteContextEntry(name); err != nil {\n\tif strings.Contains(err.Error(), \"not found\") {\n\t\treturn nil // idempotent delete: treat missing context as success\n\t}\n\treturn err\n}","preventionTips":["Verify the exact context name with kubectl config get-contexts before mutating.","Confirm KUBECONFIG points at the file that contains the target context.","Make delete operations idempotent when context may already be gone.","Generate context names from a single source of truth in automation."],"tags":["kubeconfig","context","not-found"],"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"}