{"record":{"id":"332430150b5e336c","repo":"alibaba/open-code-review","slug":"resolve-home-dir-w","errorCode":null,"errorMessage":"resolve home dir: %w","messagePattern":"resolve home dir: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/session/list.go","lineNumber":101,"sourceCode":"\tFilesReviewed   []string        `json:\"files_reviewed\"`\n\tDurationSeconds float64         `json:\"duration_seconds\"`\n\tLLMFailures     int64           `json:\"llm_failures\"`\n\tRunManifest     *RunManifest    `json:\"run_manifest\"`\n\tSchemaVersion   string          `json:\"schema_version\"`\n\tRunID           string          `json:\"run_id\"`\n\tParentRunID     string          `json:\"parent_run_id\"`\n\tSourceProvider  string          `json:\"source_provider\"`\n\tSourceModel     string          `json:\"source_model\"`\n\tTargetProvider  string          `json:\"target_provider\"`\n\tTargetModel     string          `json:\"target_model\"`\n}\n\n// SessionsDir returns the on-disk directory that holds JSONL session files\n// for a given repository. It does not create the directory.\nfunc SessionsDir(repoDir string) (string, error) {\n\thome, err := os.UserHomeDir()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"resolve home dir: %w\", err)\n\t}\n\treturn filepath.Join(home, \".opencodereview\", sessionSubDir, encodeRepoPath(repoDir)), nil\n}\n\n// ListSessions enumerates all persisted sessions for the given repository\n// directory, sorted by StartTime descending (most recent first). Missing\n// directories return an empty slice with no error.\nfunc ListSessions(repoDir string) ([]Summary, error) {\n\tdir, err := SessionsDir(repoDir)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tentries, err := os.ReadDir(dir)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\treturn nil, nil\n\t\t}\n\t\treturn nil, fmt.Errorf(\"read sessions dir %q: %w\", dir, err)","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/alibaba/open-code-review/blob/5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f/internal/session/list.go#L83-L119","documentation":"SessionsDir computes the per-repo session directory under ~/.opencodereview. It calls os.UserHomeDir, and if that fails (home directory cannot be determined) the error is wrapped as 'resolve home dir: ...'. No directory is created by this function.","triggerScenarios":"Calling SessionsDir (directly or via ListSessions/countSessionFiles) when os.UserHomeDir returns an error — $HOME is empty on Unix or the USERPROFILE lookup fails on Windows.","commonSituations":"Running in CI containers or systemd services with HOME unset; sudo/su environments dropping HOME; Docker images running as non-root user without HOME configured.","solutions":["Export HOME (Unix) or USERPROFILE (Windows) to a writable directory before running the command.","If using sudo, use `sudo -E` or set HOME explicitly.","In containers, set ENV HOME=/root (or the app user's home) in the image.","Fix the wrapped OS error if HOME is set but invalid (e.g. home listed in /etc/passwd does not exist)."],"exampleFix":"// before\nocr session list\n// error: resolve home dir: $HOME is not defined\n// after\nexport HOME=/home/dev\nocr session list","handlingStrategy":"validation","validationCode":"if os.Getenv(\"HOME\") == \"\" && runtime.GOOS != \"windows\" {\n    return fmt.Errorf(\"HOME must be set before using session features\")\n}\nif _, err := os.UserHomeDir(); err != nil {\n    return fmt.Errorf(\"home dir unresolvable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"dir, err := session.SessionsDir(repoDir)\nif err != nil {\n    if strings.Contains(err.Error(), \"resolve home dir\") {\n        os.Setenv(\"HOME\", \"/tmp\") // or prompt user / fail fast\n        return session.SessionsDir(repoDir)\n    }\n    return err\n}","preventionTips":["Always export HOME (or USERPROFILE on Windows) in CI and service environments.","Use `sudo -E` to preserve HOME through privilege escalation.","Set HOME explicitly in Dockerfiles for non-root users.","Check os.UserHomeDir() early in tool wrappers that depend on session history."],"tags":["session","home-dir","environment"],"backgroundTag":"missing-env-var","analyzedSha":"5cf97d0d15cbd41b602513c4be3bfec3cee5bf7f","analyzedAt":"2026-09-02T02:08:09.116Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}