alibaba/open-code-review · error
resolve home dir: %w
Error message
resolve home dir: %w
What it means
This error wraps os.UserHomeDir() failures that occur when the JSONL session writer opens its session file. The library stores review sessions under ~/.opencodereview/<sessions>/<encoded-repo>, so it must resolve the user's home directory first. If that resolution fails, no session file can be created and open() aborts with this wrapped error.
Source
Thrown at internal/session/persist.go:106
// Replace separators with -
p = strings.ReplaceAll(p, "/", "-")
p = strings.ReplaceAll(p, "\\", "-")
// Replace colons (from Windows drive letters)
vol = strings.ReplaceAll(vol, ":", "_")
// Handle edge case where path was only separators or volume name
result := vol + p
if result == "" {
return "empty"
}
return result
}
func (jw *jsonlWriter) open() error {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("resolve home dir: %w", err)
}
sessionDir := filepath.Join(home, ".opencodereview", sessionSubDir, encodeRepoPath(jw.repoDir))
if err := os.MkdirAll(sessionDir, 0700); err != nil {
return fmt.Errorf("create session dir: %w", err)
}
filename := filepath.Join(sessionDir, jw.sessionID+".jsonl")
f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("open session file: %w", err)
}
jw.file = f
jw.writer = bufio.NewWriter(f)
return nil
}
View on GitHub (pinned to 5cf97d0d15)
Solutions
- Set the HOME environment variable to a writable directory (e.g. export HOME=/root or HOME=/tmp) before invoking the tool
- Run the process as a user that exists in /etc/passwd with a valid home directory
- If in Docker, add ENV HOME=/root to the image or pass -e HOME=... at runtime
- Verify with `go env` or a quick `os.UserHomeDir()` test that home resolution succeeds in the target environment
Example fix
// before (failing shell) cron: ocr review ... // after HOME=/root ocr review ... # or set HOME in the crontab/service unit
Defensive patterns
Strategy: validation
Validate before calling
if os.Getenv("HOME") == "" {
if _, err := os.UserHomeDir(); err != nil {
return fmt.Errorf("HOME must be set: %w", err)
}
} Try / catch
if err := runOCR(); err != nil {
var pe *fs.PathError
if strings.Contains(err.Error(), "resolve home dir") {
log.Fatalf("Set HOME to a writable directory: %v", err)
}
} Prevention
- Always export HOME in cron jobs, systemd units, and Docker images
- Run services as users with valid passwd entries
- Smoke-test home resolution in your CI image before depending on the tool
When it happens
Trigger: newJSONLWriter -> open() calls os.UserHomeDir() and it returns an error: $HOME is unset in the environment and no passwd entry exists for the current user (typical in cron jobs, Docker containers, or bare CI runners running as a non-login user).
Common situations: Running `ocr` inside a Docker container without HOME set; systemd services with a minimal Environment=; CI jobs executing as a uid with no home directory; Go cross-runtime environments where user.Current() cannot read /etc/passwd.
Related errors
- resolve home dir: %w
- cannot determine home directory: %w
- read background file %q: %w
- background file %q is a directory, not a file
- resolve LLM endpoint: %w
AI-assisted analysis of alibaba/open-code-review@5cf97d0d15 (2026-09-02).
Data as JSON: /api/errors/27782a96c2596c1a.
Report an issue: GitHub.