ahmetb/kubectx · error

failed to decode file %d: %w

Error message

failed to decode file %d: %w

What it means

YAML decoding of the i-th loaded kubeconfig file failed in Parse: the file content is not valid YAML (or is empty beyond a document). All opened handles are closed to avoid leaks before returning; %d identifies the failing file index in KUBECONFIG order.

Source

Thrown at internal/kubeconfig/kubeconfig.go:85

	}
	return firstErr
}

func (k *Kubeconfig) Parse() error {
	rwcs, err := k.loader.Load()
	if err != nil {
		return fmt.Errorf("failed to load: %w", err)
	}

	k.files = make([]fileEntry, 0, len(rwcs))
	for i, f := range rwcs {
		var v yaml.Node
		if err := yaml.NewDecoder(f).Decode(&v); err != nil {
			// Close all file handles on failure to avoid leaks.
			for _, rf := range rwcs {
				rf.Close()
			}
			return fmt.Errorf("failed to decode file %d: %w", i, err)
		}
		rn := yaml.NewRNode(&v)
		if rn.YNode().Kind != yaml.MappingNode {
			for _, rf := range rwcs {
				rf.Close()
			}
			return fmt.Errorf("kubeconfig file %d is not a map document", i)
		}
		var p string
		if ph, ok := f.(PathHinter); ok {
			p = ph.Path()
		}
		k.files = append(k.files, fileEntry{f: f, path: p, config: rn})
	}
	return nil
}

// ConfigPaths returns the file paths of all loaded kubeconfig files.

View on GitHub (pinned to 12ad6fb22e)

Solutions

  1. Map the file index to the N-th path in KUBECONFIG and lint its YAML
  2. Fix tab/syntax errors in that file or remove it from KUBECONFIG
  3. Empty files should be removed or given a minimal valid document
Defensive patterns

Strategy: try-catch

When it happens

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

Common situations: See trigger scenarios.

Understand the failure class


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