Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/repo/adapters/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,14 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res
const clean = literal.replace(/\\/g, "/").replace(/^(?:[A-Za-z]:)?\/+/, "");
const glob = clean.search(/[*?[{]/);
const prefix = glob < 0 ? clean : clean.slice(0, clean.lastIndexOf("/", glob) + 1);
for (const base of new Set([sourceOwner.directory, dirname(source) === "." ? "" : dirname(source)])) {
// Also from the workspace root: rustc and proc macros run there, and `[env]` paths such as
// CARGO_WORKSPACE_DIR start there. From the root, only a path below it names a crate.
const bases = [sourceOwner.directory, dirname(source) === "." ? "" : dirname(source)];
for (const [index, base] of [...bases, ""].entries()) {
const joined = posix.normalize(posix.join(base || ".", prefix || "."));
if (joined === ".." || joined.startsWith("../")) continue;
const rel = joined === "." ? "" : joined.replace(/\/+$/, "");
if (index === bases.length && !rel) continue;
const inside = rel ? owner(rel) ?? owner(`${rel}/`) : packages.find(pkg => pkg.directory === "");
if (inside) reached.add(inside.manifest);
for (const other of packages) if (other.directory && (rel === "" || under(other.directory, rel) || other.directory === rel)) reached.add(other.manifest);
Expand Down
22 changes: 22 additions & 0 deletions tests/repo/rust-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,28 @@ mod tests {
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("literal paths from the workspace root reach the crate they name", () => {
const files = { "Cargo.toml": '[workspace]\nmembers=["crates/app", "crates/other", "crates/third"]\n',
".cargo/config.toml": '[env]\nCARGO_WORKSPACE_DIR = { value = "", relative = true }\n',
"crates/app/Cargo.toml": manifest("app"), "crates/app/src/lib.rs": "", "crates/other/Cargo.toml": manifest("other"), "crates/other/src/lib.rs": "",
"crates/third/Cargo.toml": manifest("third"), "crates/third/src/lib.rs": "" };
const root = fixture(files);
try {
const context = { repoPath: root, files: Object.keys(files), profile: analyzeRepository({ repoPath: root }) };
const output = metadata(root, [pkg(root, "crates/app", "app"), pkg(root, "crates/other", "other"), pkg(root, "crates/third", "third")]);
const reads = (source: string) => {
writeFileSync(join(root, "crates/app/src/lib.rs"), source);
const result = analyzeRustMetadata(context, output);
assert.deepEqual(result.blockers, [], source);
return result.edges.filter(edge => edge.from === "crates/app/Cargo.toml" && edge.to.endsWith("Cargo.toml")).map(edge => edge.to).sort();
};
assert.deepEqual(reads('#[test] fn header() { let p = std::path::Path::new(env!("CARGO_WORKSPACE_DIR")).join("crates/other/src/lib.rs"); std::fs::read_to_string(p).unwrap(); }'), ["crates/other/Cargo.toml"]);
assert.deepEqual(reads('#[test] fn all() { let _ = std::fs::read_dir(concat!(env!("CARGO_WORKSPACE_DIR"), "crates/")); }'), ["crates/other/Cargo.toml", "crates/third/Cargo.toml"]);
// The root itself, or a path that only makes sense inside the crate, is not a read of another crate.
assert.deepEqual(reads('#[test] fn local() { let _ = std::fs::read("tests/data/input.bin"); let _ = format!("{}/{}", "a", "b"); let _ = std::path::Path::new("/"); }'), []);
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("registry dependencies narrow; local outside path dependencies, symlinked modules and discovery-excluded Rust sources fail closed", (t) => {
const files = { "Cargo.toml": manifest("app"), "src/lib.rs": "mod helper;", "src/helper.rs": "pub fn helper() {}" };
const root = fixture(files);
Expand Down
Loading