charmbracelet/crush · error

config not loaded

Error message

config not loaded

What it means

Returned by ProjectNeedsInitialization when the passed *ConfigStore is nil. The nil store means the config was never loaded, so the function cannot locate the data directory to check the init flag file.

Source

Thrown at internal/config/init.go:31

const (
	InitFlagFilename = "init"
)

type ProjectInitFlag struct {
	Initialized bool `json:"initialized"`
}

func Init(workingDir, dataDir string, debug bool) (*ConfigStore, error) {
	store, err := Load(workingDir, dataDir, debug)
	if err != nil {
		return nil, err
	}
	return store, nil
}

func ProjectNeedsInitialization(store *ConfigStore) (bool, error) {
	if store == nil {
		return false, fmt.Errorf("config not loaded")
	}

	cfg := store.Config()
	flagFilePath := filepath.Join(cfg.Options.DataDirectory, InitFlagFilename)

	_, err := os.Stat(flagFilePath)
	if err == nil {
		return false, nil
	}

	if !os.IsNotExist(err) {
		return false, fmt.Errorf("failed to check init flag file: %w", err)
	}

	someContextFileExists, err := contextPathsExist(store.WorkingDir())
	if err != nil {
		return false, fmt.Errorf("failed to check for context files: %w", err)
	}

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Ensure config.Load succeeded and returned a non-nil *ConfigStore before calling ProjectNeedsInitialization.
  2. Check and handle the error returned by config.Load instead of using its nil result.
  3. Guard call sites with an explicit nil check on the store.

Example fix

// before
needsInit, _ := config.ProjectNeedsInitialization(store)
// after
if store == nil {
	return fmt.Errorf("config must be loaded before init check")
}
needsInit, err := config.ProjectNeedsInitialization(store)
Defensive patterns

Strategy: validation

Validate before calling

if store == nil {
	return errors.New("config store is nil: load config first")
}

Type guard

func storeLoaded(s *config.ConfigStore) bool { return s != nil }

Try / catch

needsInit, err := config.ProjectNeedsInitialization(store)
if err != nil {
	if err.Error() == "config not loaded" {
		// run config.Load first
	}
	return err
}

Prevention

When it happens

Trigger: Calling config.ProjectNeedsInitialization(nil), typically after a failed/short-circuited config.Load whose result was nil but was used anyway.

Common situations: Ignoring the error from config.Load and passing its nil store onward; calling initialization checks before any config exists; error-handling paths that forget to return early.

Related errors


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