Skip to content

Commit 66e2e97

Browse files
fix(dirs): restore /log subdirectory in log path after appdirs migration (#565)
* fix(dirs): restore /log subdirectory in log path after appdirs migration PR #562 replaced `appdirs` with `dirs` for Windows ARM64 compatibility, but `dirs` has no `log_dir()` equivalent to `appdirs::user_log_dir()`. The migration used `dirs::cache_dir()` directly, dropping the /log component from log paths. This caused logs to be written to the wrong location: Before: ~/.cache/activitywatch/log/{module}/ After: ~/.cache/activitywatch/{module}/ (WRONG) Fix by adding a platform-specific `get_user_log_dir()` helper that replicates the old `appdirs::user_log_dir("activitywatch")` behavior: - Linux: ~/.cache/activitywatch/log/ - macOS: ~/Library/Logs/activitywatch/ - Windows: {LOCALAPPDATA}\activitywatch\Logs\ Also adds a regression test to prevent this from happening again. * style: run cargo fmt
1 parent c455da9 commit 66e2e97

1 file changed

Lines changed: 62 additions & 4 deletions

File tree

aw-server/src/dirs.rs

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,42 @@ pub fn get_cache_dir() -> Result<PathBuf, ()> {
6060

6161
#[cfg(not(target_os = "android"))]
6262
pub fn get_log_dir(module: &str) -> Result<PathBuf, ()> {
63-
let dir = dirs::cache_dir()
64-
.ok_or(())?
65-
.join("activitywatch")
66-
.join(module);
63+
let dir = get_user_log_dir()?.join(module);
6764
fs::create_dir_all(&dir).expect("Unable to create log dir");
6865
Ok(dir)
6966
}
7067

68+
/// Returns the platform-appropriate log directory for ActivityWatch.
69+
///
70+
/// Replicates the behavior of the old `appdirs::user_log_dir("activitywatch")`:
71+
/// - Linux: ~/.cache/activitywatch/log/
72+
/// - macOS: ~/Library/Logs/activitywatch/
73+
/// - Windows: {LOCALAPPDATA}\activitywatch\Logs\
74+
#[cfg(target_os = "linux")]
75+
fn get_user_log_dir() -> Result<PathBuf, ()> {
76+
Ok(dirs::cache_dir()
77+
.ok_or(())?
78+
.join("activitywatch")
79+
.join("log"))
80+
}
81+
82+
#[cfg(target_os = "macos")]
83+
fn get_user_log_dir() -> Result<PathBuf, ()> {
84+
Ok(dirs::home_dir()
85+
.ok_or(())?
86+
.join("Library")
87+
.join("Logs")
88+
.join("activitywatch"))
89+
}
90+
91+
#[cfg(target_os = "windows")]
92+
fn get_user_log_dir() -> Result<PathBuf, ()> {
93+
Ok(dirs::data_local_dir()
94+
.ok_or(())?
95+
.join("activitywatch")
96+
.join("Logs"))
97+
}
98+
7199
#[cfg(target_os = "android")]
72100
pub fn get_log_dir(module: &str) -> Result<PathBuf, ()> {
73101
panic!("not implemented on Android");
@@ -99,3 +127,33 @@ fn test_get_dirs() {
99127
db_path(true).unwrap();
100128
db_path(false).unwrap();
101129
}
130+
131+
#[test]
132+
#[cfg(not(target_os = "android"))]
133+
fn test_log_dir_has_log_component() {
134+
let log_dir = get_log_dir("aw-server-rust").unwrap();
135+
let path_str = log_dir.to_string_lossy();
136+
137+
// The log path must contain a log-specific subdirectory, not just the cache dir.
138+
// This guards against the regression from PR #562 where /log was dropped.
139+
#[cfg(target_os = "linux")]
140+
assert!(
141+
path_str.contains("activitywatch/log/"),
142+
"Linux log path should contain activitywatch/log/, got: {}",
143+
path_str
144+
);
145+
146+
#[cfg(target_os = "macos")]
147+
assert!(
148+
path_str.contains("Library/Logs/activitywatch"),
149+
"macOS log path should use Library/Logs, got: {}",
150+
path_str
151+
);
152+
153+
#[cfg(target_os = "windows")]
154+
assert!(
155+
path_str.contains("activitywatch\\Logs\\") || path_str.contains("activitywatch/Logs/"),
156+
"Windows log path should contain activitywatch/Logs, got: {}",
157+
path_str
158+
);
159+
}

0 commit comments

Comments
 (0)