ahmetb/kubectx · error

kubectl config view failed: %w

Error message

kubectl config view failed: %w

What it means

The subprocess `kubectl config view --minify --flatten --context <name>` exited non-zero when building the minimal per-context kubeconfig for the isolated shell. The wrapped error is an *exec.ExitError; causes include invalid/expired credentials in that context, unreachable API server during token refresh, or a context whose cluster/user references are broken.

Source

Thrown at cmd/kubectx/shell.go:66

func resolveKubectl() (string, error) {
	if v := os.Getenv("KUBECTL"); v != "" {
		return v, nil
	}
	path, err := exec.LookPath("kubectl")
	if err != nil {
		return "", fmt.Errorf("kubectl is required for --shell but was not found in PATH")
	}
	return path, nil
}

func extractMinimalKubeconfig(kubectlPath, contextName string) ([]byte, error) {
	cmd := exec.Command(kubectlPath, "config", "view", "--minify", "--flatten",
		"--context", contextName)
	cmd.Env = os.Environ()
	data, err := cmd.Output()
	if err != nil {
		return nil, fmt.Errorf("kubectl config view failed: %w", err)
	}
	return data, nil
}

func detectShell() string {
	if runtime.GOOS == "windows" {
		// cmd.exe always sets the PROMPT env var, so if it is present
		// we can reliably assume we are running inside cmd.exe.
		if os.Getenv("PROMPT") != "" {
			return "cmd.exe"
		}
		// Otherwise assume PowerShell. PSModulePath is always set on
		// Windows regardless of the shell, so it cannot be used as a
		// discriminator; however the absence of PROMPT is a strong
		// enough signal that we are in a PowerShell session.
		if pwsh, err := exec.LookPath("pwsh"); err == nil {
			return pwsh
		}

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Run kubectl config view --minify --flatten --context <name> manually to see the underlying failure
  2. Refresh credentials for the context (re-login, aws eks update-kubeconfig, etc.)
  3. Verify the context's cluster/server and user entries are well-formed in the kubeconfig
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at cmd/kubectx/shell.go:66 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/89c4d77a9ea65fb1. Report an issue: GitHub.