[Detail Bug] config_path() ignores XDG_CONFIG_HOME on non-Windows; hardcodes ~/.config
Detail Bug Report
https://app.detail.dev/org_89d327b3-b883-4365-b6a3-46b6701342a9/bugs/bug_8be78b8c-2b80-40c9-b30d-da23dc3a30c4
Summary
- Context: The
config_path()function determines where the CLI stores its configuration file on disk across different operating systems. - Bug: On non-Windows platforms (Linux, macOS, BSD, etc.), the function ignores the
XDG_CONFIG_HOMEenvironment variable and always uses$HOME/.configinstead. - Actual vs. expected: The function should respect the XDG Base Directory Specification by using
XDG_CONFIG_HOMEwhen set, but it currently ignores this environment variable and hardcodes.configrelative to the home directory. - Impact: Users who customize their configuration directory location via
XDG_CONFIG_HOMEwill have the s2 CLI ignore their preference, violating the XDG Base Directory Specification and causing configuration files to be placed in unexpected locations.
Code with bug
#[cfg(not(target_os = "windows"))]
pub fn config_path() -> Result<PathBuf, S2CliError> {
let mut path = dirs::home_dir().ok_or(S2ConfigError::DirNotFound)?; // <-- BUG 🔴 Should use config_dir()
path.push(".config"); // <-- BUG 🔴 Hardcodes .config, ignoring XDG_CONFIG_HOME
path.push("s2");
path.push("config.toml");
Ok(path)
}
Failing test
Test script
#[cfg(test)]
mod tests {
use super::*;
#[cfg(not(target_os = "windows"))]
#[test]
fn test_xdg_config_home_ignored() {
// Save original values
let original_xdg = std::env::var("XDG_CONFIG_HOME").ok();
let original_home = std::env::var("HOME").ok();
unsafe {
// Set up test environment
std::env::set_var("HOME", "/home/testuser");
std::env::set_var("XDG_CONFIG_HOME", "/custom/config/path");
}
// Get the config path using the actual function
let config_path = config_path().expect("config_path should succeed");
// Also test what dirs::config_dir() returns
let dirs_config = dirs::config_dir().expect("dirs::config_dir should succeed");
println!("Current config_path() returns: {:?}", config_path);
println!("dirs::config_dir() returns: {:?}", dirs_config);
// What the path SHOULD be (respecting XDG_CONFIG_HOME)
let expected_path = PathBuf::from("/custom/config/path/s2/config.toml");
// Restore original values
unsafe {
if let Some(val) = original_xdg {
std::env::set_var("XDG_CONFIG_HOME", val);
} else {
std::env::remove_var("XDG_CONFIG_HOME");
}
if let Some(val) = original_home {
std::env::set_var("HOME", val);
} else {
std::env::remove_var("HOME");
}
}
// This assertion will fail, demonstrating the bug
assert_eq!(
config_path, expected_path,
"BUG: XDG_CONFIG_HOME is not respected! Got {:?}, expected {:?}",
config_path, expected_path
);
}
}
Test output
running 1 test
Current config_path() returns: "/home/testuser/.config/s2/config.toml"
dirs::config_dir() returns: "/custom/config/path"
thread 'config::tests::test_xdg_config_home_ignored' (15870) panicked at src/config.rs:146:9:
assertion `left == right` failed: BUG: XDG_CONFIG_HOME is not respected! Got "/home/testuser/.config/s2/config.toml", expected "/custom/config/path/s2/config.toml"
left: "/home/testuser/.config/s2/config.toml"
right: "/custom/config/path/s2/config.toml"
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test config::tests::test_xdg_config_home_ignored ... FAILED
Recommended fix
Replace the non-Windows implementation to use dirs::config_dir() consistently:
#[cfg(not(target_os = "windows"))]
pub fn config_path() -> Result<PathBuf, S2CliError> {
let mut path = dirs::config_dir().ok_or(S2ConfigError::DirNotFound)?; // <-- FIX 🟢
path.push("s2");
path.push("config.toml");
Ok(path)
}
This makes the non-Windows implementation identical to the Windows implementation, ensuring consistent behavior across all platforms and proper respect for the XDG Base Directory Specification.
Migration consideration: This fix will change the default config location on macOS from $HOME/.config/s2/config.toml to $HOME/Library/Application Support/s2/config.toml, which is the platform-appropriate location on macOS. Users who have existing configurations may need to migrate their config files, or the CLI could implement a fallback mechanism to check the old location if the new location doesn't exist.
was addressed in https://github.com/s2-streamstore/s2-cli/pull/183, but we later decided to stick to ~/.config, so reverted back in https://github.com/s2-streamstore/s2-cli/pull/188.