derailed/k9s · error

set current context failed. %w

Error message

set current context failed. %w

What it means

Returned by Config.ActivateContext (internal/config/config.go:157), the top-level facade that delegates to K9s.ActivateContext. Any failure in the underlying activation — context lookup miss, per-context config load error, proxy connectivity failure — is re-wrapped as 'set current context failed. %w'. Expect it stacked: this message wraps error chains rooted in internal/config/k9s.go and internal/client/config.go.

Source

Thrown at internal/config/config.go:157

	if ns == "" {
		ns = client.DefaultNamespace
	}
	if err := c.SetActiveNamespace(ns); err != nil {
		return err
	}

	return data.EnsureDirPath(c.K9s.AppScreenDumpDir(), data.DefaultDirMod)
}

// Reset resets the context to the new current context/cluster.
func (c *Config) Reset() {
	c.K9s.Reset()
}

func (c *Config) ActivateContext(n string) (*data.Context, error) {
	ct, err := c.K9s.ActivateContext(n)
	if err != nil {
		return nil, fmt.Errorf("set current context failed. %w", err)
	}

	return ct, nil
}

// CurrentContext fetch the configuration active context.
func (c *Config) CurrentContext() (*data.Context, error) {
	return c.K9s.ActiveContext()
}

// ActiveNamespace returns the active namespace in the current context.
// If none found return the empty ns.
func (c *Config) ActiveNamespace() string {
	ns, err := c.K9s.ActiveContextNamespace()
	if err != nil {
		slog.Error("Unable to assert active namespace. Using default", slogs.Error, err)
		ns = client.DefaultNamespace
	}

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. Unwrap the chain (fmt.Printf("%+v", err) or errors.Unwrap loop) and fix the deepest cause — this wrapper itself carries no extra information
  2. Confirm the context exists in the loaded kubeconfig (kubectl config get-contexts)
  3. Validate or delete the per-context yaml under the k9s config dir
  4. If the cause is proxy connectivity, fix the address or remove the proxy block

Example fix

// before
ct, err := appCfg.ActivateContext("prod")
if err != nil {
	panic(err) // opaque 'set current context failed. ...'
}

// after: surface the root cause
ct, err := appCfg.ActivateContext("prod")
if err != nil {
	var root error = err
	for errors.Unwrap(root) != nil {
		root = errors.Unwrap(root)
	}
	log.Printf("context switch failed, root cause: %v", root)
	return err
}
Defensive patterns

Strategy: try-catch

Try / catch

ct, err := appCfg.ActivateContext(name)
if err != nil {
	// strip wrappers: 'set current context failed.' adds no information
	log.Printf("activation failed: root=%v", errors.Unwrap(errors.Unwrap(err)))
	// fall back to a known-good default context if the app must stay up
	ct, err = appCfg.ActivateContext(fallbackContext)
	if err != nil {
		return err
	}
}

Prevention

When it happens

Trigger: Any programmatic call to Config.ActivateContext(name) where name is absent from the kubeconfig, the context's k9s yaml is invalid, or the context's proxy is unreachable. In the k9s UI this path runs on :ctx switching.

Common situations: Programmatic embedders of k9s config switching contexts at runtime; UI context switch to a context whose config was never initialized or was corrupted; switching after the kubeconfig changed on disk.

Related errors


AI-assisted analysis of derailed/k9s@2d3ccc6ba2 (2026-08-15). Data as JSON: /api/errors/71cad7dcedef877d. Report an issue: GitHub.