ahmetb/kubectx · error
file %d: %w
Error message
file %d: %w
What it means
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.
Source
Thrown at internal/kubeconfig/contexts.go:47
}
if contexts == nil {
return nil, errors.New("\"contexts\" entry is nil")
} else if contexts.YNode().Kind != yaml.SequenceNode {
return nil, errors.New("\"contexts\" is not a sequence node")
}
return contexts, nil
}
// contextNodeWithFileIndex searches for a context by name across all files.
// Returns the context node and the index of the file that contains it.
// Files without a valid "contexts" sequence are skipped, but if errors occur
// during lookup they are included in the final error message.
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)
}
View on GitHub (pinned to 12ad6fb22e)
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.
Example fix
// KUBECONFIG=~/.kube/config:~/.kube/extra (extra is corrupt) // before KUBECONFIG="$HOME/.kube/config:$HOME/.kube/extra" kubens ns delete old-ctx // after: repair or drop the bad file KUBECONFIG="$HOME/.kube/config" kubens ns delete old-ctx
Defensive patterns
Strategy: validation
Validate before calling
// Shell: validate every file in KUBECONFIG before multi-file operations
IFS=':' read -ra files <<< "${KUBECONFIG:-$HOME/.kube/config}"
for f in "${files[@]}"; do
kubectl config view --kubeconfig "$f" --raw > /dev/null || { echo "invalid kubeconfig: $f"; exit 1; }
done Try / catch
if err := kc.DeleteContextEntry(name); err != nil {
if strings.Contains(err.Error(), "errors in files:") {
// parse embedded "file N: ..." details to repair the corrupt file
fmt.Fprintf(os.Stderr, "some kubeconfig files failed to parse: %v\n", err)
}
return err
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- kubeconfig error: %w
- kubeconfig error: %w
- kubeconfig error: %w
- file %d lookup: %w
- context with name %q not found (errors in files: %w)
AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02).
Data as JSON: /api/errors/a2bb559bd5795426.
Report an issue: GitHub.