Skip to main content

xtask/
delivery_isolation.rs

1//! The structural guard of Decision I of #3928 (back-half paydown 1B.1c): the
2//! event failure-isolation code has one home.
3//!
4//! ADR 0284: a subscriber's throw is logged and does not stop its siblings or
5//! reach the committed publisher. Both targets build that statement with
6//! `bynk-emit`'s `compose::events::isolated_delivery`. The duplicate came back
7//! once after #1327, as a hand-written copy in the Bundle root that "mirrored"
8//! the Durable Object's, so this counts the one string only that statement
9//! logs, in production Rust source: more than one occurrence means a second copy
10//! of the isolation code was written.
11
12use std::path::{Path, PathBuf};
13
14use crate::greenfield_status::{in_test_range, test_mod_ranges};
15use crate::module_lines::is_whole_test_file;
16
17/// The message the isolation statement logs. Spelled in two halves so this file
18/// is not itself an occurrence.
19pub fn marker() -> String {
20    ["EventsFanout delivery", " failed"].concat()
21}
22
23/// Every line of production Rust source under each crate's `src` (this crate
24/// aside, which only names the marker) that holds `needle`, outside
25/// `#[cfg(test)]` modules and whole-test files, as `path:line`.
26pub fn occurrences(root: &Path, needle: &str) -> Vec<String> {
27    let mut found = Vec::new();
28    let Ok(entries) = std::fs::read_dir(root) else {
29        return found;
30    };
31    let mut crates: Vec<PathBuf> = entries
32        .filter_map(Result::ok)
33        .map(|e| e.path())
34        .filter(|p| p.join("src").is_dir() && p.file_name().is_some_and(|n| n != "xtask"))
35        .collect();
36    crates.sort();
37    for krate in crates {
38        scan(&krate.join("src"), root, needle, &mut found);
39    }
40    found
41}
42
43fn scan(dir: &Path, root: &Path, needle: &str, found: &mut Vec<String>) {
44    let Ok(entries) = std::fs::read_dir(dir) else {
45        return;
46    };
47    let mut paths: Vec<PathBuf> = entries.filter_map(Result::ok).map(|e| e.path()).collect();
48    paths.sort();
49    for path in paths {
50        if path.is_dir() {
51            scan(&path, root, needle, found);
52        } else if path.extension().is_some_and(|e| e == "rs")
53            && let Ok(source) = std::fs::read_to_string(&path)
54        {
55            // Repo-relative, with `/` separators on every platform, so the
56            // reported location reads (and compares) the same on Windows.
57            let rel = path
58                .strip_prefix(root)
59                .unwrap_or(&path)
60                .components()
61                .map(|c| c.as_os_str().to_string_lossy())
62                .collect::<Vec<_>>()
63                .join("/");
64            // A whole-test file (`tests.rs`, `*_tests.rs`, anything under `tests/`) is not
65            // production: a split that moves a test module out to `foo/tests.rs` keeps
66            // its test-only copies out of the count, as it does for `module_lines`.
67            if rel
68                .split_once("/src/")
69                .is_some_and(|(_, in_src)| is_whole_test_file(in_src))
70            {
71                continue;
72            }
73            found.extend(
74                occurrences_in(&source, needle)
75                    .into_iter()
76                    .map(|line| format!("{rel}:{line}")),
77            );
78        }
79    }
80}
81
82/// The 1-based lines of `source` that hold `needle` outside `#[cfg(test)]` modules.
83pub fn occurrences_in(source: &str, needle: &str) -> Vec<usize> {
84    let lines: Vec<&str> = source.lines().collect();
85    let tests = test_mod_ranges(&lines);
86    lines
87        .iter()
88        .enumerate()
89        .filter(|(i, l)| l.contains(needle) && !in_test_range(*i, &tests))
90        .map(|(i, _)| i + 1)
91        .collect()
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    fn a_test_module_copy_is_not_an_occurrence_but_a_production_one_is() {
100        let needle = marker();
101        let source = format!(
102            "fn a() {{ log(\"{needle}\"); }}\n#[cfg(test)]\nmod tests {{\n    fn t() {{ log(\"{needle}\"); }}\n}}\nfn b() {{ log(\"{needle}\"); }}\n"
103        );
104        assert_eq!(occurrences_in(&source, &needle), [1, 6]);
105    }
106}