ahmetb/kubectx · error

failed to get contexts: %w

Error message

failed to get contexts: %w

What it means

Inside ContextNames, the kyaml Pipe(yaml.Get("contexts")) failed for one of the merged kubeconfig files while enumerating context names. The fault is in the top-level contexts field of that file; a missing field is fine (nil is skipped), so this is a structural read error.

Source

Thrown at internal/kubeconfig/contexts.go:78

		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)
		}
		if contexts == nil {
			continue
		}
		fileNames, err := contexts.ElementValues("name")
		if err != nil {
			return nil, fmt.Errorf("failed to get context names: %w", err)
		}
		for _, n := range fileNames {
			if !seen[n] {
				seen[n] = true
				names = append(names, n)
			}
		}
	}
	return names, nil
}

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Check the contexts key in each KUBECONFIG file — it must be a YAML list or absent
  2. Fix or delete the malformed file
  3. Verify with kubectl config get-contexts which file breaks parsing
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at internal/kubeconfig/contexts.go:78 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ahmetb/kubectx@12ad6fb22e (2026-09-02). Data as JSON: /api/errors/4fb8c4eea76bc021. Report an issue: GitHub.