|
1 | 1 | import * as path from "path" |
2 | 2 | import * as os from "os" |
3 | 3 | import fs from "fs/promises" |
| 4 | +import { string } from "zod" |
| 5 | +import { dir } from "console" |
4 | 6 |
|
5 | 7 | /** |
6 | 8 | * Gets the global .roo directory path based on the current platform |
@@ -145,15 +147,48 @@ export async function readFileIfExists(filePath: string): Promise<string | null> |
145 | 147 | * ``` |
146 | 148 | */ |
147 | 149 | export function getRooDirectoriesForCwd(cwd: string): string[] { |
148 | | - const directories: string[] = [] |
| 150 | + const directories: Set<string> = new Set<string>() |
149 | 151 |
|
150 | 152 | // Add global directory first |
151 | | - directories.push(getGlobalRooDirectory()) |
| 153 | + directories.add(getGlobalRooDirectory()) |
152 | 154 |
|
153 | | - // Add project-local directory second |
154 | | - directories.push(getProjectRooDirectoryForCwd(cwd)) |
| 155 | + // Add project and any parent directories |
| 156 | + let currentCwd = path.resolve(cwd) |
| 157 | + const rootDir = path.parse(currentCwd).root |
155 | 158 |
|
156 | | - return directories |
| 159 | + // Get parentRulesMaxDepth from global state |
| 160 | + let maxDepth = 1 |
| 161 | + try { |
| 162 | + const { ContextProxy } = require("../../core/config/ContextProxy") |
| 163 | + maxDepth = ContextProxy.instance?.getValue("parentRulesMaxDepth") ?? 1 |
| 164 | + console.log("Using parentRulesMaxDepth:", maxDepth) |
| 165 | + } catch (error) { |
| 166 | + // In test environments, ContextProxy might not be initialized |
| 167 | + // Fall back to default value of 1 |
| 168 | + console.error("Using default parentRulesMaxDepth: 1", error) |
| 169 | + } |
| 170 | + |
| 171 | + let currentDepth = 0 |
| 172 | + |
| 173 | + // Loop from initialCwd up to root or until max depth is reached |
| 174 | + while (currentDepth < maxDepth) { |
| 175 | + directories.add(getProjectRooDirectoryForCwd(currentCwd)) |
| 176 | + |
| 177 | + // Stop if we've reached the root directory |
| 178 | + if (currentCwd === rootDir) { |
| 179 | + break |
| 180 | + } |
| 181 | + const parentCwd = path.resolve(currentCwd, "..") |
| 182 | + |
| 183 | + // Safety break if path.resolve doesn't change currentCwd (e.g., already at root) |
| 184 | + if (parentCwd === currentCwd) { |
| 185 | + break |
| 186 | + } |
| 187 | + currentCwd = parentCwd |
| 188 | + currentDepth++ |
| 189 | + } |
| 190 | + |
| 191 | + return Array.from(directories).sort() |
157 | 192 | } |
158 | 193 |
|
159 | 194 | /** |
|
0 commit comments