use std::collections::HashSet;
pub fn looks_like_a_transient_windows_file_lock(message: &str) -> bool {
(message.contains("failed to remove file") || message.contains("failed to move"))
&& (message.contains("os error 5") || message.contains("Access is denied"))
}
pub fn other_running_xr_pids() -> Vec<u32> {
exclude_self(all_pids_for_this_executable(), std::process::id())
}
fn exclude_self(mut pids: HashSet<u32>, self_pid: u32) -> Vec<u32> {
pids.remove(&self_pid);
let mut pids: Vec<u32> = pids.into_iter().collect();
pids.sort_unstable();
pids
}
#[cfg(windows)]
fn all_pids_for_this_executable() -> HashSet<u32> {
let Ok(output) = std::process::Command::new("tasklist")
.args(["/FI", "IMAGENAME eq xr.exe", "/NH", "/FO", "CSV"])
.output()
else {
return HashSet::new();
};
parse_tasklist_csv_pids(&String::from_utf8_lossy(&output.stdout))
}
#[cfg(windows)]
fn parse_tasklist_csv_pids(output: &str) -> HashSet<u32> {
output
.lines()
.filter_map(|line| line.split(',').nth(1)?.trim_matches('"').parse().ok())
.collect()
}
#[cfg(unix)]
fn all_pids_for_this_executable() -> HashSet<u32> {
let Ok(output) = std::process::Command::new("pgrep")
.args(["-x", "xr"])
.output()
else {
return HashSet::new();
};
parse_pgrep_pids(&String::from_utf8_lossy(&output.stdout))
}
#[cfg(unix)]
fn parse_pgrep_pids(output: &str) -> HashSet<u32> {
output
.lines()
.filter_map(|line| line.trim().parse().ok())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn looks_like_a_transient_windows_file_lock_matches_xr_devs_own_report() {
let message = "error: failed to remove file `E:\\app\\target\\debug\\larust-app.exe`\n\n\
Caused by:\n Access is denied. (os error 5)";
assert!(looks_like_a_transient_windows_file_lock(message));
}
#[test]
fn looks_like_a_transient_windows_file_lock_matches_xr_upgrades_own_report() {
let message = "error: failed to move \
`C:\\Users\\x\\.cargo\\bin\\cargo-installis30EJ\\xr.exe` to \
`C:\\Users\\x\\.cargo\\bin\\xr.exe`\n\nCaused by:\n Access is denied. \
(os error 5)";
assert!(looks_like_a_transient_windows_file_lock(message));
}
#[test]
fn looks_like_a_transient_windows_file_lock_rejects_an_ordinary_compile_error() {
let message = "error: expected one of `!` or `::`, found `is`";
assert!(!looks_like_a_transient_windows_file_lock(message));
}
#[test]
fn looks_like_a_transient_windows_file_lock_rejects_an_unrelated_access_denied() {
let message = "error: Access is denied. (os error 5) opening `.env`";
assert!(!looks_like_a_transient_windows_file_lock(message));
}
#[test]
fn exclude_self_removes_only_the_matching_pid() {
let pids = HashSet::from([100, 200, 300]);
assert_eq!(exclude_self(pids, 200), vec![100, 300]);
}
#[test]
fn exclude_self_returns_results_in_sorted_order() {
let pids = HashSet::from([300, 100, 200]);
assert_eq!(exclude_self(pids, 999), vec![100, 200, 300]);
}
#[test]
fn exclude_self_is_a_no_op_when_self_is_not_in_the_set() {
let pids = HashSet::from([100, 200]);
assert_eq!(exclude_self(pids, 999), vec![100, 200]);
}
#[test]
fn exclude_self_on_an_empty_set_stays_empty() {
assert_eq!(exclude_self(HashSet::new(), 1), Vec::<u32>::new());
}
#[cfg(windows)]
#[test]
fn parse_tasklist_csv_pids_extracts_the_pid_column() {
let output = "\"xr.exe\",\"12345\",\"Console\",\"1\",\"4,100 K\"\n\
\"xr.exe\",\"6789\",\"Console\",\"1\",\"3,900 K\"\n";
let pids = parse_tasklist_csv_pids(output);
assert_eq!(pids, HashSet::from([12345, 6789]));
}
#[cfg(windows)]
#[test]
fn parse_tasklist_csv_pids_ignores_a_no_matching_processes_line() {
let output = "INFO: No tasks are running which match the specified criteria.\r\n";
assert_eq!(parse_tasklist_csv_pids(output), HashSet::new());
}
#[cfg(unix)]
#[test]
fn parse_pgrep_pids_extracts_each_line_as_a_pid() {
assert_eq!(parse_pgrep_pids("123\n456\n"), HashSet::from([123, 456]));
}
#[cfg(unix)]
#[test]
fn parse_pgrep_pids_on_no_matches_stays_empty() {
assert_eq!(parse_pgrep_pids(""), HashSet::new());
}
}