use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context, Result};
use crate::lint_apply::{
check_cargo_dylint_available, check_lint_path, default_instrument_manifest_path,
diff_snapshots, resolve_default_lint_path, snapshot_rs_files, write_manifest, LintRunManifest,
Operation,
};
use crate::staleness;
pub struct LintInstrumentArgs {
pub lint_path: PathBuf,
pub workspace_root: PathBuf,
pub dry_run: bool,
pub allow_dirty: bool,
}
pub fn warn_if_captrack_dep_missing_or_telemetry_off(workspace_root: &Path) {
let root_toml = workspace_root.join("Cargo.toml");
if !root_toml.is_file() {
return; }
let root_body = match std::fs::read_to_string(&root_toml) {
Ok(b) => b,
Err(_) => return,
};
let member_tomls = collect_member_tomls(workspace_root, &root_body);
let mut found_captrack = false;
let mut found_telemetry = false;
for toml_path in &member_tomls {
let body = match std::fs::read_to_string(toml_path) {
Ok(b) => b,
Err(_) => continue,
};
if has_captrack_dep(&body) {
found_captrack = true;
if has_telemetry_feature(&body) {
found_telemetry = true;
}
}
}
if has_captrack_dep(&root_body) {
found_captrack = true;
if has_telemetry_feature(&root_body) {
found_telemetry = true;
}
}
if !found_captrack {
eprintln!(
"captrack-pgo: warning: workspace does not appear to depend on `captrack`.\n\
Instrument will rewrite constructors to `::captrack::TrackedX::with_capacity_named(...)`\n\
which won't compile unless `captrack` is added as a dependency.\n\
Add to your Cargo.toml: captrack = {{ version = \"*\", features = [\"telemetry\"] }}"
);
} else if !found_telemetry {
eprintln!(
"captrack-pgo: warning: `captrack` is a dependency but the `telemetry` feature \
does not appear to be enabled.\n\
The instrumented code will compile but record nothing.\n\
Enable with: captrack = {{ version = \"*\", features = [\"telemetry\"] }}"
);
}
}
fn collect_member_tomls(workspace_root: &Path, root_toml_body: &str) -> Vec<PathBuf> {
let mut paths = vec![workspace_root.join("Cargo.toml")];
let mut in_workspace_section = false;
let mut in_members_array = false;
for line in root_toml_body.lines() {
let trimmed = line.trim();
if trimmed.starts_with('[') {
in_workspace_section = trimmed == "[workspace]" || trimmed.starts_with("[workspace]");
in_members_array = false; continue;
}
if !in_workspace_section {
continue;
}
if trimmed.starts_with("members") || in_members_array {
in_members_array = true;
let mut pos = 0;
let line_bytes = trimmed.as_bytes();
while pos < line_bytes.len() {
if line_bytes[pos] == b'"' {
let start = pos + 1;
if let Some(end_off) = trimmed[start..].find('"') {
let member = &trimmed[start..start + end_off];
let member_toml = workspace_root.join(member).join("Cargo.toml");
paths.push(member_toml);
pos = start + end_off + 1;
} else {
break;
}
} else {
pos += 1;
}
}
if trimmed.contains(']') {
in_members_array = false;
}
}
}
paths
}
fn has_captrack_dep(body: &str) -> bool {
for line in body.lines() {
let t = line.trim();
if t.starts_with('#') {
continue;
}
if t.starts_with("captrack") {
let rest = t.trim_start_matches("captrack").trim_start();
if rest.starts_with('=') {
return true;
}
}
}
false
}
fn has_telemetry_feature(body: &str) -> bool {
let mut in_captrack_block = false;
for line in body.lines() {
let t = line.trim();
if t.starts_with('#') {
continue;
}
if t.starts_with("captrack") {
let rest = t.trim_start_matches("captrack").trim_start();
if rest.starts_with('=') {
in_captrack_block = true;
}
}
if in_captrack_block {
if t.contains("telemetry") {
return true;
}
if !t.starts_with("captrack") && (t.contains('}') || (t.contains('=') && !t.is_empty()))
{
in_captrack_block = false;
}
}
}
false
}
pub fn run_lint_instrument(args: LintInstrumentArgs) -> Result<()> {
check_lint_path(&args.lint_path)?;
let workspace_cargo_toml = args.workspace_root.join("Cargo.toml");
if !workspace_cargo_toml.is_file() {
return Err(anyhow!(
"workspace root '{}' does not contain a Cargo.toml",
args.workspace_root.display()
));
}
warn_if_captrack_dep_missing_or_telemetry_off(&args.workspace_root);
check_cargo_dylint_available()?;
let before_snapshots =
snapshot_rs_files(&args.workspace_root).context("snapshot workspace before dylint")?;
let mut cmd = std::process::Command::new("cargo");
cmd.arg("dylint");
cmd.arg("--path");
cmd.arg(&args.lint_path);
if !args.dry_run {
cmd.arg("--fix");
}
cmd.arg("--");
cmd.arg("--manifest-path");
cmd.arg(&workspace_cargo_toml);
cmd.arg("--all-targets");
if args.allow_dirty {
cmd.arg("--allow-dirty");
}
cmd.env("CAPTRACK_PGO_INSTRUMENT", "1");
cmd.env_remove("CAPTRACK_PGO_PROFILE");
cmd.stdin(std::process::Stdio::null());
if args.dry_run {
println!("captrack-pgo: dry-run — running without --fix (suggestions appear in stderr):");
println!(
" CAPTRACK_PGO_INSTRUMENT=1 cargo dylint --path {} -- --manifest-path {}",
args.lint_path.display(),
workspace_cargo_toml.display()
);
}
let status = cmd
.status()
.context("spawn cargo dylint — is cargo-dylint installed?")?;
if !status.success() {
return Err(anyhow!(
"cargo dylint exited with non-zero status: {:?}\n\
Files NOT modified; manifest NOT written.",
status.code()
));
}
if args.dry_run {
println!("(dry-run complete — no files modified, manifest not written)");
return Ok(());
}
let (changed_files, total_bytes_changed) = diff_snapshots(&before_snapshots);
let manifest = LintRunManifest {
version: 1,
operation: Operation::Instrument,
files: changed_files.clone(),
};
let manifest_path = default_instrument_manifest_path(&args.workspace_root);
write_manifest(&manifest, &manifest_path)?;
let staleness_path = staleness::write_staleness_snapshot(&args.workspace_root)
.context("write staleness snapshot")?;
let n = changed_files.len();
println!(
"instrument: modified {} file{}, net {} byte{} changed",
n,
if n == 1 { "" } else { "s" },
total_bytes_changed,
if total_bytes_changed.abs() == 1 {
""
} else {
"s"
}
);
println!(" manifest: {}", manifest_path.display());
println!(
" revert with: captrack-pgo undo --manifest {}",
manifest_path.display()
);
println!(" staleness snapshot: {}", staleness_path.display());
Ok(())
}
pub fn resolve_lint_path(lint_path: Option<PathBuf>) -> Result<PathBuf> {
match lint_path {
Some(p) => Ok(p),
None => resolve_default_lint_path(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn has_captrack_dep_detects_simple_dep() {
let toml = r#"
[dependencies]
captrack = { version = "0.1", features = ["telemetry"] }
anyhow = "1"
"#;
assert!(has_captrack_dep(toml));
}
#[test]
fn has_captrack_dep_false_for_no_dep() {
let toml = r#"
[dependencies]
anyhow = "1"
serde = "1"
"#;
assert!(!has_captrack_dep(toml));
}
#[test]
fn has_captrack_dep_ignores_comments() {
let toml = r#"
[dependencies]
# captrack = "0.1"
anyhow = "1"
"#;
assert!(!has_captrack_dep(toml));
}
#[test]
fn has_telemetry_feature_inline() {
let toml = r#"
[dependencies]
captrack = { version = "0.1", features = ["telemetry"] }
"#;
assert!(has_captrack_dep(toml));
assert!(has_telemetry_feature(toml));
}
#[test]
fn has_telemetry_feature_missing() {
let toml = r#"
[dependencies]
captrack = { version = "0.1" }
"#;
assert!(has_captrack_dep(toml));
assert!(!has_telemetry_feature(toml));
}
#[test]
fn collect_member_tomls_finds_members() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
let toml_body = "[workspace]\nmembers = [\"alpha\", \"beta\"]\n";
std::fs::create_dir_all(root.join("alpha")).unwrap();
std::fs::create_dir_all(root.join("beta")).unwrap();
std::fs::write(root.join("alpha").join("Cargo.toml"), "").unwrap();
std::fs::write(root.join("beta").join("Cargo.toml"), "").unwrap();
let paths = collect_member_tomls(root, toml_body);
assert!(
paths.len() >= 3,
"expected at least 3 paths, got {:?}",
paths
);
assert!(paths
.iter()
.any(|p| p.ends_with("alpha/Cargo.toml") || p.ends_with("alpha\\Cargo.toml")));
assert!(paths
.iter()
.any(|p| p.ends_with("beta/Cargo.toml") || p.ends_with("beta\\Cargo.toml")));
}
#[test]
fn warn_check_no_captrack_does_not_panic() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = []\n[dependencies]\nanyhow = \"1\"\n",
)
.unwrap();
warn_if_captrack_dep_missing_or_telemetry_off(root);
}
#[test]
fn warn_check_with_captrack_telemetry_does_not_panic() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
std::fs::write(
root.join("Cargo.toml"),
"[workspace]\nmembers = []\n[dependencies]\ncaptrack = { version = \"0.1\", features = [\"telemetry\"] }\n",
)
.unwrap();
warn_if_captrack_dep_missing_or_telemetry_off(root);
}
}