agentmux_common/
checkout.rs1pub const COMPILED_IN: &str = env!("CARGO_MANIFEST_DIR");
27
28use std::path::Path;
29
30pub fn check(compiled: &str) -> Result<(), String> {
35 match std::env::var("CARGO_MANIFEST_DIR") {
36 Ok(running) if !running.is_empty() => compare(Path::new(compiled), Path::new(&running)),
37 _ => Ok(()),
38 }
39}
40
41pub fn check_linked(compiled: &str, linked: &[&str]) -> Result<(), String> {
45 let root = |dir: &str| Path::new(dir).parent().and_then(Path::parent).map(Path::to_path_buf);
46 let Some(mine) = root(compiled) else { return Ok(()) };
47 for lib in linked {
48 let Some(theirs) = root(lib) else { continue };
49 if !same_folder(&mine, &theirs) {
50 return Err(format!(
51 "this test binary was built from {compiled} but links a workspace library compiled from {lib}: \
52 checkouts sharing a cargo target directory reuse each other's libraries, so the tests would run \
53 the other checkout's code. Give each checkout its own target directory (unset CARGO_TARGET_DIR, \
54 or point it at a folder of this checkout's own), then build again."
55 ));
56 }
57 }
58 Ok(())
59}
60
61#[doc(hidden)]
65pub fn guard(compiled: &str, linked: &[&str]) {
66 if std::env::var_os("CARGO_MANIFEST_DIR").is_none() {
67 return;
68 }
69 if let Err(e) = check(compiled).and_then(|()| check_linked(compiled, linked)) {
70 eprintln!("error: {e}");
71 std::process::exit(101);
72 }
73}
74
75#[macro_export]
82macro_rules! checkout_guard {
83 ($($lib:expr),* $(,)?) => {
84 const _: () = {
85 extern "C" fn agentmux_checkout_guard() {
86 $crate::checkout::guard(env!("CARGO_MANIFEST_DIR"), &[$crate::checkout::COMPILED_IN, $($lib),*]);
87 }
88 #[used]
89 #[cfg_attr(any(target_os = "linux", target_os = "android", target_os = "freebsd"), link_section = ".init_array")]
90 #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
91 #[cfg_attr(target_vendor = "apple", link_section = "__DATA,__mod_init_func")]
92 static AGENTMUX_CHECKOUT_GUARD: extern "C" fn() = agentmux_checkout_guard;
93 };
94 };
95}
96
97fn compare(compiled: &Path, running: &Path) -> Result<(), String> {
98 if same_folder(compiled, running) {
99 return Ok(());
100 }
101 Err(format!(
102 "this test binary was compiled from {} but cargo is running it for {}: the two checkouts share a \
103 cargo target directory, so their builds overwrite each other and tests can run the other checkout's \
104 code. Give each checkout its own target directory (unset CARGO_TARGET_DIR, or point it at a folder \
105 of this checkout's own), then build again.",
106 compiled.display(),
107 running.display()
108 ))
109}
110
111fn same_folder(a: &Path, b: &Path) -> bool {
115 if let (Ok(ca), Ok(cb)) = (a.canonicalize(), b.canonicalize()) {
116 if ca == cb {
117 return true;
118 }
119 }
120 same_file::is_same_file(a.join("Cargo.toml"), b.join("Cargo.toml")).unwrap_or(false)
121}
122
123#[cfg(test)]
124mod tests {
125 use super::*;
126
127 #[test]
131 fn cargo_says_which_checkout_runs_the_test() {
132 if std::env::var_os("CARGO").is_some() {
133 assert!(std::env::var_os("CARGO_MANIFEST_DIR").is_some(), "cargo ran this test without CARGO_MANIFEST_DIR");
134 }
135 }
136
137 fn checkout(root: &Path) {
138 std::fs::create_dir_all(root).unwrap();
139 std::fs::write(root.join("Cargo.toml"), "[package]\n").unwrap();
140 }
141
142 #[test]
143 fn another_checkouts_binary_is_refused_and_says_what_to_do() {
144 let base = std::env::temp_dir().join(format!("agentmux-checkout-test-{}", std::process::id()));
145 let (a, b) = (base.join("clone-a"), base.join("clone-b"));
146 checkout(&a);
147 checkout(&b);
148 let err = compare(&a, &b).unwrap_err();
149 assert!(err.contains("share a cargo target directory"), "{err}");
150 assert!(err.contains("CARGO_TARGET_DIR"), "{err}");
151 let stamp = std::fs::metadata(a.join("Cargo.toml")).unwrap().modified().unwrap();
153 std::fs::File::options().write(true).open(b.join("Cargo.toml")).unwrap().set_modified(stamp).unwrap();
154 assert!(compare(&a, &b).is_err(), "look-alike copies aren't the same checkout");
155 assert!(compare(&a, &a.join("..").join("clone-a")).is_ok());
157 let _ = std::fs::remove_dir_all(&base);
158 }
159
160 #[test]
163 fn a_library_from_another_checkout_is_refused() {
164 let base = std::env::temp_dir().join(format!("agentmux-linked-test-{}", std::process::id()));
165 for clone in ["clone-a", "clone-b"] {
166 checkout(&base.join(clone));
167 std::fs::create_dir_all(base.join(clone).join("crates").join("lib")).unwrap();
168 std::fs::create_dir_all(base.join(clone).join("crates").join("app")).unwrap();
169 }
170 let dir = |clone: &str, krate: &str| base.join(clone).join("crates").join(krate).to_string_lossy().into_owned();
171 assert!(check_linked(&dir("clone-a", "app"), &[&dir("clone-a", "lib")]).is_ok());
172 let err = check_linked(&dir("clone-a", "app"), &[&dir("clone-a", "lib"), &dir("clone-b", "lib")]).unwrap_err();
173 assert!(err.contains("links a workspace library compiled from") && err.contains("clone-b"), "{err}");
174 let _ = std::fs::remove_dir_all(&base);
175 }
176}