Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
fix(opencode): use ~/.config/opencode for plugin path on macOS (#2655)
Worktrunk Bot9af258ddab28
diff --git a/src/cli/config.rs b/src/cli/config.rsindex 7cbd28186..363ff8015 100644--- a/src/cli/config.rs+++ b/src/cli/config.rs@@ -167,8 +167,9 @@ $ wt config plugins opencode install --yes## Plugin location-The plugin is written to `~/.config/opencode/plugins/worktrunk.ts`.-Override with the `OPENCODE_CONFIG_DIR` environment variable."#+The plugin is written to `~/.config/opencode/plugins/worktrunk.ts`,+mirroring OpenCode's own global-config precedence:+`$OPENCODE_CONFIG_DIR` > `$XDG_CONFIG_HOME/opencode` > `~/.config/opencode`."#)]Install,@@ -256,7 +257,9 @@ $ wt config plugins opencode uninstall## Plugin location-Written to `~/.config/opencode/plugins/worktrunk.ts` (or `$OPENCODE_CONFIG_DIR/plugins/worktrunk.ts`)."#+Written to `~/.config/opencode/plugins/worktrunk.ts`. Honors OpenCode's+config precedence: `$OPENCODE_CONFIG_DIR` > `$XDG_CONFIG_HOME/opencode` >+`~/.config/opencode`."#)]Opencode {#[command(subcommand)]diff --git a/src/commands/config/opencode.rs b/src/commands/config/opencode.rsindex 7fa2028ff..1fd3ab34d 100644--- a/src/commands/config/opencode.rs+++ b/src/commands/config/opencode.rs@@ -2,7 +2,8 @@//!//! Installs the worktrunk activity tracking plugin for OpenCode.//! The plugin source (`dev/opencode-plugin.ts`) is embedded in the binary via-//! `include_str!()` and written to `${OPENCODE_CONFIG_DIR:-~/.config/opencode}/plugins/worktrunk.ts`.+//! `include_str!()` and written under the OpenCode global-config directory (see+//! `opencode_plugins_dir` for the precedence rules) at `…/plugins/worktrunk.ts`.use std::path::PathBuf;@@ -18,13 +19,23 @@ const PLUGIN_SOURCE: &str = include_str!("../../../dev/opencode-plugin.ts");/// Resolve the OpenCode plugins directory.///-/// Uses `$OPENCODE_CONFIG_DIR/plugins/` if set, otherwise `~/.config/opencode/plugins/`.+/// Mirrors OpenCode's own global-config precedence (see <https://opencode.ai/docs/config>):+/// `$OPENCODE_CONFIG_DIR` > `$XDG_CONFIG_HOME/opencode` > `~/.config/opencode`. The macOS+/// `~/Library/Application Support/opencode/` path is reserved for *managed* settings and is+/// not where OpenCode looks for user plugins, so we deliberately avoid `dirs::config_dir()`+/// here — it would put the plugin in the wrong place on macOS.fn opencode_plugins_dir() -> Result<PathBuf> {let config_dir = if let Ok(dir) = std::env::var("OPENCODE_CONFIG_DIR") {PathBuf::from(dir)+ } else if let Some(xdg) = std::env::var("XDG_CONFIG_HOME")+ .ok()+ .filter(|s| !s.is_empty())+ {+ PathBuf::from(xdg).join("opencode")} else {- dirs::config_dir()- .context("Could not determine config directory")?+ worktrunk::path::home_dir()+ .context("Could not determine home directory")?+ .join(".config").join("opencode")};Ok(config_dir.join("plugins"))diff --git a/tests/integration_tests/config_show.rs b/tests/integration_tests/config_show.rsindex 1e06f6007..4c8e0f824 100644--- a/tests/integration_tests/config_show.rs+++ b/tests/integration_tests/config_show.rs@@ -2347,25 +2347,22 @@ fn test_opencode_uninstall_not_installed(temp_home: TempDir) {});}-/// Install uses dirs::config_dir() fallback when OPENCODE_CONFIG_DIR is unset.+/// Install honours `XDG_CONFIG_HOME` when `OPENCODE_CONFIG_DIR` is unset.///-/// This exercises the `dirs::config_dir()` branch in `opencode_plugins_dir()`-/// (lines 26-28 of opencode.rs). On Linux with XDG_CONFIG_HOME set, dirs-/// resolves to `$XDG_CONFIG_HOME`, so the plugin lands at-/// `{temp_home}/.config/opencode/plugins/worktrunk.ts`.-///-/// Linux-only: `dirs::config_dir()` resolves differently per platform-/// (macOS: `~/Library/Application Support`, Windows: native API), making-/// the path assertion platform-specific. The core install logic is tested-/// cross-platform via `OPENCODE_CONFIG_DIR` in other tests.-#[cfg(target_os = "linux")]+/// Cross-platform: OpenCode's documented global-config location is+/// `~/.config/opencode/` (or `$XDG_CONFIG_HOME/opencode/`) on every OS, so the+/// plugin must land at `{temp_home}/.config/opencode/plugins/worktrunk.ts`+/// regardless of platform. Previously this test was Linux-only because the+/// implementation called `dirs::config_dir()`, which on macOS returns+/// `~/Library/Application Support` (the *managed* settings path, not the+/// plugin path) — see #2654.#[rstest]-fn test_opencode_install_uses_dirs_fallback(temp_home: TempDir) {+fn test_opencode_install_uses_xdg_config_home(temp_home: TempDir) {let settings = setup_home_snapshot_settings(&temp_home);settings.bind(|| {let mut cmd = wt_command();set_temp_home_env(&mut cmd, temp_home.path());- // Remove OPENCODE_CONFIG_DIR so the code falls through to dirs::config_dir()+ // Remove OPENCODE_CONFIG_DIR so the code falls through to XDG_CONFIG_HOMEcmd.env_remove("OPENCODE_CONFIG_DIR");cmd.args(["config", "plugins", "opencode", "install", "--yes"]);@@ -2374,15 +2371,49 @@ fn test_opencode_install_uses_dirs_fallback(temp_home: TempDir) {let canonical_home =crate::common::canonicalize(temp_home.path()).unwrap_or_else(|_| temp_home.path().into());- // dirs::config_dir() uses XDG_CONFIG_HOME on Linux → {temp_home}/.config+ // set_temp_home_env sets XDG_CONFIG_HOME = $HOME/.configlet plugin_path = canonical_home.join(".config/opencode/plugins/worktrunk.ts");assert!(plugin_path.exists(),- "Plugin file should exist at dirs::config_dir() fallback path: {}",+ "Plugin file should exist at XDG_CONFIG_HOME fallback path: {}",plugin_path.display());}+/// Install defaults to `$HOME/.config/opencode/plugins/` when neither+/// `OPENCODE_CONFIG_DIR` nor `XDG_CONFIG_HOME` is set.+///+/// Cross-platform regression guard for #2654: with the previous+/// `dirs::config_dir()`-based fallback, this test passed on Linux (XDG default)+/// but failed on macOS (`~/Library/Application Support`). After the fix, the+/// fallback is `$HOME/.config/opencode/plugins/worktrunk.ts` on every OS, in+/// line with OpenCode's own global-config precedence.+#[rstest]+fn test_opencode_install_defaults_to_home_dot_config(temp_home: TempDir) {+ let mut cmd = wt_command();+ set_temp_home_env(&mut cmd, temp_home.path());+ cmd.env_remove("OPENCODE_CONFIG_DIR");+ cmd.env_remove("XDG_CONFIG_HOME");+ cmd.args(["config", "plugins", "opencode", "install", "--yes"]);++ let output = cmd.output().expect("install command should run");+ assert!(+ output.status.success(),+ "install failed: stdout={}, stderr={}",+ String::from_utf8_lossy(&output.stdout),+ String::from_utf8_lossy(&output.stderr),+ );++ let canonical_home =+ crate::common::canonicalize(temp_home.path()).unwrap_or_else(|_| temp_home.path().into());+ let plugin_path = canonical_home.join(".config/opencode/plugins/worktrunk.ts");+ assert!(+ plugin_path.exists(),+ "Plugin should default to $HOME/.config/opencode/plugins/worktrunk.ts but file not found at: {}",+ plugin_path.display(),+ );+}+/// Install prompt declined (no `--yes`, piped stdin → empty → declined)./// Exercises the `return Ok(())` branch at lines 83-84 of opencode.rs.#[rstest]diff --git a/tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_dirs_fallback.snap b/tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_xdg_config_home.snapsimilarity index 85%rename from tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_dirs_fallback.snaprename to tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_xdg_config_home.snapindex 4783853f2..0253e96e5 100644--- a/tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_dirs_fallback.snap+++ b/tests/snapshots/integration__integration_tests__config_show__opencode_install_uses_xdg_config_home.snap@@ -4,6 +4,7 @@ info:program: wtargs:- config+ - plugins- opencode- install- "--yes"@@ -11,10 +12,7 @@ info:APPDATA: "[TEST_CONFIG_HOME]"CLICOLOR_FORCE: "1"COLUMNS: "500"- GIT_BRANCH_ICON: ""- GIT_DIR_ICON: ""- GIT_REMOTE_BRANCH_ICON: ""- GIT_WORKTREE_ICON: ""+ GIT_EDITOR: ""HOME: "[TEST_HOME]"LANG: CLC_ALL: C@@ -28,13 +26,17 @@ info:WORKTRUNK_APPROVALS_PATH: "[TEST_APPROVALS]"WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"WORKTRUNK_SYSTEM_CONFIG_PATH: "[TEST_SYSTEM_CONFIG]"+ WORKTRUNK_TEST_BASH_INSTALLED: "0"WORKTRUNK_TEST_CLAUDE_INSTALLED: "0"WORKTRUNK_TEST_DELAYED_STREAM_MS: "-1"WORKTRUNK_TEST_EPOCH: "1735776000"+ WORKTRUNK_TEST_FISH_INSTALLED: "0"WORKTRUNK_TEST_NUSHELL_ENV: "0"WORKTRUNK_TEST_OPENCODE_INSTALLED: "0"WORKTRUNK_TEST_POWERSHELL_ENV: "0"+ WORKTRUNK_TEST_POWERSHELL_INSTALLED: "0"WORKTRUNK_TEST_SKIP_URL_HEALTH_CHECK: "1"+ WORKTRUNK_TEST_ZSH_INSTALLED: "0"XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"---success: true