Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/korp-collectors

tests/rayon_free_law.rs

//! **ROOT LAW #0 at the DEPENDENCY GRAPH, not in the source text.**
//!
//! Concurrency in this constellation is `gatling`; rayon is forbidden. Every `rayon`
//! string under `src/` here is a comment saying so, and every fan-out goes through
//! `rotaryengine::gatling_forkjoin` — the source has always been innocent. The graph
//! was not.
//!
//! Measured on oden 2026-08-03 at korp-collectors `1971f91` (stinky `korpdep-1`):
//! `cargo tree -i rayon --edges normal --target all --all-features` printed **two
//! independent normal edges** —
//!
//! ```text
//!   korp-collectors → skade 0.5.2 → znippy-zoomies 0.1.15 → rayon 1.12.0
//!   korp-collectors → znippy-common 0.9.12 → sysinfo 0.39.6 → rayon 1.12.0
//! ```
//!
//! The first came in because the `znippy-zoomies` that `skade` resolves out of
//! crates.io — 0.1.15 — declares `rayon = "1"` unconditionally, while the local
//! checkout is 0.1.21 and rayon-free. korp itself was clean under `--features
//! collectors` for one reason only: korp's own `[patch.crates-io]` unified every
//! znippy-zoomies edge onto the local copy, and this crate's table did not. So the
//! identical code linked rayon when built as its own root and did not when built
//! inside korp.
//!
//! Both edges are gone. Re-measured on oden 2026-08-04 at `413b225`: the graph prints
//! "nothing to print" under `--all-features` and under each of `skade-sink` /
//! `znippy` / `osm` / `daemon` alone. The `[patch.crates-io]` entry in
//! `Cargo.toml:209-221` names itself as the cure, and `Cargo.lock` now pins
//! znippy-zoomies 0.1.21 — with that lock in place, removing the patch does NOT bring
//! rayon back, so which of the two is load-bearing on a FRESH resolve is not something
//! I established. Which is a reason to keep this file, not to skip it.
//!
//! **Nothing was watching.** That is what this file is for. A source grep — the shape
//! the constellation's other rayon guards take — could not have seen either edge:
//! there was no rayon in any source file, in any of the crates involved, at any point.
//! Only the resolved graph knew.
//!
//! ## The guard's own guard
//!
//! Shelling out to another program is the easiest way to build a check that can never
//! fail: a malformed invocation prints nothing on stdout, and "nothing" is exactly
//! what a clean graph looks like. So [`the_probe_can_see_an_edge_that_is_there`] runs
//! the SAME command shape with a needle that is definitely in the graph and asserts it
//! comes back non-empty. If the invocation is wrong, that test fails and the law above
//! is not believed.

use std::process::Command;

/// The crate whose absence from the graph is the law.
const FORBIDDEN: &str = "rayon";

/// A package that is unquestionably in this crate's graph, whatever the features —
/// the control needle for [`the_probe_can_see_an_edge_that_is_there`]. `serde` is a
/// direct dependency in `Cargo.toml`.
const CONTROL: &str = "serde";

/// What one `cargo tree -i <pkg>` run said.
struct Tree {
    /// Did cargo itself run to completion?
    ok: bool,
    stdout: String,
    stderr: String,
}

impl Tree {
    /// Did cargo report the needle as absent from the graph? `cargo tree -i` prints
    /// `warning: nothing to print.` on stderr and nothing on stdout for a package
    /// that is not in the graph.
    fn is_absent(&self) -> bool {
        self.stdout.trim().is_empty() && self.stderr.contains("nothing to print")
    }
}

/// Run `cargo tree -i <needle> --edges normal --target all <extra…>` in this crate.
///
/// `--edges normal` on purpose: a dev-dependency edge is a build-time tool and not
/// something this crate ships, and the law is about what is linked into the artefact.
/// `--target all` on purpose too — a `[target.'cfg(...)'.dependencies]` rayon would
/// otherwise hide from a host-only walk.
fn tree(needle: &str, extra: &[&str]) -> Tree {
    let manifest = concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.toml");
    let mut cmd = Command::new(std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()));
    cmd.args([
        "tree",
        "--manifest-path",
        manifest,
        "-i",
        needle,
        "--edges",
        "normal",
        "--target",
        "all",
    ]);
    cmd.args(extra);
    let out = cmd.output().unwrap_or_else(|e| {
        panic!(
            "could not run `cargo tree` — this guard reads the resolved dependency \
             graph and cannot fall back to a source grep, because a source grep is \
             exactly what missed the edges it exists for: {e}"
        )
    });
    Tree {
        ok: out.status.success(),
        stdout: String::from_utf8_lossy(&out.stdout).into_owned(),
        stderr: String::from_utf8_lossy(&out.stderr).into_owned(),
    }
}

/// The feature sets the law is checked over. `--all-features` is the union, and each
/// of the four that pull an outside tree is checked alone as well, so a report can
/// name WHICH feature carries an edge instead of only that one exists.
const FEATURE_ARMS: &[(&str, &[&str])] = &[
    ("--all-features", &["--all-features"]),
    (
        "skade-sink",
        &["--no-default-features", "--features", "skade-sink"],
    ),
    ("znippy", &["--no-default-features", "--features", "znippy"]),
    ("osm", &["--no-default-features", "--features", "osm"]),
    ("daemon", &["--no-default-features", "--features", "daemon"]),
];

/// **The law: no rayon on any normal edge, under any feature set.**
#[test]
fn no_rayon_on_any_shipped_edge() {
    let mut offenders = Vec::new();
    for (name, args) in FEATURE_ARMS {
        let t = tree(FORBIDDEN, args);
        if !t.ok {
            // A cargo that failed proves nothing either way, and silence from a failed
            // command is the shape of a clean result. Refuse to read it as one.
            panic!(
                "`cargo tree -i {FORBIDDEN}` failed for the `{name}` arm, so the law \
                 was not checked at all:\n{}",
                t.stderr
            );
        }
        if !t.is_absent() {
            offenders.push(format!("── {name} ──\n{}", t.stdout.trim_end()));
        }
    }
    assert!(
        offenders.is_empty(),
        "ROOT LAW #0: rayon is on {} shipped dependency edge(s) of korp-collectors.\n\n{}\n\n\
         The source is not the place to look — every `rayon` in src/ is a comment \
         forbidding it. Read the chain above and either patch the offending crate onto \
         a rayon-free version (see the `[patch.crates-io]` table in Cargo.toml) or drop \
         the feature.",
        offenders.len(),
        offenders.join("\n\n")
    );
}

/// **Anti-hollow: the probe can see an edge that IS there.**
///
/// This whole guard is one subprocess whose "pass" signal is empty output. A typo in
/// a flag, a renamed subcommand, a cargo that refuses the manifest — every one of
/// those produces exactly the output a clean graph produces. So run the same command
/// shape against a needle that must be in the graph and require a real answer.
///
/// It also asserts the discriminator directly: the control must NOT look absent, and
/// the same helper must call the control present and (in the law above) rayon absent
/// — one function, two verdicts, so it cannot be stuck on either.
#[test]
fn the_probe_can_see_an_edge_that_is_there() {
    let t = tree(CONTROL, &["--all-features"]);
    assert!(t.ok, "`cargo tree -i {CONTROL}` did not run:\n{}", t.stderr);
    assert!(
        !t.is_absent(),
        "the control needle `{CONTROL}` came back ABSENT from this crate's own graph — \
         the invocation is wrong, and every 'no rayon' verdict from it is meaningless.\n\
         stdout: {:?}\nstderr: {:?}",
        t.stdout,
        t.stderr
    );
    assert!(
        t.stdout.contains("korp-collectors"),
        "the inverted tree must reach this crate, got:\n{}",
        t.stdout
    );
}