derailed/k9s · error

context-config yaml load failed: %w %s

Error message

context-config yaml load failed: %w
%s

What it means

Returned by the per-context config loader in internal/config/data/dir.go:98 when yaml.Unmarshal of a context-specific config file (under K9S_CONFIG_DIR/contexts/<cluster>/<context>.yaml) fails. The error deliberately embeds the full file content (%s) after the cause so the broken YAML is visible in logs. Schema violations only log a warning; a true unmarshal failure returns this error and aborts loading that context's config.

Source

Thrown at internal/config/data/dir.go:98

func (d *Dir) loadConfig(path string) (*Config, error) {
	d.mx.Lock()
	defer d.mx.Unlock()

	bb, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}
	if err := JSONValidator.Validate(json.ContextSchema, bb); err != nil {
		slog.Warn("Validation failed. Please update your config and restart!",
			slogs.Path, path,
			slogs.Error, err,
		)
	}

	var cfg Config
	if err := yaml.Unmarshal(bb, &cfg); err != nil {
		return nil, fmt.Errorf("context-config yaml load failed: %w\n%s", err, string(bb))
	}

	return &cfg, nil
}

View on GitHub (pinned to 2d3ccc6ba2)

Solutions

  1. Read the error output — it contains both the parse cause and the entire file content; fix the flagged structure
  2. Run yamllint on the exact path shown to get line/column
  3. If the file is expendable, delete it — k9s regenerates sane defaults on next activation
  4. Pin one k9s version on machines sharing the config directory

Example fix

# before: contexts/prod-c/prod.yaml
context:
  namespace:
	active: default   # TAB indentation -> unmarshal error

# after (spaces only)
context:
  namespace:
    active: default
Defensive patterns

Strategy: try-catch

Validate before calling

func contextYamlParses(path string) error {
	bb, err := os.ReadFile(path)
	if err != nil {
		return err
	}
	var probe data.Config
	return yaml.Unmarshal(bb, &probe)
}

Try / catch

if _, err := k9s.ActivateContext(name); err != nil {
	if strings.Contains(err.Error(), "context-config yaml load failed") {
		// the error embeds the full file content — log it,
		// then remove the file so defaults regenerate on next activation
	}
}

Prevention

When it happens

Trigger: Hand-editing a context yaml and breaking structure (tabs, wrong types like namespace.active: 3 when a string is expected); a context config written by a newer k9s then opened by an older one; templating tools mangling the file.

Common situations: Customizing per-context preferences (namespace favorites, view settings); downgrading k9s versions; dotfile sync across versions; partial writes on disk-full.

Related errors


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