charmbracelet/crush · error

failed to initialize config: %w

Error message

failed to initialize config: %w

What it means

sessionSetup initializes configuration via config.Init before any session subcommand (list/show/delete/rename/last) runs. If config loading or validation fails, the setup is aborted and this wrapped error is returned, stopping the subcommand.

Source

Thrown at internal/cmd/session.go:113

	sessionCmd.AddCommand(sessionShowCmd)
	sessionCmd.AddCommand(sessionLastCmd)
	sessionCmd.AddCommand(sessionDeleteCmd)
	sessionCmd.AddCommand(sessionRenameCmd)
}

type sessionServices struct {
	sessions session.Service
	messages message.Service
	cfg      *config.ConfigStore
}

func sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func(), error) {
	dataDir, _ := cmd.Flags().GetString("data-dir")
	ctx := cmd.Context()

	cfg, err := config.Init("", dataDir, false)
	if err != nil {
		return nil, nil, nil, fmt.Errorf("failed to initialize config: %w", err)
	}
	if dataDir == "" {
		dataDir = cfg.Config().Options.DataDirectory
	}
	if shouldEnableMetrics(cfg.Config()) {
		event.Init()
	}

	conn, err := db.Connect(ctx, dataDir)
	if err != nil {
		return nil, nil, nil, fmt.Errorf("failed to connect to database: %w", err)
	}

	queries := db.New(conn)
	svc := &sessionServices{
		sessions: session.NewService(queries, conn),
		messages: message.NewService(queries),
		cfg:      cfg,

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Run `crush` or the config validation command to see the underlying validation error from the wrapped chain
  2. Validate JSON syntax of crush.json / crushrc (jq . or bash -n)
  3. Check file permissions on the config file and the data directory
  4. Temporarily move the config aside and rerun to confirm it is the config content vs environment
Defensive patterns

Strategy: validation

Validate before calling

// validate config before invoking the command
if _, err := os.Stat(configPath); err != nil {
    return fmt.Errorf("config file missing: %w", err)
}
if f, err := os.Open(configPath); err == nil {
    defer f.Close()
    if err := json.NewDecoder(f).Decode(&struct{}{}); err != nil {
        return fmt.Errorf("invalid config JSON: %w", err)
    }
}

Try / catch

cfg, err := config.Init("", dataDir, false)
if err != nil {
    slog.Error("Config initialization failed", "error", err)
    os.Exit(1)
}

Prevention

When it happens

Trigger: config.Init("", dataDir, false) fails: malformed crushrc/crush.json, unreadable config file, invalid provider or options in config, or filesystem permission errors on the data directory.

Common situations: Syntax error in ~/.config/crush/crush.json or crushrc after hand-editing; CRUSH_DATA_DIR pointing to a non-writable path; deprecated/unknown keys left in config after a version upgrade.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/1ab73658a7554a63. Report an issue: GitHub.