From 3945dba424238308f651208016fa460b83d7c91c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Tue, 17 Nov 2026 23:40:07 +0700 Subject: [PATCH 1/4] feat(rust): accept literal #[path] modules inside their own crate --- src/repo/adapters/rust.ts | 56 ++++++++++++++++++++++++++++++--- tests/repo/rust-adapter.test.ts | 35 +++++++++++++++++++++ 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/repo/adapters/rust.ts b/src/repo/adapters/rust.ts index 76aa08e2..3976c345 100644 --- a/src/repo/adapters/rust.ts +++ b/src/repo/adapters/rust.ts @@ -197,7 +197,7 @@ function completeRustInventory(root: string, files: ReadonlySet, 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; } @@ -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 = []; const paths: string[] = []; let compiles = false; const includeArguments = new Set(); 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") { @@ -473,6 +503,8 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res if (packages.some(({ pkg }) => pkg.targets.some(target => target["required-features"]?.length))) { result.blockers.push("Cargo targets requiring features need an explicitly supported test invocation; full validation required"); } + const targetRoots = new Set(packages.flatMap(({ pkg }) => pkg.targets.map(target => resolvedPath(root, target.src_path)))); + const pathModuleTrees: string[] = []; for (const source of result.sourcePaths.filter(path => path.endsWith(".rs"))) { const sourceOwner = owner(source); if (!sourceOwner) continue; @@ -504,7 +536,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; @@ -513,10 +553,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 diff --git a/tests/repo/rust-adapter.test.ts b/tests/repo/rust-adapter.test.ts index 9675ceba..17165813 100644 --- a/tests/repo/rust-adapter.test.ts +++ b/tests/repo/rust-adapter.test.ts @@ -360,6 +360,41 @@ test("registry dependencies narrow; local outside path dependencies, symlinked m } 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("Cargo rejects symlink escapes and duplicate target source identities", (t) => { const files = { "Cargo.toml": manifest("root"), "src/lib.rs": "" }; const root = fixture(files); From c9e8e5a8ded595a46b40c05e32d002755cfd44a0 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:19:45 +0700 Subject: [PATCH 2/4] refactor(rust): declare the #[path] module sets next to the inventory check --- src/repo/adapters/rust.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/repo/adapters/rust.ts b/src/repo/adapters/rust.ts index 3976c345..29bb5cfc 100644 --- a/src/repo/adapters/rust.ts +++ b/src/repo/adapters/rust.ts @@ -449,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"); @@ -503,8 +506,6 @@ export function analyzeRustMetadata(context: AdapterContext, output: string, res if (packages.some(({ pkg }) => pkg.targets.some(target => target["required-features"]?.length))) { result.blockers.push("Cargo targets requiring features need an explicitly supported test invocation; full validation required"); } - const targetRoots = new Set(packages.flatMap(({ pkg }) => pkg.targets.map(target => resolvedPath(root, target.src_path)))); - const pathModuleTrees: string[] = []; for (const source of result.sourcePaths.filter(path => path.endsWith(".rs"))) { const sourceOwner = owner(source); if (!sourceOwner) continue; From ae37fab7ae9c71f4170c44332c39e8b0f4b58f7b Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 00:19:56 +0700 Subject: [PATCH 3/4] test(rust): move the #[path] test next to the other symlink and inventory tests --- tests/repo/rust-adapter.test.ts | 70 ++++++++++++++++----------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/tests/repo/rust-adapter.test.ts b/tests/repo/rust-adapter.test.ts index 17165813..8924be41 100644 --- a/tests/repo/rust-adapter.test.ts +++ b/tests/repo/rust-adapter.test.ts @@ -360,41 +360,6 @@ test("registry dependencies narrow; local outside path dependencies, symlinked m } 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("Cargo rejects symlink escapes and duplicate target source identities", (t) => { const files = { "Cargo.toml": manifest("root"), "src/lib.rs": "" }; const root = fixture(files); @@ -490,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); From c2dab3234c15d7bd5ccf4ad7a3c4b10dc014fdc4 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:44:58 +0700 Subject: [PATCH 4/4] chore: DCO remediation for 3945dba, c9e8e5a, ae37fab I, FredApps <153173854+FredApps@users.noreply.github.com>, hereby add my Signed-off-by to this commit: 3945dba424238308f651208016fa460b83d7c91c I, FredApps <153173854+FredApps@users.noreply.github.com>, hereby add my Signed-off-by to this commit: c9e8e5a8ded595a46b40c05e32d002755cfd44a0 I, FredApps <153173854+FredApps@users.noreply.github.com>, hereby add my Signed-off-by to this commit: ae37fab7ae9c71f4170c44332c39e8b0f4b58f7b Signed-off-by: FredApps <153173854+FredApps@users.noreply.github.com>