repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

build(xtask): arch gate rejects Mutex in library crate production code

9dcdc04da04abb9db03dae285a2af48b427f653e

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

+56 / −0 text lines

Diff layout

xtask/src/main.rs

Modified

+56 / −0 text lines

587587 ));
588588 }
589589 }
590 if LIBRARY_CRATES.contains(&rule.crate_name)
591 && let Some(offender) = mutex_in_production(Path::new(rule.crate_name))
592 {
593 return fail(&format!(
594 "{}: Mutex in library code (concurrency law): {}\n",
595 rule.crate_name,
596 offender.display()
597 ));
598 }
590599 }
591600 ExitCode::SUCCESS
603/// Crates under the "no Mutex in library crates" law; binaries are exempt.
604const LIBRARY_CRATES: &[&str] = &[
605 "smith",
606 "smith-core",
607 "smith-ai",
608 "smith-ui",
609 "smith-tui",
610 "smith-rpc",
611 "smith-harness",
612 "smith-alloc",
613];
615/// The first `.rs` file under `crate_dir/src` whose production slice
616/// (everything before `#[cfg(test)]`) mentions `Mutex`.
617fn mutex_in_production(crate_dir: &Path) -> Option<PathBuf> {
618 let mut pending = vec![crate_dir.join("src")];
619 while let Some(dir) = pending.pop() {
620 let Ok(entries) = fs::read_dir(&dir) else {
621 continue;
622 };
623 for entry in entries.flatten() {
624 let path = entry.path();
625 if path.is_dir() {
626 pending.push(path);
627 } else if path.extension().is_some_and(|ext| ext == "rs")
628 && fs::read_to_string(&path)
629 .is_ok_and(|source| production_slice(&source).contains("Mutex"))
630 {
631 return Some(path);
632 }
633 }
634 }
635 None
636}
638/// Source text before the first `#[cfg(test)]`; tests may lock freely.
639fn production_slice(source: &str) -> &str {
640 source.split("#[cfg(test)]").next().unwrap_or(source)
641}
594643/// Library edges a crate may have; every other workspace crate is denied.
595644struct DependencyRule {
596645 crate_name: &'static str,
663712#[cfg(test)]
664713mod tests {
714 #[test]
715 fn production_slice_stops_at_the_test_module() {
716 let source = "use std::sync::Arc;\n#[cfg(test)]\nmod tests { use std::sync::Mutex; }";
717 assert!(!super::production_slice(source).contains("Mutex"));
718 assert!(super::production_slice("static M: Mutex<u8>;").contains("Mutex"));
719 }
665721 use super::{CRATES, DEPENDENCY_RULES, binary_name, dev_home_dirs, has_dependency, target_dir};
666722 use std::path::PathBuf;