use std::path::Path;
const NODE_MODULES: &str = "node_modules";
const DELETED_SUFFIX: &str = " (deleted)";
const FALLOW_EXECUTABLES: &[&str] = &["fallow", "fallow-lsp", "fallow-mcp", "fallow-multicall"];
pub fn is_standalone_fallow_binary(current_exe: Option<&Path>) -> bool {
let Some(current_exe) = current_exe else {
return false;
};
let resolved = dunce::canonicalize(current_exe).unwrap_or_else(|_| current_exe.to_path_buf());
let is_fallow = resolved
.file_stem()
.and_then(|stem| stem.to_str())
.map(|stem| stem.strip_suffix(DELETED_SUFFIX).unwrap_or(stem))
.is_some_and(|stem| FALLOW_EXECUTABLES.contains(&stem));
is_fallow
&& !resolved
.components()
.any(|component| component.as_os_str() == NODE_MODULES)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[test]
fn a_binary_inside_node_modules_is_an_npm_install() {
let exe = PathBuf::from("/project/node_modules/@fallow-cli/linux-x64-gnu/fallow");
assert!(!is_standalone_fallow_binary(Some(&exe)));
}
#[test]
fn a_fallow_binary_outside_node_modules_is_standalone() {
for exe in [
"/opt/homebrew/Cellar/fallow/3.43.0/bin/fallow",
"/home/user/.cargo/bin/fallow-lsp",
"/home/user/node_modules_tools/fallow-mcp",
"C:/tools/fallow.exe",
] {
assert!(
is_standalone_fallow_binary(Some(&PathBuf::from(exe))),
"{exe}"
);
}
}
#[test]
fn a_host_process_that_loads_the_addon_keeps_the_npm_hint() {
for exe in ["/usr/local/bin/node", "/home/user/.bun/bin/bun"] {
assert!(
!is_standalone_fallow_binary(Some(&PathBuf::from(exe))),
"{exe}"
);
}
}
#[test]
fn an_unknown_binary_path_keeps_the_npm_hint() {
assert!(!is_standalone_fallow_binary(None));
}
#[test]
fn a_replaced_running_binary_is_still_standalone() {
let exe = PathBuf::from("/home/linuxbrew/.linuxbrew/bin/fallow (deleted)");
assert!(is_standalone_fallow_binary(Some(&exe)));
}
#[cfg(unix)]
#[test]
fn a_bin_link_into_an_npm_package_stays_an_npm_install() {
let root = tempfile::tempdir().expect("temporary directory");
let package = root.path().join("node_modules/@fallow-cli/linux-x64-gnu");
std::fs::create_dir_all(&package).expect("create package directory");
let real = package.join("fallow");
std::fs::write(&real, []).expect("write fallow fixture");
let links = root.path().join("links");
std::fs::create_dir_all(&links).expect("create link directory");
let link = links.join("fallow");
std::os::unix::fs::symlink(&real, &link).expect("link fallow");
assert!(!is_standalone_fallow_binary(Some(&link)));
}
#[cfg(unix)]
#[test]
fn a_link_to_a_cellar_binary_is_standalone() {
let root = tempfile::tempdir().expect("temporary directory");
let cellar = root.path().join("Cellar/fallow/3.43.0/bin");
std::fs::create_dir_all(&cellar).expect("create Cellar directory");
let real = cellar.join("fallow");
std::fs::write(&real, []).expect("write fallow fixture");
let bin = root.path().join("node_modules/bin");
std::fs::create_dir_all(&bin).expect("create link directory");
let link = bin.join("fallow");
std::os::unix::fs::symlink(&real, &link).expect("link fallow");
assert!(is_standalone_fallow_binary(Some(&link)));
}
}