use std::collections::BTreeSet;
use crate::error::Result;
use crate::resolve::narrow;
use crate::types::{Label, Purpose, Scope, SourcePath, TargetGraph, TargetRole};
pub(crate) fn watched_inputs(
whole: &TargetGraph,
scoped: &TargetGraph,
) -> Result<BTreeSet<SourcePath>> {
let mut inputs = inputs_of(scoped);
let discovery: BTreeSet<Label> = whole
.nodes()
.iter()
.filter(|node| node.role == TargetRole::Discovery)
.map(|node| node.label.clone())
.collect();
if !discovery.is_empty() {
let discovered = narrow(whole, &Scope::Targets(discovery), Purpose::Build)?;
inputs.extend(inputs_of(&discovered));
}
Ok(inputs)
}
fn inputs_of(graph: &TargetGraph) -> BTreeSet<SourcePath> {
graph
.nodes()
.iter()
.flat_map(|node| node.inputs.iter().cloned())
.collect()
}
#[cfg(test)]
mod tests {
#![expect(
clippy::unwrap_used,
reason = "tests unwrap known-valid fixtures; a panic is the intended failure signal"
)]
use std::collections::BTreeSet;
use super::watched_inputs;
use crate::grants::fixtures::{graph, label, node, path};
use crate::resolve::narrow;
use crate::types::{Node, Purpose, Scope, SourcePath, TargetGraph, TargetRole};
fn reads(files: &[&str]) -> impl FnOnce(&mut Node) {
let files: Vec<SourcePath> = files.iter().map(|file| path(file)).collect();
move |node| node.inputs = files
}
fn paths(files: &[&str]) -> BTreeSet<SourcePath> {
files.iter().map(|file| path(file)).collect()
}
fn workspace() -> TargetGraph {
graph(
vec![
node("//app:app", reads(&["app/main.c"])),
node("//lib:lib", reads(&["lib/lib.c"])),
node("//other:other", reads(&["other/other.c"])),
],
&[&[1], &[], &[]],
)
}
fn scoped(whole: &TargetGraph, target: &str) -> TargetGraph {
let scope = Scope::Targets(BTreeSet::from([label(target)]));
narrow(whole, &scope, Purpose::Build).unwrap().into_owned()
}
#[test]
fn the_scoped_targets_and_what_they_need_give_their_inputs() {
let whole = workspace();
let scoped = scoped(&whole, "//app:app");
assert_eq!(
watched_inputs(&whole, &scoped).unwrap(),
paths(&["app/main.c", "lib/lib.c"])
);
}
#[test]
fn a_target_outside_the_scope_gives_nothing() {
let whole = workspace();
let scoped = scoped(&whole, "//app:app");
let inputs = watched_inputs(&whole, &scoped).unwrap();
assert!(!inputs.is_empty());
assert!(!inputs.contains(&path("other/other.c")));
}
#[test]
fn every_discovery_target_gives_its_inputs_whatever_the_scope() {
let whole = graph(
vec![
node("//app:app", reads(&["app/main.c"])),
node("//find:find", |node| {
node.role = TargetRole::Discovery;
node.inputs = vec![path("find/list.txt")];
}),
node("//lib:lib", reads(&["lib/lib.c"])),
node("//other:other", reads(&["other/other.c"])),
],
&[&[], &[2], &[], &[]],
);
let scoped = scoped(&whole, "//app:app");
assert_eq!(
watched_inputs(&whole, &scoped).unwrap(),
paths(&["app/main.c", "find/list.txt", "lib/lib.c"])
);
}
#[test]
fn no_discovery_target_adds_nothing() {
let whole = workspace();
let scoped = scoped(&whole, "//lib:lib");
assert_eq!(
watched_inputs(&whole, &scoped).unwrap(),
paths(&["lib/lib.c"])
);
}
}