sipeed/picoclaw · error

config is nil

Error message

config is nil

What it means

saveValidatedConfig refuses a nil *config.Config (helpers.go:107-109). It is a defensive invariant guard: the CLI always passes a config obtained from loadConfig, so a nil reaching this point means a caller ignored a load error or a new code path forgot the nil check. Not triggerable by end-user input.

Source

Thrown at cmd/picoclaw/internal/mcp/helpers.go:108

      "required": ["mcp"],
      "additionalProperties": true
    }
  },
  "required": ["tools"],
  "additionalProperties": true
}`

func loadConfig() (*config.Config, error) {
	cfg, err := config.LoadConfig(internal.GetConfigPath())
	if err != nil {
		return nil, fmt.Errorf("failed to load config: %w", err)
	}
	return cfg, nil
}

func saveValidatedConfig(cfg *config.Config) error {
	if cfg == nil {
		return fmt.Errorf("config is nil")
	}

	normalizedCfg := normalizedConfigForSave(cfg)

	data, err := json.Marshal(normalizedCfg)
	if err != nil {
		return fmt.Errorf("failed to serialize config: %w", err)
	}

	if err := validateConfigDocument(data); err != nil {
		return err
	}

	if err := config.SaveConfig(internal.GetConfigPath(), normalizedCfg); err != nil {
		return fmt.Errorf("failed to save config: %w", err)
	}

	return nil

View on GitHub (pinned to 49183d7e8d)

Solutions

  1. Propagate the loadConfig error and return early instead of continuing with nil
  2. nil-check cfg immediately after loading, before any mutation or save

Example fix

// before
cfg, err := loadConfig()
if err != nil {
	log.Println(err) // swallowed
}
_ = saveValidatedConfig(cfg) // cfg may be nil

// after
cfg, err := loadConfig()
if err != nil {
	return err
}
return saveValidatedConfig(cfg)
Defensive patterns

Strategy: validation

Validate before calling

cfg, err := loadConfig()
if err != nil {
	return err // never continue with a possibly-nil cfg
}
if cfg == nil {
	return fmt.Errorf("config load returned nil without error")
}
return saveValidatedConfig(cfg)

Prevention

When it happens

Trigger: Calling the save path with a nil pointer after swallowing a loadConfig error; a new subcommand wired up without propagating the load failure.

Common situations: Contributors adding mcp subcommands; unit tests stubbing config loading with nil.

Related errors


AI-assisted analysis of sipeed/picoclaw@49183d7e8d (2026-08-15). Data as JSON: /api/errors/643d5c74572c1b99. Report an issue: GitHub.