| 1 | //! Filesystem path resolution helpers for config/cache/workspace locations. |
| 2 | //! |
| 3 | //! Pure path-building helpers extracted verbatim from `config.rs`. They depend |
| 4 | //! only on `std`, `codewhale-paths`, and `shellexpand` plus one another, so they |
| 5 | //! form a clean leaf. `config.rs` pulls them back in (`use paths::{...}`) for the |
| 6 | //! workspace-trust and config-loading logic that stays there, and re-exports |
| 7 | //! the `pub(crate)` entry point (`expand_path`) so |
| 8 | //! external `crate::config::` callers resolve unchanged (#3311). |
| 9 | //! |
| 10 | //! Visibility note: helpers that were file-private `fn` in `config.rs` are |
| 11 | //! `pub(crate)` here purely so the parent module can name them; none are |
| 12 | //! re-exported publicly, so the crate's external surface is unchanged. |
| 13 | |
| 14 | use std::path::{Path, PathBuf}; |
| 15 | |
| 16 | pub fn default_config_path() -> anyhow::Result<PathBuf> { |
| 17 | try_default_config_path() |
| 18 | } |
| 19 | |
| 20 | pub(crate) fn try_default_config_path() -> anyhow::Result<PathBuf> { |
| 21 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 22 | { |
| 23 | with_test_state_path(try_default_config_path_from_environment, || { |
| 24 | Ok(crate::test_support::unsealed_test_state_root() |
| 25 | .join(codewhale_config::CONFIG_FILE_NAME)) |
| 26 | }) |
| 27 | } |
| 28 | |
| 29 | #[cfg(not(any(test, all(feature = "test-thread-scoped-state", debug_assertions))))] |
| 30 | try_default_config_path_from_environment() |
| 31 | } |
| 32 | |
| 33 | fn try_default_config_path_from_environment() -> anyhow::Result<PathBuf> { |
| 34 | codewhale_config::resolve_config_path(None) |
| 35 | } |
| 36 | |
| 37 | /// Holding [`lock_test_env`] is not enough to read the process environment: |
| 38 | /// many tests take that lock only to serialize unrelated variables, and |
| 39 | /// trusting it routed them at a populated `~/.codewhale/config.toml` (#5355, |
| 40 | /// #5359). Settings already requires a sealed `EnvVarGuard`; config paths |
| 41 | /// must use the same gate. |
| 42 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 43 | fn with_test_state_path<T>( |
| 44 | from_environment: impl FnOnce() -> T, |
| 45 | isolated: impl FnOnce() -> T, |
| 46 | ) -> T { |
| 47 | let honor_guarded_environment = crate::test_support::guarded_environment_provides_state_paths(); |
| 48 | crate::test_support::with_test_env_lock(|| { |
| 49 | if honor_guarded_environment { |
| 50 | from_environment() |
| 51 | } else { |
| 52 | isolated() |
| 53 | } |
| 54 | }) |
| 55 | } |
| 56 | |
| 57 | pub(crate) fn codewhale_home_dir() -> Result<Option<PathBuf>, codewhale_paths::PathOverrideError> { |
| 58 | codewhale_paths::codewhale_home_override() |
| 59 | } |
| 60 | |
| 61 | /// The user-global config document: `$CODEWHALE_HOME/config.toml` when an |
| 62 | /// explicit home is set, otherwise `~/.codewhale/config.toml` (falling back to |
| 63 | /// the legacy `~/.deepseek/config.toml` only when that file already exists). |
| 64 | /// |
| 65 | /// Credential writes are rerouted here when the ambient config path resolves |
| 66 | /// to a workspace-scoped document (#5045, #5193); non-credential settings keep |
| 67 | /// the ambient scoping. |
| 68 | pub fn home_config_path() -> Option<PathBuf> { |
| 69 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 70 | { |
| 71 | with_test_state_path(home_config_path_from_environment, || { |
| 72 | Some( |
| 73 | crate::test_support::unsealed_test_state_root() |
| 74 | .join(codewhale_config::CONFIG_FILE_NAME), |
| 75 | ) |
| 76 | }) |
| 77 | } |
| 78 | |
| 79 | #[cfg(not(any(test, all(feature = "test-thread-scoped-state", debug_assertions))))] |
| 80 | home_config_path_from_environment() |
| 81 | } |
| 82 | |
| 83 | /// Compare the physical user-global document using the same parent path |
| 84 | /// normalization and file-symlink rejection as ConfigStore. |
| 85 | pub fn is_home_config_path(path: &Path) -> bool { |
| 86 | let Some(home) = home_config_path() else { |
| 87 | return false; |
| 88 | }; |
| 89 | match ( |
| 90 | codewhale_config::resolve_config_path(Some(home)), |
| 91 | codewhale_config::resolve_config_path(Some(path.to_path_buf())), |
| 92 | ) { |
| 93 | (Ok(home), Ok(path)) => home == path, |
| 94 | _ => false, |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | fn home_config_path_from_environment() -> Option<PathBuf> { |
| 99 | match codewhale_home_dir() { |
| 100 | Ok(Some(home)) => return Some(home.join(codewhale_config::CONFIG_FILE_NAME)), |
| 101 | Ok(None) => {} |
| 102 | Err(error) => { |
| 103 | tracing::error!( |
| 104 | error = %error, |
| 105 | "invalid Codewhale home override; refusing to substitute a different config path" |
| 106 | ); |
| 107 | return None; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | codewhale_paths::user_home().map(|home| { |
| 112 | let primary = home.join(".codewhale").join("config.toml"); |
| 113 | if primary.exists() { |
| 114 | return primary; |
| 115 | } |
| 116 | let legacy = home.join(".deepseek").join("config.toml"); |
| 117 | if legacy.exists() { |
| 118 | return legacy; |
| 119 | } |
| 120 | primary |
| 121 | }) |
| 122 | } |
| 123 | |
| 124 | pub(crate) fn workspace_config_key(workspace: &Path) -> String { |
| 125 | canonicalize_or_keep(workspace) |
| 126 | .to_string_lossy() |
| 127 | .into_owned() |
| 128 | } |
| 129 | |
| 130 | pub fn canonicalize_or_keep(path: &Path) -> PathBuf { |
| 131 | path.canonicalize().unwrap_or_else(|_| path.to_path_buf()) |
| 132 | } |
| 133 | |
| 134 | pub(crate) fn env_config_path() -> Result<Option<PathBuf>, codewhale_paths::PathOverrideError> { |
| 135 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 136 | { |
| 137 | with_test_state_path(env_config_path_unlocked, || Ok(None)) |
| 138 | } |
| 139 | #[cfg(not(any(test, all(feature = "test-thread-scoped-state", debug_assertions))))] |
| 140 | { |
| 141 | env_config_path_unlocked() |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | fn env_config_path_unlocked() -> Result<Option<PathBuf>, codewhale_paths::PathOverrideError> { |
| 146 | codewhale_paths::config_path_override() |
| 147 | } |
| 148 | |
| 149 | pub(crate) fn expand_pathbuf(path: PathBuf) -> PathBuf { |
| 150 | if let Some(raw) = path.to_str() { |
| 151 | return expand_path(raw); |
| 152 | } |
| 153 | path |
| 154 | } |
| 155 | |
| 156 | pub(crate) fn default_managed_config_path() -> Option<PathBuf> { |
| 157 | #[cfg(unix)] |
| 158 | { |
| 159 | Some(PathBuf::from("/etc/deepseek/managed_config.toml")) |
| 160 | } |
| 161 | #[cfg(not(unix))] |
| 162 | { |
| 163 | codewhale_paths::user_home().map(|home| { |
| 164 | let primary = home.join(".codewhale").join("managed_config.toml"); |
| 165 | if primary.exists() { |
| 166 | return primary; |
| 167 | } |
| 168 | home.join(".deepseek").join("managed_config.toml") |
| 169 | }) |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | pub(crate) fn default_requirements_path() -> Option<PathBuf> { |
| 174 | #[cfg(unix)] |
| 175 | { |
| 176 | Some(PathBuf::from("/etc/deepseek/requirements.toml")) |
| 177 | } |
| 178 | #[cfg(not(unix))] |
| 179 | { |
| 180 | codewhale_paths::user_home().map(|home| { |
| 181 | let primary = home.join(".codewhale").join("requirements.toml"); |
| 182 | if primary.exists() { |
| 183 | return primary; |
| 184 | } |
| 185 | home.join(".deepseek").join("requirements.toml") |
| 186 | }) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | pub fn expand_path(path: &str) -> PathBuf { |
| 191 | if let Some(stripped) = path.strip_prefix('~') |
| 192 | && (stripped.is_empty() || stripped.starts_with('/') || stripped.starts_with('\\')) |
| 193 | && let Some(mut home) = codewhale_paths::user_home() |
| 194 | { |
| 195 | let suffix = stripped.trim_start_matches(['/', '\\']); |
| 196 | if !suffix.is_empty() { |
| 197 | home.push(suffix); |
| 198 | } |
| 199 | return home; |
| 200 | } |
| 201 | |
| 202 | let expanded = shellexpand::tilde(path); |
| 203 | PathBuf::from(expanded.as_ref()) |
| 204 | } |
| 205 | |
| 206 | pub fn default_skills_dir() -> Option<PathBuf> { |
| 207 | default_user_state_path("skills") |
| 208 | } |
| 209 | |
| 210 | pub fn owned_skills_dir() -> Option<PathBuf> { |
| 211 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 212 | if !crate::test_support::guarded_environment_provides_state_paths() { |
| 213 | return default_skills_dir(); |
| 214 | } |
| 215 | codewhale_paths::codewhale_home() |
| 216 | .ok() |
| 217 | .flatten() |
| 218 | .map(|home| home.join("skills")) |
| 219 | } |
| 220 | |
| 221 | pub(crate) fn default_mcp_config_path() -> Option<PathBuf> { |
| 222 | default_user_state_path("mcp.json") |
| 223 | } |
| 224 | |
| 225 | pub(crate) fn default_memory_path() -> Option<PathBuf> { |
| 226 | default_user_state_path("memory.md") |
| 227 | } |
| 228 | |
| 229 | fn default_user_state_path(name: &str) -> Option<PathBuf> { |
| 230 | #[cfg(any(test, all(feature = "test-thread-scoped-state", debug_assertions)))] |
| 231 | { |
| 232 | with_test_state_path( |
| 233 | || default_user_state_path_from_environment(name), |
| 234 | || Some(crate::test_support::unsealed_test_state_root().join(name)), |
| 235 | ) |
| 236 | } |
| 237 | |
| 238 | #[cfg(not(any(test, all(feature = "test-thread-scoped-state", debug_assertions))))] |
| 239 | default_user_state_path_from_environment(name) |
| 240 | } |
| 241 | |
| 242 | fn default_user_state_path_from_environment(name: &str) -> Option<PathBuf> { |
| 243 | match codewhale_home_dir() { |
| 244 | Ok(Some(home)) => return Some(home.join(name)), |
| 245 | Ok(None) => {} |
| 246 | Err(error) => { |
| 247 | tracing::error!( |
| 248 | error = %error, |
| 249 | "invalid Codewhale home override; refusing to substitute a different state root" |
| 250 | ); |
| 251 | return None; |
| 252 | } |
| 253 | } |
| 254 | codewhale_paths::user_home().map(|home| { |
| 255 | let primary = home.join(".codewhale").join(name); |
| 256 | if primary.exists() { |
| 257 | return primary; |
| 258 | } |
| 259 | let legacy = home.join(".deepseek").join(name); |
| 260 | if legacy.exists() { |
| 261 | return legacy; |
| 262 | } |
| 263 | primary |
| 264 | }) |
| 265 | } |
| 266 | |
| 267 | /// Resolve the directory pasted images should land in. Prefers |
| 268 | /// `~/.codewhale/clipboard-images/` so the path is stable across worktrees and |
| 269 | /// matches the location described in user-facing docs; falls back to |
| 270 | /// `<workspace>/clipboard-images/` if the home dir is unavailable. |
| 271 | pub fn clipboard_images_dir(workspace: &Path) -> PathBuf { |
| 272 | let home = codewhale_paths::user_home(); |
| 273 | clipboard_images_dir_for_home(workspace, home.as_deref()) |
| 274 | } |
| 275 | |
| 276 | pub fn clipboard_images_dir_for_home(workspace: &Path, home: Option<&Path>) -> PathBuf { |
| 277 | if let Some(home) = home { |
| 278 | return home.join(".codewhale").join("clipboard-images"); |
| 279 | } |
| 280 | workspace.join("clipboard-images") |
| 281 | } |
| 282 |