Repository navigation
feat(monorepo): widen dynamic rule walk to the git repository root #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
code-yeongyu
merged 3 commits into
code-yeongyu:main
from
tradem:feat/monorepo-repository-root-walk
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { readFileSync, realpathSync } from "node:fs"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import type { EngineDeps } from "../src/rules/engine.js"; | ||
| import { createEngine, defaultConfig } from "../src/rules/engine.js"; | ||
| import { findRuleCandidates } from "../src/rules/finder.js"; | ||
| import { findProjectRoot } from "../src/rules/project-root.js"; | ||
| import { createTempFs, type TempFs } from "./helpers/temp-fs.js"; | ||
|
|
||
| function canonicalPath(path: string): string { | ||
| return realpathSync.native(path); | ||
| } | ||
|
|
||
| function createMonorepoDeps(fs: TempFs): EngineDeps { | ||
| const homeDir = fs.mkdir("home"); | ||
| return { | ||
| findProjectRoot, | ||
| findCandidates: (options) => findRuleCandidates({ ...options, homeDir }), | ||
| readFile: (path) => { | ||
| try { | ||
| return readFileSync(path, "utf-8"); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }, | ||
| extractToolPaths: () => [], | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Monorepo semantics: a Cargo/pnpm workspace member (nested project marker) must still | ||
| * discover rule directories from the workspace and repository levels because | ||
| * `loadDynamicRules` widens the per-target project root to the enclosing git root | ||
| * (see `widenToRepositoryRoot`). | ||
| */ | ||
| describe("loadDynamicRules (monorepo workspace)", () => { | ||
| it("#given workspace member target with workspace-level rules #when loadDynamicRules #then workspace rule and single-file rules from walk levels are returned", () => { | ||
| // given | ||
| const fs = createTempFs("pi-rules-monorepo-"); | ||
| try { | ||
| fs.mkdir("repo/.git"); | ||
| fs.write("repo/backend/Cargo.toml", ""); | ||
| const rustRule = fs.write( | ||
| "repo/backend/.github/instructions/rust.instructions.md", | ||
| '---\napplyTo: "crates/**/*.rs"\n---\nBackend Rust rule.\n', | ||
| ); | ||
| const agents = fs.write("repo/backend/AGENTS.md", "# Backend AGENTS\n"); | ||
| fs.write("repo/backend/crates/member/Cargo.toml", ""); | ||
| const targetPath = fs.write("repo/backend/crates/member/src/lib.rs", "pub fn f() {}\n"); | ||
|
|
||
| const engine = createEngine(defaultConfig(), createMonorepoDeps(fs)); | ||
|
|
||
| // when | ||
| const result = engine.loadDynamicRules(fs.path("repo"), [targetPath]); | ||
|
|
||
| // then | ||
| const paths = result.rules.map((rule) => canonicalPath(rule.path)); | ||
| expect(paths).toContain(canonicalPath(rustRule)); | ||
| expect(paths).toContain(canonicalPath(agents)); | ||
| } finally { | ||
| fs.cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| it("#given workspace member target #when loadDynamicRules #then repository-level rules match via repo-relative globs", () => { | ||
| // given | ||
| const fs = createTempFs("pi-rules-monorepo-"); | ||
| try { | ||
| fs.mkdir("repo/.git"); | ||
| fs.write("repo/backend/Cargo.toml", ""); | ||
| const rootLevelRule = fs.write( | ||
| "repo/.github/instructions/scripts.instructions.md", | ||
| '---\napplyTo: "backend/crates/**/*.rs"\n---\nRepository Rust rule.\n', | ||
| ); | ||
| fs.write("repo/backend/crates/member/Cargo.toml", ""); | ||
| const targetPath = fs.write("repo/backend/crates/member/src/lib.rs", "pub fn f() {}\n"); | ||
|
|
||
| const engine = createEngine(defaultConfig(), createMonorepoDeps(fs)); | ||
|
|
||
| // when | ||
| const result = engine.loadDynamicRules(fs.path("repo"), [targetPath]); | ||
|
|
||
| // then | ||
| expect(result.rules.map((rule) => canonicalPath(rule.path))).toContain(canonicalPath(rootLevelRule)); | ||
| } finally { | ||
| fs.cleanup(); | ||
| } | ||
| }); | ||
|
|
||
| it("#given workspace-level globs scoped to another subtree #when loadDynamicRules #then rule does not match", () => { | ||
| // given | ||
| const fs = createTempFs("pi-rules-monorepo-"); | ||
| try { | ||
| fs.mkdir("repo/.git"); | ||
| fs.write("repo/backend/Cargo.toml", ""); | ||
| fs.write( | ||
| "repo/backend/.github/instructions/scripts.instructions.md", | ||
| '---\napplyTo: "scripts/**"\n---\nScripts rule.\n', | ||
| ); | ||
| fs.write("repo/backend/crates/member/Cargo.toml", ""); | ||
| const targetPath = fs.write("repo/backend/crates/member/src/lib.rs", "pub fn f() {}\n"); | ||
|
|
||
| const engine = createEngine(defaultConfig(), createMonorepoDeps(fs)); | ||
|
|
||
| // when | ||
| const result = engine.loadDynamicRules(fs.path("repo"), [targetPath]); | ||
|
|
||
| // then | ||
| expect(result.rules.map((rule) => rule.path)).toHaveLength(0); | ||
| } finally { | ||
| fs.cleanup(); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Walk correctly returns the first .git found walking up, so it cannot escape above the innermost repo root. Please realpath the walk (same as findProjectRoot) before comparing/returning: on macOS resolve() stays on /var/folders while tests and findProjectRoot use /private/var, so widenToRepositoryRoot currently returns a non-canonical path and the nested-member test fails. Also add a test that a nested .git wins over a parent .git (must not keep walking).