use std::path::PathBuf;
pub fn get_workspace_target_dir() -> PathBuf {
const TARGET_DIR_VAR: &str = "CARGO_TARGET_DIR";
std::env::var(TARGET_DIR_VAR)
.map(PathBuf::from)
.unwrap_or_else(|_| {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let workspace_root = find_workspace_root_from(manifest_dir);
workspace_root.join("target")
})
}
pub fn find_workspace_root_from(start_path: &str) -> PathBuf {
PathBuf::from(start_path)
.ancestors()
.find(|p| {
p.join("Cargo.toml").exists() && {
let content = std::fs::read_to_string(p.join("Cargo.toml")).unwrap_or_default();
content.contains("[workspace]")
}
})
.expect("Could not find workspace root")
.to_path_buf()
}