diff --git a/crates/ide-agent/src/native/claude/cli.rs b/crates/ide-agent/src/native/claude/cli.rs index 3f501b6e8c..99affc5c97 100644 --- a/crates/ide-agent/src/native/claude/cli.rs +++ b/crates/ide-agent/src/native/claude/cli.rs @@ -134,13 +134,14 @@ impl Cli for Process { } } -/// Claude Code's configuration directory (`CLAUDE_CONFIG_DIR`, else `~/.claude`). +/// Claude Code's configuration directory (`CLAUDE_CONFIG_DIR`, else `~/.claude`); an empty value counts as +/// unset, and a leading `~` is the user's home. pub(crate) fn config_dir(env: &[(String, String)]) -> Option { let set = env .iter() - .find(|(k, _)| k == "CLAUDE_CONFIG_DIR") - .map(|(_, v)| PathBuf::from(v)) - .or_else(|| std::env::var_os("CLAUDE_CONFIG_DIR").map(PathBuf::from)); + .find(|(k, v)| k == "CLAUDE_CONFIG_DIR" && !v.is_empty()) + .map(|(_, v)| PathBuf::from(crate::provider::expand_home(v))) + .or_else(|| std::env::var_os("CLAUDE_CONFIG_DIR").filter(|v| !v.is_empty()).map(PathBuf::from)); set.or_else(|| { std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(|h| Path::new(&h).join(".claude")) }) @@ -160,4 +161,19 @@ mod tests { "--resume a --fork-session --session-id b" ); } + + #[test] + fn an_empty_config_dir_is_unset() { + let dir = config_dir(&[("CLAUDE_CONFIG_DIR".into(), String::new())]); + assert!(dir.is_some_and(|d| d.is_absolute())); + let set = config_dir(&[("CLAUDE_CONFIG_DIR".into(), "/home/me/.claude-work".into())]); + assert_eq!(set, Some(PathBuf::from("/home/me/.claude-work"))); + } + + #[test] + fn a_config_dir_under_the_home_folder_can_start_with_a_tilde() { + let home = crate::home_dir().unwrap(); + let dir = config_dir(&[("CLAUDE_CONFIG_DIR".into(), "~/.claude-work".into())]); + assert_eq!(dir, Some(home.join(".claude-work"))); + } } diff --git a/crates/ide-agent/src/native/copilot/mod.rs b/crates/ide-agent/src/native/copilot/mod.rs index ecca42a17b..25432b5e09 100644 --- a/crates/ide-agent/src/native/copilot/mod.rs +++ b/crates/ide-agent/src/native/copilot/mod.rs @@ -37,12 +37,13 @@ const DISCOVER_TIMEOUT: Duration = if cfg!(test) { Duration::from_secs(5) } else const EARLY_CANCEL: Duration = Duration::from_secs(2); const EXIT_GRACE: Duration = Duration::from_millis(100); -/// Copilot's configuration directory (`COPILOT_HOME`, otherwise `/.copilot`). +/// Copilot's configuration directory (`COPILOT_HOME`, otherwise `/.copilot`); an empty value counts as +/// unset, and a leading `~` is the user's home. pub(crate) fn config_dir(env: &[(String, String)], home: Option<&Path>) -> Option { env.iter() - .find(|(key, _)| key == "COPILOT_HOME") - .map(|(_, value)| PathBuf::from(value)) - .or_else(|| std::env::var_os("COPILOT_HOME").map(PathBuf::from)) + .find(|(key, value)| key == "COPILOT_HOME" && !value.is_empty()) + .map(|(_, value)| PathBuf::from(crate::provider::expand_home(value))) + .or_else(|| std::env::var_os("COPILOT_HOME").filter(|v| !v.is_empty()).map(PathBuf::from)) .or_else(|| home.map(|home| home.join(".copilot"))) } diff --git a/crates/ide-agent/src/native/copilot/tests.rs b/crates/ide-agent/src/native/copilot/tests.rs index f84258a049..1ddd1fd70c 100644 --- a/crates/ide-agent/src/native/copilot/tests.rs +++ b/crates/ide-agent/src/native/copilot/tests.rs @@ -124,7 +124,17 @@ fn resolves_copilot_home_from_agent_env_or_user_home() { ); if std::env::var_os("COPILOT_HOME").is_none() { assert_eq!(super::config_dir(&[], Some(home)), Some(home.join(".copilot"))); + assert_eq!( + super::config_dir(&[("COPILOT_HOME".into(), String::new())], Some(home)), + Some(home.join(".copilot")) + ); } + assert!(super::config_dir(&[("COPILOT_HOME".into(), String::new())], Some(home)).is_some_and(|d| d.is_absolute())); + let user_home = crate::home_dir().unwrap(); + assert_eq!( + super::config_dir(&[("COPILOT_HOME".into(), "~/.copilot-work".into())], Some(home)), + Some(user_home.join(".copilot-work")) + ); } #[test] diff --git a/crates/ide-agent/src/provider.rs b/crates/ide-agent/src/provider.rs index 58a531fe9b..f749613333 100644 --- a/crates/ide-agent/src/provider.rs +++ b/crates/ide-agent/src/provider.rs @@ -194,6 +194,9 @@ impl ProviderSpec { } fn resolve_for_launch_base(mut self, cwd: &Path, strip_ambient_tokens: bool) -> Self { + for (_, value) in &mut self.env { + *value = expand_home(value); + } if let Some(program) = self.managed_cli.take() { self.command = program; } else if self.driver != Driver::Acp { @@ -235,7 +238,7 @@ impl ProviderSpec { return Some(LoginLaunch { program, args: separate_args.iter().map(|s| (*s).to_owned()).collect(), - env: self.env.iter().cloned().collect(), + env: self.env.iter().map(|(name, value)| (name.clone(), expand_home(value))).collect(), }); } @@ -1201,6 +1204,21 @@ pub(crate) fn load(custom_dir: Option<&Path>, secrets: Option<&dyn EnvSecrets>) specs } +/// `value` with a leading `~` (alone or before a path separator) replaced by the user's home, +/// as a shell does for `NAME=~/path`. +pub(crate) fn expand_home(value: &str) -> String { + let rest = match value.strip_prefix('~') { + Some("") => "", + Some(rest) if rest.starts_with('/') || rest.starts_with('\\') => &rest[1..], + _ => return value.to_owned(), + }; + match crate::home_dir() { + Some(home) if rest.is_empty() => home.to_string_lossy().into_owned(), + Some(home) => home.join(rest).to_string_lossy().into_owned(), + None => value.to_owned(), + } +} + /// Whether `command` resolves to an executable (a path, or a name on [`ide_core::tool_path::search_path`]). #[cfg(test)] pub(crate) fn installed(command: &str) -> bool { @@ -1647,6 +1665,20 @@ mod tests { assert!(resolved.env.iter().all(|(name, _)| name != COPILOT_GITHUB_ACCOUNT_ENV)); } + #[test] + fn a_leading_tilde_in_the_environment_is_the_home_folder() { + let home = crate::home_dir().unwrap(); + let mut spec = builtin_spec("claude").unwrap(); + for (name, value) in [("A", "~/.claude-work"), ("B", "~"), ("C", "a~/b"), ("D", "~user/x"), ("E", "/abs")] { + spec.env.push((name.into(), value.into())); + } + let resolved = spec.resolve_for_launch(Path::new(".")); + let value = |name: &str| resolved.env.iter().find(|(n, _)| n == name).map(|(_, v)| v.clone()).unwrap(); + assert_eq!(value("A"), home.join(".claude-work").to_string_lossy()); + assert_eq!(value("B"), home.to_string_lossy()); + assert_eq!((value("C"), value("D"), value("E")), ("a~/b".into(), "~user/x".into(), "/abs".into())); + } + #[test] fn acp_adapters_of_native_agents_are_unlisted_until_customized() { let dir = tempfile::tempdir().unwrap();