Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore(dep-audit): refine unused dependency detection

Steven Enamakelb559b68feaf0
diff --git a/scripts/dep-audit/report.mjs b/scripts/dep-audit/report.mjs
index 21cf792244..a72841e18d 100644
--- a/scripts/dep-audit/report.mjs
+++ b/scripts/dep-audit/report.mjs
@@ -119,25 +119,65 @@ function packageDir(data, packageName) {
}
/**
- * Textual re-check of an unused flag. Returns "attribute/macro use" if the
- * crate name (with `-` folded to `_`) appears anywhere in the package's
- * sources, `build.rs`, benches or tests; "none" otherwise.
+ * Directories whose `*.rs` files belong to a package: its own directory plus
+ * the parent directory of every explicit `path = "..."` target in its
+ * Cargo.toml (`[[test]]`, `[[example]]`, `[[bench]]`, `[[bin]]`, `[lib]`).
+ * OpenHuman's root crate declares its integration tests as
+ * `path = "../../tests/<name>.rs"`, which a scan of the crate dir alone misses.
+ */
+function packageSourceDirs(dir) {
+ const dirs = new Set([dir]);
+ const manifest = path.join(dir, "Cargo.toml");
+ if (!fs.existsSync(manifest)) return [...dirs];
+ const toml = fs.readFileSync(manifest, "utf8");
+ let section = "";
+ for (const raw of toml.split("\n")) {
+ const line = raw.trim();
+ const head = /^\[\[?([a-zA-Z.-]+)\]?\]/.exec(line);
+ if (head) {
+ section = head[1];
+ continue;
+ }
+ if (!/^(test|example|bench|bin|lib)$/.test(section)) continue;
+ const m = /^path\s*=\s*"([^"]+)"/.exec(line);
+ if (m) dirs.add(path.dirname(path.resolve(dir, m[1])));
+ }
+ return [...dirs];
+}
+
+/**
+ * Textual re-check of an unused flag over the package's sources.
+ *
+ * Returns:
+ * "path" — the crate is referenced the way Rust code references a crate
+ * (`foo::`, `use foo`, `extern crate foo`, `#[foo`, `foo!`), which
+ * tinyanalyzer misses only when it sits inside an attribute or a
+ * macro body. Treat as used.
+ * "word" — the bare name occurs (comment, string, doc) but never as a path.
+ * "none" — nothing at all.
*/
function textualUse(dir, dep) {
if (!dir || !fs.existsSync(dir)) return "unknown";
const ident = dep.replace(/-/g, "_");
+ const dirs = packageSourceDirs(dir);
+ const pathRe = `(\\b${ident}::|\\buse\\s+${ident}\\b|extern\\s+crate\\s+${ident}\\b|#\\[${ident}\\b|\\b${ident}!)`;
+ if (grepAny(dirs, pathRe)) return "path";
+ if (grepAny(dirs, `\\b${ident}\\b`)) return "word";
+ return "none";
+}
+
+function grepAny(dirs, pattern) {
try {
- // -l: stop at the first file per match; -r over the package dir only.
const out = execFileSync(
"grep",
- ["-rlE", `\\b${ident}\\b`, "--include=*.rs", "--exclude-dir=target", dir],
+ ["-rlE", pattern, "--include=*.rs", "--exclude-dir=target", "--exclude-dir=vendor", ...dirs],
{ encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] },
);
- return out.trim() ? "attribute/macro use" : "none";
+ return out.trim().length > 0;
} catch (err) {
- // grep exits 1 when nothing matched.
- if (err.status === 1) return "none";
- return "unknown";
+ // grep exits 1 when nothing matched; anything else is a real failure we
+ // would rather surface as "used" than as a false removal.
+ return err.status !== 1;
}
}
@@ -162,12 +202,13 @@ function unusedFor(target, data) {
version: pkg?.version ?? null,
exclusive_count: pkg?.exclusive_count ?? null,
evidence,
- verdict: evidence === "none" ? "remove" : "verify",
+ verdict: evidence === "path" ? "keep" : "remove",
});
}
return rows.sort(
(a, b) =>
(a.verdict === "remove" ? 0 : 1) - (b.verdict === "remove" ? 0 : 1) ||
+ (a.evidence === "none" ? 0 : 1) - (b.evidence === "none" ? 0 : 1) ||
(b.exclusive_count ?? 0) - (a.exclusive_count ?? 0) ||
a.package.localeCompare(b.package) ||
a.dependency.localeCompare(b.dependency),
@@ -265,8 +306,8 @@ function renderMarkdown(summary, n) {
// --- Unused --------------------------------------------------------------
push(``, `## 1. Declared dependencies no source file names`, ``);
push(
- `Verdict ${code("remove")}: the crate name appears nowhere in the package's ${code("*.rs")} files (not even inside an attribute) — delete the line from ${code("Cargo.toml")} and build.`,
- `Verdict ${code("verify")}: tinyanalyzer saw no ${code("use")}/path, but the name does occur textually (usually ${code("#[derive(crate::Trait)]")}) — probably a keeper; confirm by hand.`,
+ `Verdict ${code("remove")}: nothing in the package's ${code("*.rs")} files (including ${code("[[test]]")}/${code("[[example]]")} targets declared by path) references the crate as ${code("crate::…")}, ${code("use crate")}, ${code("#[crate…")} or ${code("crate!")}. Delete the line from ${code("Cargo.toml")} and build; if it was an optional dependency, drop the ${code("dep:")} feature too. "name only" means the word occurs in a comment or string, which is not a use.`,
+ `Verdict ${code("keep")}: tinyanalyzer saw no ${code("use")}/path, but one exists inside an attribute or macro body (${code("#[tokio::test]")}, ${code("#[derive(thiserror::Error)]")}) — a false positive of the tool, listed so the count is honest.`,
`Crates listed in ${code("[dependencies].ignore_unused")} of ${code("scripts/dep-audit/tinyanalyzer.toml")} are not reported at all.`,
``,
);
@@ -280,7 +321,7 @@ function renderMarkdown(summary, n) {
);
for (const u of unusedRows) {
push(
- `| ${u.target} | ${u.package} | ${code(u.dependency)} | ${u.kinds.join(", ")} | ${u.version ?? "—"} | ${u.exclusive_count ?? "—"} | ${u.verdict === "remove" ? "**remove**" : `verify (${u.evidence})`} |`,
+ `| ${u.target} | ${u.package} | ${code(u.dependency)} | ${u.kinds.join(", ")} | ${u.version ?? "—"} | ${u.exclusive_count ?? "—"} | ${u.verdict === "remove" ? (u.evidence === "word" ? "**remove** (name only)" : "**remove**") : "keep (attribute/macro path)"} |`,
);
}
}