From 77caebf31092fe471957f1dd86892aa24cf4634a Mon Sep 17 00:00:00 2001 From: DuProcess <273172371+DuProcess@users.noreply.github.com> Date: Thu, 16 Jul 2026 20:04:57 -0400 Subject: [PATCH] Expand Windows-style tilde paths --- src/config.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/config.rs b/src/config.rs index 713e4ca..b304c4a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -325,9 +325,17 @@ pub fn load_hosts_config(path: Option) -> Result { } pub fn expand_tilde(path: &str) -> PathBuf { - if let Some(rest) = path.strip_prefix("~/") + let rest = if path == "~" { + Some("") + } else { + path.strip_prefix("~/").or_else(|| path.strip_prefix("~\\")) + }; + if let Some(rest) = rest && let Some(home) = dirs::home_dir() { + if rest.is_empty() { + return home; + } return home.join(rest); } PathBuf::from(path) @@ -335,7 +343,8 @@ pub fn expand_tilde(path: &str) -> PathBuf { #[cfg(test)] mod tests { - use super::{ClientConfig, ServerConfig}; + use super::{ClientConfig, ServerConfig, expand_tilde}; + use std::path::PathBuf; #[test] fn old_client_configs_fill_new_fields_from_defaults() { @@ -360,6 +369,20 @@ dosh_port = 50000 assert!(config.send_env.iter().any(|value| value == "TERM")); } + #[test] + fn expand_tilde_handles_unix_and_windows_separators() { + let Some(home) = dirs::home_dir() else { + return; + }; + assert_eq!(expand_tilde("~"), home); + assert_eq!(expand_tilde("~/one/two"), home.join("one/two")); + assert_eq!(expand_tilde("~\\one\\two"), home.join("one\\two")); + assert_eq!( + expand_tilde("relative/path"), + PathBuf::from("relative/path") + ); + } + #[test] fn old_server_configs_fill_new_fields_from_defaults() { let raw = r#"