ahmetb/kubectx · error

context with name not found

Error message

context with name %q not found

What it means

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.

Solutions

  1. List contexts and copy the exact name: kubectl config get-contexts (or kubens).
  2. Confirm KUBECONFIG points at the file that actually contains the context.
  3. Create the context if it should exist: kubectl config set-context <name> --cluster=... --user=...
  4. Guard the operation in code by checking existence before delete/modify.

Example fix

// before
if err := kc.DeleteContextEntry("stg-context"); err != nil { return err }
// after: check existence first
node, err := kc.ContextNode("stg-context") // or equivalent existence check
if err != nil {
	return fmt.Errorf("context %q does not exist, nothing to delete", "stg-context")
}
Defensive patterns

Strategy: validation

Validate before calling

// Go: check existence before delete/modify to avoid the not-found path
node, err := kc.ContextNode(name) // uses the same lookup internally
if err != nil {
	return fmt.Errorf("context %q does not exist: %w", name, err)
}

Try / catch

if err := kc.DeleteContextEntry(name); err != nil {
	if strings.Contains(err.Error(), "not found") {
		return nil // idempotent delete: treat missing context as success
	}
	return err
}

Prevention

When it happens

Trigger: DeleteContextEntry, ModifyContextName, or contextNode called with a context name absent from all parsed kubeconfig files, with all files parsed successfully.

Common situations: 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.

Related errors


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

Appendix: source

Thrown at internal/kubeconfig/contexts.go:63

		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)
		}
		if contexts == nil {
			continue

View on GitHub (pinned to 12ad6fb22e)