alibaba/open-code-review · error

cannot determine home directory: %w

Error message

cannot determine home directory: %w

What it means

defaultConfigPath builds the default config location ~/.opencodereview/config.json using os.UserHomeDir(). This error wraps the failure of that lookup. os.UserHomeDir fails when neither $HOME (Unix) nor %USERPROFILE% (Windows) is set, so the tool cannot know where to place the config file.

Source

Thrown at cmd/opencodereview/config_cmd.go:95

	Short: "Interactive model selection",
	Args:  cobra.NoArgs,
	RunE: func(cmd *cobra.Command, args []string) error {
		return runConfigModel()
	},
}

func init() {
	configCmd.AddCommand(configSetCmd)
	configCmd.AddCommand(configUnsetCmd)
	configCmd.AddCommand(configProviderCmd)
	configCmd.AddCommand(configModelCmd)
}

// Default config file location: ~/.opencodereview/config.json
func defaultConfigPath() (string, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return "", fmt.Errorf("cannot determine home directory: %w", err)
	}
	return filepath.Join(home, ".opencodereview", "config.json"), nil
}

// resolveConfigPath returns OCR_CONFIG_PATH when set, otherwise the default user config path.
// Intentionally used only by read-only commands (e.g. ocr llm test). Write paths such as
// config set and review keep defaultConfigPath() so a leaked OCR_CONFIG_PATH cannot redirect writes.
func resolveConfigPath() (string, error) {
	if p := strings.TrimSpace(os.Getenv("OCR_CONFIG_PATH")); p != "" {
		return p, nil
	}
	return defaultConfigPath()
}

func runConfigSet(key, value string) error {
	configPath, err := defaultConfigPath()
	if err != nil {
		return err

View on GitHub (pinned to 5cf97d0d15)

Solutions

  1. Set HOME explicitly: `export HOME=/home/<user>` (or `ENV HOME=/root` in the Dockerfile) before running ocr.
  2. Note: `ocr config set` intentionally ignores OCR_CONFIG_PATH for writes, so setting that env var will not help here — fix HOME instead.
  3. For read-only commands, OCR_CONFIG_PATH can bypass the home lookup: `OCR_CONFIG_PATH=/etc/ocr/config.json ocr llm test`.
  4. Verify the environment: `env | grep ^HOME=` and check the service user's passwd entry (`getent passwd <user>`).

Example fix

// before (Dockerfile)
RUN ocr config set provider anthropic
// after (Dockerfile)
ENV HOME=/root
RUN ocr config set provider anthropic
Defensive patterns

Strategy: fallback

Validate before calling

// shell: ensure HOME is resolvable before invoking ocr config write commands
[ -n "${HOME:-}" ] || { echo "HOME is not set"; exit 1; }
# or for read-only commands, pin the path:
export OCR_CONFIG_PATH="${OCR_CONFIG_PATH:-$HOME/.opencodereview/config.json}"

Try / catch

out, err := exec.Command("ocr", "config", "set", key, val).CombinedOutput()
if err != nil && strings.Contains(string(out), "cannot determine home directory") {
	cmd.Env = append(os.Environ(), "HOME=/tmp/ocr-home") // retry with explicit HOME
}

Prevention

When it happens

Trigger: Running `ocr config set/unset` (which call defaultConfigPath) in an environment where HOME is unset — cron jobs, systemd units, Docker containers running as non-root without ENV HOME, stripped-down CI runners, or SSH with a malformed environment.

Common situations: Dockerfile missing `ENV HOME=/root`; running ocr under a service account whose passwd entry lacks a home; `sudo -u app` without preserving HOME; minimal Alpine/busybox containers; CI jobs running with `env -i`.

Understand the failure class

Background: "environment variable is not set" and "Missing keys in environment" errors: what missing required env var messages mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02). Data as JSON: /api/errors/734265fb4329519f. Report an issue: GitHub.