ahmetb/kubectx · error

failed to get current context: %w

Error message

failed to get current context: %w

What it means

GetCurrentContext errored while determining which context's namespaces to list. The kyaml read of current-context failed structurally (e.g. non-scalar field); an unset current-context is handled separately below.

Source

Thrown at cmd/kubens/list.go:53

type ListOp struct{}

func (op ListOp) Run(stdout, stderr io.Writer) error {
	kc := new(kubeconfig.Kubeconfig).WithLoader(kubeconfig.DefaultLoader)
	defer kc.Close()
	if err := kc.Parse(); err != nil {
		return fmt.Errorf("kubeconfig error: %w", err)
	}
	return formatNamespaceList(kc, stdout)
}

// formatNamespaceList writes the sorted, current-namespace-highlighted list
// of namespaces available in the current context to w (one per line). It
// mirrors what ListOp.Run prints so the interactive fzf picker shows the same
// list the user would see from `kubens | fzf`.
func formatNamespaceList(kc *kubeconfig.Kubeconfig, w io.Writer) error {
	ctx, err := kc.GetCurrentContext()
	if err != nil {
		return fmt.Errorf("failed to get current context: %w", err)
	}
	if ctx == "" {
		return errors.New("current-context is not set")
	}
	curNs, err := kc.NamespaceOfContext(ctx)
	if err != nil {
		return fmt.Errorf("cannot read current namespace: %w", err)
	}

	ns, err := queryNamespaces(kc)
	if err != nil {
		return fmt.Errorf("could not list namespaces (is the cluster accessible?): %w", err)
	}

	for _, c := range ns {
		s := c
		if c == curNs {
			s = printer.ActiveItemColor.Sprint(c)

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Inspect current-context in each kubeconfig file and fix malformed values
  2. Use kubectl config current-context to find the offending file
  3. Repair the YAML and rerun kubens
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at cmd/kubens/list.go:53 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/a80e717535740c55. Report an issue: GitHub.