ahmetb/kubectx · error
context with name %q not found (errors in files: %w)
Error message
context with name %q not found (errors in files: %w)
What it means
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.
Source
Thrown at internal/kubeconfig/contexts.go:60
func (k *Kubeconfig) contextNodeWithFileIndex(name string) (*yaml.RNode, int, error) {
var fileErrors []error
for i := range k.files {
contexts, err := contextsNodeOf(&k.files[i])
if err != nil {
fileErrors = append(fileErrors, fmt.Errorf("file %d: %w", i, err))
continue
}
context, err := contexts.Pipe(yaml.Lookup("[name=" + name + "]"))
if err != nil {
fileErrors = append(fileErrors, fmt.Errorf("file %d lookup: %w", i, err))
continue
}
if context != nil {
return context, i, nil
}
}
if len(fileErrors) > 0 {
return nil, -1, fmt.Errorf("context with name %q not found (errors in files: %w)",
name, errors.Join(fileErrors...))
}
return nil, -1, fmt.Errorf("context with name %q not found", name)
}
func (k *Kubeconfig) contextNode(name string) (*yaml.RNode, error) {
node, _, err := k.contextNodeWithFileIndex(name)
return node, err
}
func (k *Kubeconfig) ContextNames() ([]string, error) {
seen := make(map[string]bool)
var names []string
for i := range k.files {
contexts, err := k.files[i].config.Pipe(yaml.Get("contexts"))
if err != nil {
return nil, fmt.Errorf("failed to get contexts: %w", err)View on GitHub (pinned to 12ad6fb22e)
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.
Example fix
// before: name mismatch plus corrupt extra file
kc.DeleteContextEntry("prod-ctx-1") // not found
// after: use the exact name from kubectl config get-contexts
kc.DeleteContextEntry("prod-context") Defensive patterns
Strategy: validation
Validate before calling
// Shell: confirm the context exists in a fully-parsable KUBECONFIG first
kubectl config get-contexts -o name | grep -qx "$CTX" || { echo "context $CTX not found"; exit 1; } Try / catch
if err := kc.DeleteContextEntry(name); err != nil {
if strings.Contains(err.Error(), "not found (errors in files") {
fmt.Fprintln(os.Stderr, "Fix the per-file errors listed before trusting the not-found result.")
}
return err
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- failed to get current context: %w
- failed to clear namespace: %w
- file %d: %w
- context with name %q not found
- kubeconfig error: %w
AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02).
Data as JSON: /api/errors/ae1abad4125f8648.
Report an issue: GitHub.