Skip to content

Commit fa50d31

Browse files
fix(rust): paths that climb out of a crate in code depend on every crate
Review on #92: a test that reads another crate's files only counted as depending on it when the code held a literal path to that crate. Tests that check every crate's files usually build the path instead, through .parent(), .ancestors(), PathBuf::pop or a formatted '..' path, so a change to the other crate skipped them. Treat such code like code that runs Cargo: its crate depends on every workspace crate. It only fires where the file also works with paths, so tree parent() methods and Vec::pop keep narrowing. Crate-local paths built from CARGO_MANIFEST_DIR stay narrow; the literal pass resolves them. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: adityankale190895 <[email protected]>
1 parent a2255f6 commit fa50d31

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

‎docs/using-diffci.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ For supported languages and setup limits, see [What is included](../README.md#wh
629629

630630
Maintainer: [@FredApps](https://github.com/FredApps).
631631

632-
Cargo package ownership and crate dependencies are not registered for selection yet. Command synthesis and integration remain separate milestones in [#62](https://github.com/DiffCI/core/issues/62). Metadata unions all features and target-platform dependencies, including dev-dependencies. Feature-conditional source stays crate-granular; required-feature test targets retain full validation. Locked registry and Git dependencies, including their build scripts and procedural macros, do not block selection: a `Cargo.lock` change keeps the full suite. A workspace crate with a build script, or code that runs Cargo or rustc, is treated as depending on every workspace crate, so it runs for any Rust change while other crates still narrow. A literal path in Rust code that reaches another crate becomes a dependency on that crate. When dependency declarations request features, Cargo resolves features for each tested crate alone and with its dependents (`cargo tree`), and a selected command must give every package the features the full `cargo test` gives it.
632+
Cargo package ownership and crate dependencies are not registered for selection yet. Command synthesis and integration remain separate milestones in [#62](https://github.com/DiffCI/core/issues/62). Metadata unions all features and target-platform dependencies, including dev-dependencies. Feature-conditional source stays crate-granular; required-feature test targets retain full validation. Locked registry and Git dependencies, including their build scripts and procedural macros, do not block selection: a `Cargo.lock` change keeps the full suite. A workspace crate with a build script, code that runs Cargo or rustc, or code that climbs out of its crate directory (`.parent()`, `.ancestors()`, `PathBuf::pop` or a formatted `..` path) is treated as depending on every workspace crate, so it runs for any Rust change while other crates still narrow. A literal path in Rust code that reaches another crate becomes a dependency on that crate. When dependency declarations request features, Cargo resolves features for each tested crate alone and with its dependents (`cargo tree`), and a selected command must give every package the features the full `cargo test` gives it.
633633

634634
| Reason | Meaning / next step |
635635
| --- | --- |

‎src/repo/adapters/rust.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ function completeRustInventory(root: string, files: ReadonlySet<string>, trees:
193193

194194
/** A small lexer, not a Rust parser: ignore comments/strings, but never guess
195195
* a computed include. Nested comments and raw strings must not hide an input. */
196-
function rustInputs(source: string): { inputs: Array<{ path: string; code: boolean }>; paths: string[]; compiles: boolean } | undefined {
196+
function rustInputs(source: string): { inputs: Array<{ path: string; code: boolean }>; paths: string[]; compiles: boolean; navigates: boolean } | undefined {
197197
const tokens: Array<{ value: string; literal?: string }> = [];
198198
for (let i = 0; i < source.length;) {
199199
if (/\s/.test(source[i]!)) { i++; continue; }
@@ -276,7 +276,23 @@ function rustInputs(source: string): { inputs: Array<{ path: string; code: boole
276276
tokens.forEach((token, index) => {
277277
if (token.literal !== undefined && !includeArguments.has(index) && pathLike(token.literal)) paths.push(token.literal);
278278
});
279-
return { inputs, paths, compiles };
279+
return { inputs, paths, compiles, navigates: navigatesUpward(tokens) };
280+
}
281+
282+
/** Code that climbs out of its crate directory in code (`.parent()`, `.ancestors()`, `PathBuf::pop`,
283+
* or a formatted path with a `..` segment) can reach any crate without a literal naming it. Tests
284+
* that check every crate's files (license headers, generated code) are written this way. Only fires
285+
* where the file also works with paths, so `node.parent()` in a parser does not count. */
286+
function navigatesUpward(tokens: ReadonlyArray<{ value: string; literal?: string }>): boolean {
287+
const pathContext = tokens.some(token => ["Path", "PathBuf", "current_dir", "current_exe"].includes(token.value)
288+
|| ["CARGO_MANIFEST_DIR", "CARGO_WORKSPACE_DIR"].includes(token.literal ?? ""));
289+
if (!pathContext) return false;
290+
const pathBuf = tokens.some(token => token.value === "PathBuf");
291+
return tokens.some((token, index) => {
292+
if (token.literal !== undefined) return /\{[^}]*\}/.test(token.literal) && /(?:^|[\\/}])\.\.(?:[\\/{]|$)/.test(token.literal);
293+
if (tokens[index - 1]?.value !== "." || tokens[index + 1]?.value !== "(") return false;
294+
return token.value === "parent" || token.value === "ancestors" || (pathBuf && token.value === "pop");
295+
});
280296
}
281297

282298
/** A string that names a file or directory rather than ordinary text. */
@@ -440,9 +456,10 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res
440456
}
441457
// Code that runs Cargo or rustc can build any workspace crate; a literal path can reach another
442458
// crate's sources at run or build time (relative to the crate, as under `cargo test`, or to the
443-
// file). Model both as dependencies of this crate instead of blocking the repository.
459+
// file). Code that climbs out of its crate in code can reach any crate, like code that compiles.
460+
// Model these as dependencies of this crate instead of blocking the repository.
444461
const reached = new Set<string>();
445-
if (scanned.compiles) for (const other of packages) reached.add(other.manifest);
462+
if (scanned.compiles || scanned.navigates) for (const other of packages) reached.add(other.manifest);
446463
for (const literal of scanned.paths) {
447464
const clean = literal.replace(/\\/g, "/").replace(/^(?:[A-Za-z]:)?\/+/, "");
448465
const glob = clean.search(/[*?[{]/);

‎tests/repo/rust-adapter.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,18 @@ test("Rust runtime reads of another crate become dependencies; non-rs included c
282282
assert.deepEqual(reads('fn all() { let _ = std::fs::read_dir(".."); }'), ["other/Cargo.toml", "third/Cargo.toml"]);
283283
assert.deepEqual(reads('fn build() { std::process::Command::new("cargo").arg("build").status().unwrap(); }'), ["other/Cargo.toml", "third/Cargo.toml"]);
284284
assert.deepEqual(reads('fn build() { std::process::Command::new(env!("CARGO")).status().unwrap(); }'), ["other/Cargo.toml", "third/Cargo.toml"]);
285+
// Paths built in code that climb out of the crate can reach any crate without a literal naming it
286+
// (review on #92): they depend on every workspace crate, like code that runs Cargo.
287+
for (const source of [
288+
'use std::path::Path; fn scan() { let p = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().join("other").join("src").join("lib.rs"); std::fs::read_to_string(p).unwrap(); }',
289+
'use std::path::Path; fn scan() { let p = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().join("other/src/lib.rs"); std::fs::read_to_string(p).unwrap(); }',
290+
'use std::path::Path; fn scan() { let root = Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(1).unwrap(); for entry in std::fs::read_dir(root).unwrap() { let _ = entry; } }',
291+
'fn scan(name: &str) { std::fs::read_to_string(format!("{}/../{}/src/lib.rs", env!("CARGO_MANIFEST_DIR"), name)).unwrap(); }',
292+
'use std::path::PathBuf; fn scan() { let mut p = PathBuf::from(env!("CARGO_MANIFEST_DIR")); p.pop(); p.push("other"); let _ = std::fs::read_dir(p); }',
293+
]) assert.deepEqual(reads(source), ["other/Cargo.toml", "third/Cargo.toml"], source);
294+
// Crate-local paths built from the manifest directory, and tree `parent()` without paths, still narrow.
295+
assert.deepEqual(reads('use std::path::Path; fn fixture() { std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("data.bin")).unwrap(); }'), []);
296+
assert.deepEqual(reads('struct Node; impl Node { fn parent(&self) -> Option<&Node> { None } } fn up(n: &Node) { let _ = n.parent(); let mut v = vec![1]; v.pop(); }'), []);
285297
} finally { rmSync(root, { recursive: true, force: true }); }
286298
});
287299

0 commit comments

Comments
 (0)