Skip to content

fix: honor XDG_CONFIG_HOME, XDG_DATA_HOME, and OpenCode env overrides#73

Merged
marcusolsson merged 2 commits into
mainfrom
fix/opencode-xdg-paths
Jun 3, 2026
Merged

fix: honor XDG_CONFIG_HOME, XDG_DATA_HOME, and OpenCode env overrides#73
marcusolsson merged 2 commits into
mainfrom
fix/opencode-xdg-paths

Conversation

@marcusolsson

Copy link
Copy Markdown
Contributor

Problem

The CLI hardcoded OpenCode config and auth paths, ignoring the XDG base directory spec and OpenCode's own env overrides:

  • Config dir was always ~/.config/opencode — ignored XDG_CONFIG_HOME and OPENCODE_CONFIG_DIR
  • Auth file was always ~/.local/share/opencode/auth.json — ignored XDG_DATA_HOME
  • OPENCODE_CONFIG (single-file override) was not checked
  • Auth path used string concatenation with forward slashes instead of path.join()

OpenCode resolves these via the xdg-basedir package, which has no Windows-specific branch — the same rules apply on all platforms. Hardcoding the paths breaks users who relocate XDG dirs (common on Windows too).

Changes

  • New: src/commands/code/xdg-paths.ts — centralized path resolver

    • getOpencodeConfigDir() — honors OPENCODE_CONFIG_DIR, then XDG_CONFIG_HOME, then ~/.config/opencode
    • getOpencodeDataDir() — honors XDG_DATA_HOME, then ~/.local/share/opencode
    • resolveGlobalConfigPath() — honors OPENCODE_CONFIG env var
    • getOpencodeAuthPath() — uses path.join() for correct separators on Windows
  • Updated: src/commands/code/opencode.ts — uses new resolvers for config dir, agents dir, and global config file

  • Updated: src/commands/code/auth-sync.ts — uses new resolver for auth file, replaces string concat with path.join()

Verification

All 223 tests pass.

Checklist

  • Resolve config directory via XDG_CONFIG_HOME / homedir + /.config on all platforms
  • Use language path-join instead of string concatenation
  • Target the config directory for opencode.json, not the data directory
  • Honor OPENCODE_CONFIG (single-file override)
  • Honor OPENCODE_CONFIG_DIR (directory override for agents/commands/plugins)
  • Support both opencode.json and opencode.jsonc

…ODE_CONFIG_DIR

OpenCode resolves config paths via the xdg-basedir npm package, which has
no Windows-specific branch. The resolution rule is identical on all platforms:
use the XDG_*_HOME env var if set, otherwise fall back to homedir + a fixed
suffix.

Previous code hardcoded ~/.config/opencode and ~/.local/share/opencode,
breaking for users who relocate XDG dirs (including on Windows).

Changes:
- Add src/commands/code/xdg-paths.ts with centralized path resolvers
- Update auth-sync.ts to use XDG_DATA_HOME for auth.json location
- Update opencode.ts to use XDG_CONFIG_HOME / OPENCODE_CONFIG_DIR / OPENCODE_CONFIG
- Replace string concatenation with path.join() for cross-platform separators

All 223 tests pass.
GitHub Actions runners set XDG_CONFIG_HOME and XDG_DATA_HOME, causing
tests that assert hardcoded paths to fail. Clear these env vars before
each test to ensure deterministic path resolution.
@marcusolsson marcusolsson merged commit 4ae77ac into main Jun 3, 2026
1 check passed
@marcusolsson marcusolsson deleted the fix/opencode-xdg-paths branch June 3, 2026 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant