From de830a7acc35e949824af72773840d686b2ed589 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Sat, 7 Nov 2026 03:47:38 +0100 Subject: [PATCH] test(cli-rs): guard the shipped dependency graph with deps-allow.txt --- packages/cli-rs/crates/cli/tests/deps.rs | 117 +++++++++++++++ packages/cli-rs/deps-allow.txt | 177 +++++++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 packages/cli-rs/crates/cli/tests/deps.rs create mode 100644 packages/cli-rs/deps-allow.txt diff --git a/packages/cli-rs/crates/cli/tests/deps.rs b/packages/cli-rs/crates/cli/tests/deps.rs new file mode 100644 index 00000000000..81384ffe3d9 --- /dev/null +++ b/packages/cli-rs/crates/cli/tests/deps.rs @@ -0,0 +1,117 @@ +//! Keeps `deps-allow.txt` equal to the third-party crates `opencode-cli` ships with (O-58). + +use std::collections::{BTreeMap, BTreeSet, HashMap}; +use std::path::Path; +use std::process::Command; + +use serde_json::Value; + +const TARGET: &str = "x86_64-unknown-linux-gnu"; +const ROOT: &str = "opencode-cli"; + +#[test] +fn allow_list_matches_the_shipped_graph() { + let workspace = Path::new(env!("CARGO_MANIFEST_DIR")).join("../.."); + let graph = graph(&workspace); + let text = std::fs::read_to_string(workspace.join("deps-allow.txt")).expect("deps-allow.txt is readable"); + let listed = parse(&text); + + let added = graph.iter().filter(|(name, marks)| listed.get(*name) != Some(marks)).map(line).collect::>(); + let removed = listed.iter().filter(|(name, marks)| graph.get(*name) != Some(marks)).map(line).collect::>(); + assert!( + added.is_empty() && removed.is_empty(), + "deps-allow.txt does not match the shipped graph of {ROOT} for {TARGET}.\n\ + \nIn the graph, not in the file (a new crate, build script or proc macro):\n{}\n\ + \nIn the file, not in the graph (removed or changed):\n{}\n\ + \nA new third-party crate, build script or proc macro needs an orchestrator decision (O-58, an O- entry in \ + decisions.md) before its line goes into deps-allow.txt; otherwise remove the dependency. For a crate that left \ + the graph, delete its line.", + bullets(&added), + bullets(&removed), + ); +} + +/// Third-party crates reachable from `ROOT` over normal and build edges, with their marks. `cargo tree` gives the +/// crates with the features `cargo build -p opencode-cli` resolves (`cargo metadata` unifies features over the whole +/// workspace, dev-dependencies included, and so lists more); `cargo metadata` gives each crate's targets and +/// declared dependencies. +fn graph(workspace: &Path) -> BTreeMap> { + let manifest = workspace.join("Cargo.toml"); + let tree = cargo( + &manifest, + &["tree", "-p", ROOT, "-e", "normal,build", "--target", TARGET, "--prefix", "none", "--format", "{p}"], + ); + let metadata: Value = + serde_json::from_str(&cargo(&manifest, &["metadata", "--format-version", "1", "--filter-platform", TARGET])) + .expect("cargo metadata prints JSON"); + let packages = metadata["packages"] + .as_array() + .unwrap() + .iter() + .map(|p| ((p["name"].as_str().unwrap(), p["version"].as_str().unwrap()), p)) + .collect::>(); + + let shipped = tree + .lines() + .filter_map(|line| { + let mut words = line.split_whitespace(); + Some((words.next()?, words.next()?.strip_prefix('v')?)) + }) + .collect::>(); + let mut graph = BTreeMap::>::new(); + for package in shipped.iter().map(|key| packages[key]).filter(|p| !p["source"].is_null()) { + let kinds = package["targets"] + .as_array() + .unwrap() + .iter() + .flat_map(|t| t["kind"].as_array().unwrap()) + .collect::>(); + let cc = + package["dependencies"].as_array().unwrap().iter().any(|dep| dep["name"] == "cc" && dep["kind"] == "build"); + let marks = graph.entry(package["name"].as_str().unwrap().to_owned()).or_default(); + if kinds.iter().any(|kind| *kind == "custom-build") { + marks.insert("build"); + } + if kinds.iter().any(|kind| *kind == "proc-macro") { + marks.insert("proc-macro"); + } + if cc { + marks.insert("cc"); + } + } + graph +} + +/// Runs cargo offline against the lock file. Inside `cargo test` this takes no lock the test run holds. +fn cargo(manifest: &Path, args: &[&str]) -> String { + let output = Command::new(std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into())) + .args(args) + .args(["--locked", "--offline", "--manifest-path"]) + .arg(manifest) + .output() + .expect("cargo runs"); + assert!(output.status.success(), "cargo {} failed:\n{}", args[0], String::from_utf8_lossy(&output.stderr)); + String::from_utf8(output.stdout).expect("cargo prints UTF-8") +} + +fn parse(text: &str) -> BTreeMap> { + text.lines() + .map(|line| line.split('#').next().unwrap().trim()) + .filter(|line| !line.is_empty()) + .map(|line| { + let mut words = line.split_whitespace(); + (words.next().unwrap().to_owned(), words.collect()) + }) + .collect() +} + +fn line((name, marks): (&String, &BTreeSet<&str>)) -> String { + std::iter::once(name.as_str()).chain(marks.iter().copied()).collect::>().join(" ") +} + +fn bullets(lines: &[String]) -> String { + if lines.is_empty() { + return " (none)".to_owned(); + } + lines.iter().map(|line| format!(" {line}")).collect::>().join("\n") +} diff --git a/packages/cli-rs/deps-allow.txt b/packages/cli-rs/deps-allow.txt new file mode 100644 index 00000000000..1eec010a2f9 --- /dev/null +++ b/packages/cli-rs/deps-allow.txt @@ -0,0 +1,177 @@ +# Third-party crates in the shipped graph of opencode-cli for x86_64-unknown-linux-gnu (normal and build edges, +# not dev), one name per line (all versions of a crate share it), sorted. Marks: `build` (has a build script), `cc` +# (the build script compiles C with `cc`), `proc-macro`. crates/cli/tests/deps.rs fails unless this file equals the +# graph. +# +# Adding a line needs an orchestrator decision (O-58: an O- entry in decisions.md). A crate that leaves the graph +# leaves this file in the same commit. + +aho-corasick +anstream +anstyle +anstyle-parse +anstyle-query +anyhow build +atomic-waker +autocfg +base64 +bitflags +block-buffer +bytes +cc +cfg-if +chacha20 +chrono +clap +clap_builder +clap_complete +clap_derive proc-macro +clap_lex +colorchoice +const-oid +cpufeatures +crypto-common +data-encoding +digest +displaydoc proc-macro +equivalent +errno +find-msvc-tools +foldhash +form_urlencoded +futures-channel +futures-core +futures-sink +futures-task +futures-util +getrandom build +hashbrown +heck +html-escape +http +http-body +http-body-util +httparse build +hybrid-array +hyper +hyper-rustls +hyper-util +iana-time-zone +icu_collections +icu_locale_core +icu_normalizer +icu_normalizer_data build +icu_properties +icu_properties_data build +icu_provider +idna +idna_adapter +indexmap +inotify +inotify-sys build +ipnet +is_terminal_polyfill +itoa +libc build +libloading +linux-raw-sys +litemap +log +memchr +mio +notify +notify-types +num-traits build +once_cell +openssl-probe +percent-encoding +pin-project-lite +potential_utf +proc-macro2 build +quote build +rand +rand_core +regex +regex-automata +regex-syntax +reqwest +ring build cc +rustix build +rustls build +rustls-native-certs +rustls-pki-types +rustls-platform-verifier +rustls-webpki +same-file +serde build +serde_core build +serde_derive proc-macro +serde_json build +sha1 +sha2 +shlex +signal-hook-registry +similar +slab +smallvec +socket2 +stable_deref_trait +streaming-iterator +strsim +subtle +syn +sync_wrapper +synstructure +terminal_size +thiserror build +thiserror-impl proc-macro +tinystr +tokio +tokio-macros proc-macro +tokio-rustls +tokio-tungstenite +tokio-util +tower +tower-http +tower-layer +tower-service +tracing +tracing-core +tree-sitter build cc +tree-sitter-bash build cc +tree-sitter-css build cc +tree-sitter-diff build cc +tree-sitter-go build cc +tree-sitter-html build cc +tree-sitter-javascript build cc +tree-sitter-json build cc +tree-sitter-language build +tree-sitter-md build cc +tree-sitter-python build cc +tree-sitter-rust build cc +tree-sitter-typescript build cc +tree-sitter-yaml build cc +tree-sitter-zig build cc +try-lock +tungstenite +typenum +unicode-ident +unicode-segmentation +unicode-width +untrusted +url +utf8_iter +utf8parse +utf_types +walkdir +want +writeable +yoke +yoke-derive proc-macro +zerofrom +zerofrom-derive proc-macro +zeroize +zerotrie +zerovec +zerovec-derive proc-macro +zmij build