use std::collections::HashMap;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::sync::RwLock;
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::catalog::{self, Artifact, CATALOG};
use crate::error::Result;
use crate::names::NameMatcher;
mod selection;
mod verdict;
pub use selection::ArtifactId;
pub use selection::Selection;
pub use verdict::{Finding, Provenance, SkipReason, TAGGED_ID, Verdict};
pub const CONFIG_FILE_NAME: &str = "voom.toml";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct DirFacts {
markers: u32,
readable: bool,
}
impl DirFacts {
const UNREADABLE: Self = Self {
markers: 0,
readable: false,
};
}
#[derive(Debug)]
pub struct Classifier {
artifacts: NameMatcher<ArtifactId>,
markers: NameMatcher<u8>,
selection: Selection,
nested_in_dependencies: Vec<(&'static str, ArtifactId)>,
facts: RwLock<HashMap<PathBuf, DirFacts>>,
probes: AtomicUsize,
}
impl Classifier {
pub fn new(selection: Selection) -> Result<Self> {
let mut artifact_patterns = Vec::new();
let mut marker_patterns = Vec::new();
let mut nested_in_dependencies = Vec::new();
for (index, ecosystem) in CATALOG.iter().enumerate() {
let Ok(ecosystem_index) = u8::try_from(index) else {
continue;
};
for marker in ecosystem.markers {
marker_patterns.push((*marker, ecosystem_index));
}
for (offset, artifact) in ecosystem.artifacts.iter().enumerate() {
let Ok(artifact_index) = u8::try_from(offset) else {
continue;
};
let last = artifact
.path
.trim_end_matches('/')
.rsplit('/')
.next()
.unwrap_or(artifact.path);
let id = ArtifactId {
ecosystem: ecosystem_index,
artifact: artifact_index,
};
artifact_patterns.push((last, id));
if artifact.depth() > 1
&& let Some(first) = artifact.segments().next()
&& let Some(dependency) = catalog::DEPENDENCY_DIRS.iter().find(|dir| **dir == first)
{
nested_in_dependencies.push((*dependency, id));
}
}
}
Ok(Self {
artifacts: NameMatcher::build(artifact_patterns)?,
markers: NameMatcher::build(marker_patterns)?,
selection,
nested_in_dependencies,
facts: RwLock::new(HashMap::new()),
probes: AtomicUsize::new(0),
})
}
#[must_use]
pub fn probe_count(&self) -> usize {
self.probes.load(Ordering::Relaxed)
}
#[must_use]
pub fn selection(&self) -> &Selection {
&self.selection
}
#[must_use]
pub fn artifacts_inside(&self, name: &OsStr) -> Vec<ArtifactId> {
self.nested_in_dependencies
.iter()
.filter(|(dir, _)| name == OsStr::new(*dir))
.map(|(_, id)| *id)
.collect()
}
pub fn classify(&self, root: &Path, path: &Path, is_dir: bool) -> Verdict {
let Some(name) = path.file_name() else {
return Verdict::NotACandidate;
};
let mut candidates: Vec<ArtifactId> = Vec::new();
self.artifacts.for_each_match(name, |id| candidates.push(id));
if candidates.is_empty() {
return Verdict::NotACandidate;
}
candidates.sort_unstable();
let mut best: Option<(u8, SkipReason)> = None;
let mut record = |rank: u8, reason: SkipReason| {
if best.as_ref().is_none_or(|(existing, _)| rank < *existing) {
best = Some((rank, reason));
}
};
for id in candidates {
let artifact = id.artifact();
if artifact.requires_dir() && !is_dir {
continue;
}
let Some(anchor_dir) = anchor_dir(path, artifact) else {
continue;
};
if !anchor_dir.starts_with(root) {
continue;
}
if !self.selection.ecosystem_enabled(id) {
record(
RANK_ECOSYSTEM_OFF,
SkipReason::NotEnabled {
spec: id.spec(),
note: artifact.note,
},
);
continue;
}
match weigh(
id,
self.prove(root, path, anchor_dir, id),
self.selection.is_enabled(id),
) {
Weighed::Proven(marker_dir) => {
return Verdict::Artifact(Finding {
path: path.to_path_buf(),
provenance: Provenance::Anchored {
artifact: id,
marker_dir,
},
});
}
Weighed::Skipped(rank, reason) => record(rank, reason),
}
}
best.map_or(Verdict::NotACandidate, |(_, reason)| Verdict::Skip(reason))
}
fn prove(&self, root: &Path, candidate: &Path, anchor_dir: &Path, id: ArtifactId) -> Proof {
let bit = 1_u32 << id.ecosystem;
let anchor = id.artifact().anchor_in(id.ecosystem());
if anchor.is_inside() {
let facts = self.facts(candidate);
return if facts.markers & bit != 0 {
Proof::Found(candidate.to_path_buf())
} else if facts.readable {
Proof::Missing
} else {
Proof::Unreadable
};
}
if anchor.is_workspace_root() {
return self.prove_workspace_root(root, anchor_dir, bit);
}
let levels = anchor.levels();
let mut dir = anchor_dir;
let mut unreadable = false;
let mut exhausted_tree = false;
for _ in 0..=levels {
let facts = self.facts(dir);
if facts.markers & bit != 0 {
return Proof::Found(dir.to_path_buf());
}
unreadable |= !facts.readable;
if dir == root {
exhausted_tree = true;
break;
}
let Some(parent) = dir.parent() else {
exhausted_tree = true;
break;
};
dir = parent;
}
if unreadable {
Proof::Unreadable
} else if exhausted_tree || levels == 0 {
Proof::Missing
} else {
Proof::BoundReached(levels)
}
}
fn prove_workspace_root(&self, root: &Path, anchor_dir: &Path, bit: u32) -> Proof {
let mut dir = anchor_dir;
let mut unreadable = false;
loop {
let facts = self.facts(dir);
if facts.markers & bit != 0 {
return Proof::Found(dir.to_path_buf());
}
unreadable |= !facts.readable;
if dir == root || !dir.starts_with(root) {
break;
}
let Some(parent) = dir.parent() else { break };
dir = parent;
}
if unreadable { Proof::Unreadable } else { Proof::Missing }
}
fn facts(&self, dir: &Path) -> DirFacts {
if let Ok(cache) = self.facts.read()
&& let Some(facts) = cache.get(dir)
{
return *facts;
}
let facts = self.probe(dir);
if let Ok(mut cache) = self.facts.write() {
cache.insert(dir.to_path_buf(), facts);
}
facts
}
fn probe(&self, dir: &Path) -> DirFacts {
self.probes.fetch_add(1, Ordering::Relaxed);
let Ok(entries) = std::fs::read_dir(dir) else {
return DirFacts::UNREADABLE;
};
let mut facts = DirFacts {
markers: 0,
readable: true,
};
for entry in entries.flatten() {
self.markers
.for_each_match(&entry.file_name(), |index| facts.markers |= 1_u32 << index);
}
facts
}
}
const RANK_PROVEN_OPT_IN: u8 = 0;
const RANK_NO_MARKER: u8 = 1;
const RANK_UNPROVEN_OPT_IN: u8 = 2;
const RANK_ECOSYSTEM_OFF: u8 = 3;
const RANK_UNREADABLE: u8 = 4;
enum Proof {
Found(PathBuf),
Missing,
BoundReached(u8),
Unreadable,
}
enum Weighed {
Proven(PathBuf),
Skipped(u8, SkipReason),
}
fn weigh(id: ArtifactId, proof: Proof, enabled: bool) -> Weighed {
let artifact = id.artifact();
let not_enabled = || SkipReason::NotEnabled {
spec: id.spec(),
note: artifact.note,
};
match (proof, enabled) {
(Proof::Found(marker_dir), true) => Weighed::Proven(marker_dir),
(Proof::Found(_), false) => Weighed::Skipped(RANK_PROVEN_OPT_IN, not_enabled()),
(Proof::Missing, true) => Weighed::Skipped(RANK_NO_MARKER, unproven(id)),
(Proof::BoundReached(levels), true) => Weighed::Skipped(
RANK_NO_MARKER,
SkipReason::MarkerOutOfReach {
ecosystem: id.ecosystem().id,
markers: id.ecosystem().markers,
levels,
},
),
(Proof::BoundReached(_) | Proof::Missing, false) => Weighed::Skipped(RANK_UNPROVEN_OPT_IN, not_enabled()),
(Proof::Unreadable, _) => Weighed::Skipped(RANK_UNREADABLE, SkipReason::Unreadable),
}
}
fn unproven(id: ArtifactId) -> SkipReason {
let ecosystem = id.ecosystem().id;
let markers = id.ecosystem().markers;
if id.artifact().anchor_in(id.ecosystem()).is_inside() {
SkipReason::NoInnerMarker { ecosystem, markers }
} else {
SkipReason::NoMarker { ecosystem, markers }
}
}
fn anchor_dir<'p>(path: &'p Path, artifact: &Artifact) -> Option<&'p Path> {
let mut dir = path.parent()?;
for segment in artifact.path.trim_end_matches('/').rsplit('/').skip(1) {
if dir.file_name()?.as_encoded_bytes() != segment.as_bytes() {
return None;
}
dir = dir.parent()?;
}
Some(dir)
}
#[must_use]
pub fn is_dependency_dir(name: &OsStr) -> bool {
catalog::DEPENDENCY_DIRS.iter().any(|dir| name == OsStr::new(dir))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::testing::tree;
fn classifier() -> Classifier {
Classifier::new(Selection::all()).unwrap()
}
fn verdict(classifier: &Classifier, root: &Path, relative: &str) -> Verdict {
let path = root.join(relative);
let is_dir = path.is_dir();
classifier.classify(root, &path, is_dir)
}
fn assert_artifact(verdict: &Verdict, ecosystem: &str, artifact: &str) {
match verdict {
Verdict::Artifact(finding) => {
let id = finding.artifact().expect("a marker proved it");
assert_eq!(id.ecosystem().id, ecosystem);
assert_eq!(id.artifact().path, artifact);
}
other => panic!("expected an artifact verdict, got {other:?}"),
}
}
#[test]
fn should_classify_target_as_rust_when_cargo_toml_is_beside_it() {
let fixture = tree(&["Cargo.toml", "target/"]);
let classifier = classifier();
let verdict = verdict(&classifier, fixture.path(), "target");
assert_artifact(&verdict, "rust", "target/");
match verdict {
Verdict::Artifact(finding) => assert!(
matches!(finding.provenance, Provenance::Anchored { marker_dir, .. } if marker_dir == fixture.path())
),
other => panic!("expected an artifact verdict, got {other:?}"),
}
}
#[test]
fn should_not_classify_target_without_a_marker() {
let fixture = tree(&["target/", "target/important.txt"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "target") {
Verdict::Skip(SkipReason::NoMarker { ecosystem, .. }) => {
assert!(
["rust", "maven", "scala"].contains(&ecosystem),
"unexpected ecosystem {ecosystem}"
);
}
other => panic!("expected a no-marker skip, got {other:?}"),
}
}
#[test]
fn should_resolve_an_ambiguous_name_by_ecosystem_not_precedence() {
let rust = tree(&["Cargo.toml", "target/"]);
let maven = tree(&["pom.xml", "target/"]);
let classifier = classifier();
assert_artifact(&verdict(&classifier, rust.path(), "target"), "rust", "target/");
assert_artifact(&verdict(&classifier, maven.path(), "target"), "maven", "target/");
}
#[test]
fn should_report_a_missing_marker_rather_than_a_lesser_reason() {
let fixture = tree(&["bin/"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "bin") {
Verdict::Skip(SkipReason::NoMarker { ecosystem, .. }) => assert_eq!(ecosystem, "dotnet"),
other => panic!("expected a no-marker skip, got {other:?}"),
}
}
#[test]
fn should_prove_dotnet_obj_through_an_ancestor_marker() {
for depth in 0..=3 {
let nesting = "src/".repeat(depth);
let fixture = tree(&["App.csproj", &format!("{nesting}obj/")]);
let classifier = classifier();
let verdict = verdict(&classifier, fixture.path(), &format!("{nesting}obj"));
assert_artifact(&verdict, "dotnet", "obj/");
}
}
#[test]
fn should_not_prove_dotnet_obj_beyond_the_ancestor_bound() {
let fixture = tree(&["App.csproj", "a/b/c/d/obj/"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "a/b/c/d/obj") {
Verdict::Skip(SkipReason::MarkerOutOfReach { ecosystem, levels, .. }) => {
assert_eq!(ecosystem, "dotnet");
assert_eq!(levels, 3, "and it names the bound it stopped at");
}
other => panic!("expected an out-of-reach skip four levels down, got {other:?}"),
}
}
#[test]
fn should_report_a_plain_missing_marker_when_the_climb_reached_the_scan_root() {
let fixture = tree(&["a/obj/"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "a/obj") {
Verdict::Skip(SkipReason::NoMarker { ecosystem, .. }) => assert_eq!(ecosystem, "dotnet"),
other => panic!("the root was reached, so nothing above proves it: got {other:?}"),
}
}
#[test]
fn should_never_report_a_sibling_anchor_as_out_of_reach() {
let fixture = tree(&["a/b/target/"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "a/b/target") {
Verdict::Skip(SkipReason::NoMarker { .. }) => {}
other => panic!("expected a plain missing marker for a sibling anchor, got {other:?}"),
}
}
#[test]
fn should_not_climb_above_the_scan_root_for_a_marker() {
let fixture = tree(&["App.csproj", "project/nested/obj/"]);
let classifier = classifier();
let root = fixture.path().join("project");
let path = root.join("nested/obj");
match classifier.classify(&root, &path, true) {
Verdict::Skip(SkipReason::NoMarker { .. }) => {}
other => panic!("expected the climb to stop at the scan root, got {other:?}"),
}
}
#[test]
fn should_prove_pycache_one_level_below_the_project_root() {
let fixture = tree(&["pyproject.toml", "package/__pycache__/"]);
let classifier = classifier();
assert_artifact(
&verdict(&classifier, fixture.path(), "package/__pycache__"),
"python",
"__pycache__/",
);
}
#[test]
fn should_verify_leading_segments_of_a_multi_segment_artifact() {
let matching = tree(&["Gemfile", "vendor/bundle/"]);
let mut selection = Selection::all();
assert!(selection.set("ruby.vendor-bundle", true), "the spec exists");
let classifier = Classifier::new(selection).expect("the catalog compiles");
assert_artifact(
&verdict(&classifier, matching.path(), "vendor/bundle"),
"ruby",
"vendor/bundle/",
);
let elsewhere = tree(&["Gemfile", "lib/bundle/"]);
assert_eq!(
verdict(&classifier, elsewhere.path(), "lib/bundle"),
Verdict::NotACandidate
);
}
#[test]
fn should_require_a_directory_for_a_slash_terminated_artifact() {
let fixture = tree(&["Cargo.toml", "target"]);
let classifier = classifier();
assert_eq!(verdict(&classifier, fixture.path(), "target"), Verdict::NotACandidate);
}
#[test]
fn should_match_a_file_artifact_that_is_not_slash_terminated() {
let fixture = tree(&["package.json", "app.tsbuildinfo"]);
let classifier = classifier();
assert_artifact(
&verdict(&classifier, fixture.path(), "app.tsbuildinfo"),
"node",
"*.tsbuildinfo",
);
}
#[test]
fn should_match_a_prefix_glob_artifact() {
let fixture = tree(&["CMakeLists.txt", "cmake-build-debug/"]);
let classifier = classifier();
assert_artifact(
&verdict(&classifier, fixture.path(), "cmake-build-debug"),
"cmake",
"cmake-build-*/",
);
}
#[test]
fn should_skip_an_off_by_default_artifact_and_say_how_to_enable_it() {
let fixture = tree(&["go.mod", "bin/"]);
let classifier = classifier();
match verdict(&classifier, fixture.path(), "bin") {
Verdict::Skip(SkipReason::NotEnabled { spec, note }) => {
assert_eq!(spec, "go.bin");
assert!(note.is_some(), "an opt-in artifact must explain itself");
}
other => panic!("expected an opt-in skip, got {other:?}"),
}
}
#[test]
fn should_remove_an_off_by_default_artifact_once_enabled() {
let fixture = tree(&["go.mod", "bin/"]);
let mut selection = Selection::all();
assert!(selection.set("go.bin", true));
let classifier = Classifier::new(selection).unwrap();
assert_artifact(&verdict(&classifier, fixture.path(), "bin"), "go", "bin/");
}
#[test]
fn should_not_classify_when_the_ecosystem_is_deselected() {
let fixture = tree(&["Cargo.toml", "target/"]);
let (selection, unknown) = Selection::only(["node"]);
assert!(unknown.is_empty(), "a deselected ecosystem declares no unknown specs");
let classifier = Classifier::new(selection).unwrap();
match verdict(&classifier, fixture.path(), "target") {
Verdict::Skip(SkipReason::NotEnabled { .. }) => {}
other => panic!("expected a deselected skip, got {other:?}"),
}
}
#[test]
fn should_report_an_unknown_ecosystem_rather_than_ignoring_it() {
let (_, unknown) = Selection::only(["rust", "rustlang"]);
assert_eq!(unknown, vec!["rustlang".to_owned()]);
}
#[test]
fn should_list_each_anchor_directory_exactly_once() {
let mut entries = vec!["Cargo.toml".to_owned()];
for index in 0..20 {
entries.push(format!("crate{index}/Cargo.toml"));
entries.push(format!("crate{index}/target/"));
}
let refs: Vec<&str> = entries.iter().map(String::as_str).collect();
let fixture = tree(&refs);
let classifier = classifier();
for index in 0..20 {
assert_artifact(
&verdict(&classifier, fixture.path(), &format!("crate{index}/target")),
"rust",
"target/",
);
}
assert_eq!(classifier.probe_count(), 20);
}
#[test]
fn should_leave_an_unreadable_anchor_unproven() {
let fixture = tree(&["Cargo.toml", "target/"]);
let classifier = classifier();
let missing = fixture.path().join("gone");
match classifier.classify(fixture.path(), &missing.join("target"), true) {
Verdict::Skip(SkipReason::Unreadable) => {}
other => panic!("expected an unreadable skip, got {other:?}"),
}
}
#[test]
fn should_treat_an_unrelated_name_as_not_a_candidate() {
let fixture = tree(&["Cargo.toml", "src/"]);
let classifier = classifier();
assert_eq!(verdict(&classifier, fixture.path(), "src"), Verdict::NotACandidate);
assert_eq!(
classifier.probe_count(),
0,
"a non-candidate must not cost a directory listing"
);
}
#[test]
fn should_recognise_dependency_directories() {
assert!(is_dependency_dir(OsStr::new("node_modules")));
assert!(is_dependency_dir(OsStr::new("vendor")));
assert!(!is_dependency_dir(OsStr::new("target")));
}
}