feat: respect XDG_DATA_HOME and CLAUDE_CONFIG_DIR for path resolution - #217
feat: respect XDG_DATA_HOME and CLAUDE_CONFIG_DIR for path resolution#217troian wants to merge 1 commit into
Conversation
|
hey @griffinmartin any chance you can take a look |
|
@troian Sorry I've been pretty busy so haven't been testing all the open PRs as much as I have in the past. I'll get to it this week. |
|
I have a similar issue. Running three Claude Team accounts across different project dirs via CLAUDE_CONFIG_DIR + direnv, and the account selection bleeds across all of them because claude-account-source.txt is always written to the hardcoded path. Ended up pre-writing it from a shell wrapper before each opencode launch as a workaround, but that breaks every update. |
|
@markthepixel do you mind trying this PR? |
|
I made a chance to keep the XDG_DATA_HOME and CLAUDE_CONFIG_DIR behavior as-is, but adds OPENCODE_ACCOUNT_SOURCE_FILE as an explicit override for just the persisted account selection file. That covers the case where someone wants one shared OpenCode db and session history, but still wants Claude account selection pinned per workspace, which is my issue |
5c134cf to
bed47d0
Compare
|
@griffinmartin bump for review |
a96ddad to
5e10fea
Compare
Greptile SummaryThis PR introduces a new
Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/paths.ts | New module centralising XDG path resolution; correctly reads XDG_DATA_HOME at call time (not module-load time), handles empty-string per spec, and appends opencode for the data dir. Clean and well-commented. |
| src/credentials.ts | Delegates getAccountStateFile() and getAuthJsonPaths() to getOpencodeDataDir() from paths.ts. Adds OPENCODE_ACCOUNT_SOURCE_FILE escape hatch. Logic is unchanged; Windows dual-write behaviour (XDG + LOCALAPPDATA) was flagged in a prior review. |
| src/refresh-lock.ts | Replaces the hardcoded default lock dir with getOpencodeDataDir(). Env-var override (OPENCODE_CLAUDE_AUTH_REFRESH_LOCK_DIR) correctly retains higher priority. Per-instance XDG dirs mean lock files are also isolated per instance — acceptable for the documented "different accounts" use case. |
| src/credentials.test.ts | Adds XDG_DATA_HOME tests for saveAccountSource, loadPersistedAccountSource, and syncAuthJson. Correctly deletes ambient XDG_DATA_HOME at suite start and copies paths.ts into each sandboxed temp module. Good coverage of the new code paths. |
| src/refresh-lock.test.ts | New test suite exercises defaultLockDir() XDG resolution and verifies OPENCODE_CLAUDE_AUTH_REFRESH_LOCK_DIR keeps precedence over XDG_DATA_HOME. Env isolation via beforeEach/afterEach is correct. |
| README.md | Adds a "Parallel instances" section with a concrete bash example and updates the env-var reference table with XDG_DATA_HOME and OPENCODE_ACCOUNT_SOURCE_FILE. Clear and accurate documentation of the new feature. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[OpenCode starts] --> B{XDG_DATA_HOME set?}
B -- Yes --> C["getDataHome() → $XDG_DATA_HOME"]
B -- No --> D["getDataHome() → ~/.local/share"]
C --> E["getOpencodeDataDir() → {dataHome}/opencode"]
D --> E
E --> F1["claude-account-source.txt\n(credentials.ts)"]
E --> F2["auth.json\n(credentials.ts)"]
E --> F3["claude-auth-debug.log\n(logger.ts)"]
E --> F4["claude-auth-refresh-*.lock\n(refresh-lock.ts)"]
F2 --> G{platform win32?}
G -- Yes --> H["Also write to\n%LOCALAPPDATA%/opencode/auth.json"]
G -- No --> I[XDG path only]
J{OPENCODE_ACCOUNT_SOURCE_FILE set?} -- Yes --> K[Use explicit path]
J -- No --> F1
L{OPENCODE_CLAUDE_AUTH_REFRESH_LOCK_DIR set?} -- Yes --> M[Use explicit dir]
L -- No --> F4
style H fill:#ffe0b2,stroke:#e65100
Reviews (2): Last reviewed commit: "feat: respect XDG_DATA_HOME and CLAUDE_C..." | Re-trigger Greptile
The plugin hardcoded ~/.local/share/opencode/ and ~/.claude/ for all file paths, ignoring XDG_DATA_HOME and CLAUDE_CONFIG_DIR. This prevented running multiple OpenCode instances in parallel with different Claude accounts, since all instances shared the same claude-account-source.txt. Changes: - credentials.ts: getAccountStateFile() and getAuthJsonPaths() use $XDG_DATA_HOME with fallback to ~/.local/share - keychain.ts: readCredentialsFile() and writeBackCredentials() use $CLAUDE_CONFIG_DIR with fallback to ~/.claude - logger.ts: getDefaultLogPath() uses $XDG_DATA_HOME with fallback to ~/.local/share - Tests added for all new paths; existing tests updated to isolate env vars that now affect path resolution - README updated with env var docs and parallel instance usage When unset, behavior is unchanged (same defaults as before). Signed-off-by: Artur Troian <troian@users.noreply.github.com>
5e10fea to
af0a8ad
Compare
|
@griffinmartin bump for review again |
The plugin hardcoded ~/.local/share/opencode/ and ~/.claude/ for all file paths, ignoring XDG_DATA_HOME and CLAUDE_CONFIG_DIR. This prevented running multiple OpenCode instances in parallel with different Claude accounts, since all instances shared the same claude-account-source.txt. Changes:
When unset, behavior is unchanged (same defaults as before).
Summary
Related issue
Testing
Checklist
feat:,fix:,docs:,chore:, etc.)make allpasses locally (runs lint, build, and test)