Skip to content
Closed
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
57 changes: 53 additions & 4 deletions src/repo/adapters/rust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ function completeRustInventory(root: string, files: ReadonlySet<string>, trees:

/** A small lexer, not a Rust parser: ignore comments/strings, but never guess
* a computed include. Nested comments and raw strings must not hide an input. */
function rustInputs(source: string): { inputs: Array<{ path: string; code: boolean }>; paths: string[]; compiles: boolean; navigates: boolean; documentation: boolean } | undefined {
function rustInputs(source: string): { inputs: Array<{ path: string; code: boolean; module?: string[] }>; paths: string[]; compiles: boolean; navigates: boolean; documentation: boolean } | undefined {
const tokens: Array<{ value: string; literal?: string }> = [];
for (let i = 0; i < source.length;) {
if (/\s/.test(source[i]!)) { i++; continue; }
Expand Down Expand Up @@ -238,23 +238,53 @@ function rustInputs(source: string): { inputs: Array<{ path: string; code: boole
// dependency edge cannot see is code that reaches another crate's modelled Rust inputs at run or
// build time. Report literal paths (the caller checks which crates they reach) and Cargo/rustc
// invocations; ordinary identifiers such as `fs`, `process`, `unsafe` or `metadata` are not inputs.
const inputs: Array<{ path: string; code: boolean }> = [];
const inputs: Array<{ path: string; code: boolean; module?: string[] }> = [];
// Inline `mod name { ... }` blocks open around each token, for `#[path]` (null: any other brace).
const blocks: Array<string | null> = [];
const paths: string[] = [];
let compiles = false;
const includeArguments = new Set<number>();
for (let i = 0; i < tokens.length; i++) {
if (tokens[i]!.value === "mod" && tokens[i + 1]?.value === "target" && tokens[i + 2]?.value === ";") return undefined;
if (tokens[i]!.value === "{") blocks.push(tokens[i - 2]?.value === "mod" && /^[A-Za-z_]/.test(tokens[i - 1]?.value ?? "") ? tokens[i - 1]!.value : null);
if (tokens[i]!.value === "}") blocks.pop();
if (tokens[i]!.value === "#") {
let j = i + 1;
if (tokens[j]?.value === "!") j++;
if (tokens[j]?.value === "[") {
let depth = 1;
const paths: string[] = [];
for (j++; j < tokens.length && depth; j++) {
if (tokens[j]!.value === "[") depth++;
if (tokens[j]!.value === "]") depth--;
if (tokens[j]!.value === "path" && tokens[j + 1]?.value === "=") return undefined;
if (tokens[j]!.value === "path" && tokens[j + 1]?.value === "=") {
// `#[path = "x.rs"]` or `#[cfg_attr(pred, path = "x.rs")]` with a plain literal.
const literal = tokens[j + 2]?.literal;
if (tokens[i + 1]?.value === "!" || !["[", ","].includes(tokens[j - 1]?.value ?? "") || literal === undefined
|| !["]", ",", ")"].includes(tokens[j + 3]?.value ?? "") || /[\0\r\n]/.test(literal)) return undefined;
paths.push(literal);
}
}
if (depth) return undefined;
if (paths.length) {
// Only on a file module (`mod name;`), after any other attributes and visibility: on an
// inline module the path would move every module declared inside it.
let k = j;
for (;;) {
if (tokens[k]?.value === "#" && tokens[k + 1]?.value === "[") {
let nested = 1;
for (k += 2; k < tokens.length && nested; k++) nested += tokens[k]!.value === "[" ? 1 : tokens[k]!.value === "]" ? -1 : 0;
} else if (tokens[k]?.value === "pub" && tokens[k + 1]?.value === "(") {
k = tokens.findIndex((token, index) => index > k && token.value === ")") + 1;
if (!k) return undefined;
} else if (tokens[k]?.value === "pub") k++;
else break;
}
if (tokens[k]?.value !== "mod" || !/^[A-Za-z_]/.test(tokens[k + 1]?.value ?? "") || tokens[k + 2]?.value !== ";") return undefined;
const module = blocks.filter((name): name is string => name !== null);
if (blocks.length !== module.length) return undefined;
for (const path of paths) inputs.push({ path, code: true, module });
}
}
}
if (tokens[i]!.value === "use") {
Expand Down Expand Up @@ -419,6 +449,9 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res
const moduleTrees = packages.flatMap(({ pkg }) => pkg.targets.map(target => resolvedPath(root, target.src_path)))
.filter((path): path is string => path !== undefined).map(path => dirname(path) === "." ? "" : dirname(path));
if (!completeRustInventory(root, known, moduleTrees)) result.blockers.push("Incomplete Rust source inventory or symlinked inputs require full validation");
// Crate roots, for `#[path]` inside inline modules; directories of `#[path]` modules, checked below.
const targetRoots = new Set(packages.flatMap(({ pkg }) => pkg.targets.map(target => resolvedPath(root, target.src_path))));
const pathModuleTrees: string[] = [];
result.sourcePaths = [...new Set(result.sourcePaths)].sort();
result.testFiles.sort();
if (!result.testFiles.length) result.blockers.push("Cargo metadata found no supported test targets");
Expand Down Expand Up @@ -504,7 +537,15 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res
reached.delete(sourceOwner.manifest);
for (const manifest of reached) result.edges.push({ from: sourceOwner.manifest, to: manifest, kind: "import" });
for (const input of scanned.inputs) {
const path = repositoryPath(root, resolve(root, dirname(source), input.path), known);
// A `#[path]` is relative to the file's directory; inside inline modules, also to their
// names, below the file's stem unless it is a crate root or mod.rs.
const modRs = targetRoots.has(source) || /(?:^|\/)mod\.rs$/.test(source);
if (input.module?.length && !modRs) {
result.blockers.push(`Rust computed includes or path attributes require full validation: ${source}`);
continue;
}
const base = input.module ? posix.join(dirname(source), ...input.module) : dirname(source);
const path = repositoryPath(root, resolve(root, base, input.path), known);
if (!path || owner(path)?.pkg.id !== sourceOwner.pkg.id) {
result.blockers.push(`Rust include outside its crate or unavailable requires full validation: ${source}`);
continue;
Expand All @@ -513,10 +554,18 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res
result.blockers.push(`Rust include code with a non-Rust extension requires full validation: ${source}`);
continue;
}
if (input.module) {
// Module source of the same crate: already a source of it. What it declares is looked up
// from its own directory, so that tree must be complete too.
pathModuleTrees.push(dirname(path) === "." ? "" : dirname(path));
result.edges.push({ from: source, to: path, kind: "import" });
continue;
}
if (!result.sourcePaths.includes(path)) result.assetPaths.push(path);
result.edges.push({ from: source, to: path, kind: "asset" });
}
}
if (pathModuleTrees.length && !completeRustInventory(root, known, pathModuleTrees)) result.blockers.push("Incomplete Rust source inventory or symlinked inputs require full validation");
result.assetPaths = [...new Set(result.assetPaths)].sort();
// Selective commands must give every package the features the full invocation gives it. Cargo
// resolves each tested crate alone and with its dependents (what a change to it selects); the
Expand Down
35 changes: 35 additions & 0 deletions tests/repo/rust-adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,41 @@ test("Cargo feature-unified unit tests are recorded per crate instead of blockin
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("literal #[path] modules inside their crate narrow; other path attributes still fall back", () => {
const lib = '#[cfg_attr(unix, path = "sys/unix.rs")]\n#[cfg_attr(windows, path = "sys/windows.rs")]\n#[allow(dead_code)]\npub(crate) mod sys;\n'
+ 'mod a { #[path = "inner.rs"] pub mod b; }\n#[path = "../shared/x.rs"] mod shared;\n';
const files = { "Cargo.toml": '[workspace]\nmembers = ["app", "other"]\n', "app/Cargo.toml": manifest("app"), "app/src/lib.rs": lib,
"app/src/sys/unix.rs": "", "app/src/sys/windows.rs": "", "app/src/a/inner.rs": "", "app/shared/x.rs": "", "app/src/plain.rs": "",
"other/Cargo.toml": manifest("other"), "other/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, "app", "app"), pkg(root, "other", "other")]);
const result = analyzeRustMetadata(context, output);
assert.deepEqual(result.blockers, []);
for (const module of ["app/src/sys/unix.rs", "app/src/sys/windows.rs", "app/src/a/inner.rs", "app/shared/x.rs"]) {
assert.ok(result.sourcePaths.includes(module), module);
assert.ok(result.edges.some(edge => edge.from === "app/src/lib.rs" && edge.to === module), module);
}
assert.ok(!result.edges.some(edge => edge.from === "app/Cargo.toml" && edge.to === "other/Cargo.toml"));
// A path module outside the target's tree makes its own directory part of the inventory.
writeFileSync(join(root, "app/shared/untracked.rs"), "");
assert.ok(analyzeRustMetadata(context, output).blockers.some(reason => reason.includes("Incomplete Rust source inventory")));
rmSync(join(root, "app/shared/untracked.rs"));
for (const [file, source] of [
["app/src/lib.rs", '#[path = "../../other/src/lib.rs"] mod external;'], ["app/src/lib.rs", '#[path = "missing.rs"] mod missing;'],
["app/src/lib.rs", '#[path = "sys"] mod sys { mod unix; }'], ["app/src/lib.rs", '#![path = "sys/unix.rs"]'],
["app/src/lib.rs", '#[path = concat!("sys/", "unix.rs")] mod sys;'], ["app/src/lib.rs", '#[doc(path = "sys/unix.rs")] mod sys;'],
// Inside an inline module of a file that is not a crate root or mod.rs, the base depends on how it was loaded.
["app/src/plain.rs", 'mod q { #[path = "../sys/unix.rs"] mod z; }'],
]) {
writeFileSync(join(root, file), source);
assert.ok(analyzeRustMetadata(context, output).blockers.some(reason => /Rust (?:computed includes|include outside)/.test(reason)), source);
writeFileSync(join(root, file), file === "app/src/lib.rs" ? lib : "");
}
} finally { rmSync(root, { recursive: true, force: true }); }
});

test("symlinks outside module trees and linked non-Rust files do not block", (t) => {
const files = { "Cargo.toml": '[workspace]\nmembers=["app"]\n', "app/Cargo.toml": manifest("app"), "app/src/lib.rs": "", "LICENSE": "MIT", "docs/guide.md": "" };
const root = fixture(files);
Expand Down
Loading