diff --git a/.changeset/honest-deers-add.md b/.changeset/honest-deers-add.md new file mode 100644 index 00000000..e915dbba --- /dev/null +++ b/.changeset/honest-deers-add.md @@ -0,0 +1,5 @@ +--- +"claude-dev": minor +--- + +Support for Loading Files from the `.clinerules/` Directory diff --git a/docs/prompting/README.md b/docs/prompting/README.md index 4bbdec13..93a980cb 100644 --- a/docs/prompting/README.md +++ b/docs/prompting/README.md @@ -128,6 +128,27 @@ Cline's system prompt, on the other hand, is not user-editable ([here's where yo - Focus on Desired Outcomes: Describe the results you want, not the specific steps. - Test and Iterate: Experiment to find what works best for your workflow. + +### Support for Loading Files from the `.clinerules/` Directory +All files under the `.clinerules/` directory are recursively loaded, and their contents are merged into clineRulesFileInstructions. + +#### Example 1: +``` +.clinerules/ +├── .local-clinerules +└── .project-clinerules +``` + +#### Example 2: +``` +.clinerules/ +├── .clinerules-nextjs +├── .clinerules-serverside +└── tests/ + ├── .pytest-clinerules + └── .jest-clinerules +``` + ## Prompting Cline 💬 **Prompting is how you communicate your needs for a given task in the back-and-forth chat with Cline.** Cline understands natural language, so write conversationally. diff --git a/src/core/Cline.ts b/src/core/Cline.ts index 305ed616..38093d5d 100644 --- a/src/core/Cline.ts +++ b/src/core/Cline.ts @@ -46,7 +46,7 @@ import { getApiMetrics } from "../shared/getApiMetrics" import { HistoryItem } from "../shared/HistoryItem" import { ClineAskResponse, ClineCheckpointRestore } from "../shared/WebviewMessage" import { calculateApiCostAnthropic } from "../utils/cost" -import { fileExistsAtPath } from "../utils/fs" +import { fileExistsAtPath, isDirectory } from "../utils/fs" import { arePathsEqual, getReadablePath } from "../utils/path" import { fixModelHtmlEscaping, removeInvalidChars } from "../utils/string" import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from "./assistant-message" @@ -1296,13 +1296,33 @@ export class Cline { const clineRulesFilePath = path.resolve(cwd, GlobalFileNames.clineRules) let clineRulesFileInstructions: string | undefined if (await fileExistsAtPath(clineRulesFilePath)) { - try { - const ruleFileContent = (await fs.readFile(clineRulesFilePath, "utf8")).trim() - if (ruleFileContent) { - clineRulesFileInstructions = `# .clinerules\n\nThe following is provided by a root-level .clinerules file where the user has specified instructions for this working directory (${cwd.toPosix()})\n\n${ruleFileContent}` + if (await isDirectory(clineRulesFilePath)) { + try { + // Read all files in the .clinerules/ directory. + const ruleFiles = await fs + .readdir(clineRulesFilePath, { withFileTypes: true, recursive: true }) + .then((files) => files.filter((file) => file.isFile())) + .then((files) => files.map((file) => path.resolve(file.parentPath, file.name))) + const ruleFileContent = await Promise.all( + ruleFiles.map(async (file) => { + const ruleFilePath = path.resolve(clineRulesFilePath, file) + const ruleFilePathRelative = path.relative(cwd, ruleFilePath) + return `${ruleFilePathRelative}\n` + (await fs.readFile(ruleFilePath, "utf8")).trim() + }), + ).then((contents) => contents.join("\n\n")) + clineRulesFileInstructions = `# .clinerules/\n\nThe following is provided by a root-level .clinerules/ directory where the user has specified instructions for this working directory (${cwd.toPosix()})\n\n${ruleFileContent}` + } catch { + console.error(`Failed to read .clinerules directory at ${clineRulesFilePath}`) + } + } else { + try { + const ruleFileContent = (await fs.readFile(clineRulesFilePath, "utf8")).trim() + if (ruleFileContent) { + clineRulesFileInstructions = `# .clinerules\n\nThe following is provided by a root-level .clinerules file where the user has specified instructions for this working directory (${cwd.toPosix()})\n\n${ruleFileContent}` + } + } catch { + console.error(`Failed to read .clinerules file at ${clineRulesFilePath}`) } - } catch { - console.error(`Failed to read .clinerules file at ${clineRulesFilePath}`) } } diff --git a/src/utils/fs.test.ts b/src/utils/fs.test.ts index ea9f132d..f820fe13 100644 --- a/src/utils/fs.test.ts +++ b/src/utils/fs.test.ts @@ -3,7 +3,7 @@ import { after, describe, it } from "mocha" import * as os from "os" import * as path from "path" import "should" -import { createDirectoriesForFile, fileExistsAtPath } from "./fs" +import { createDirectoriesForFile, fileExistsAtPath, isDirectory } from "./fs" describe("Filesystem Utilities", () => { const tmpDir = path.join(os.tmpdir(), "cline-test-" + Math.random().toString(36).slice(2)) @@ -68,4 +68,24 @@ describe("Filesystem Utilities", () => { exists.should.be.true() }) }) + describe("isDirectory", () => { + it("should return true for directories", async () => { + await fs.mkdir(tmpDir, { recursive: true }) + const isDir = await isDirectory(tmpDir) + isDir.should.be.true() + }) + + it("should return false for files", async () => { + const testFile = path.join(tmpDir, "test.txt") + await fs.writeFile(testFile, "test") + const isDir = await isDirectory(testFile) + isDir.should.be.false() + }) + + it("should return false for non-existent paths", async () => { + const nonExistentPath = path.join(tmpDir, "does-not-exist") + const isDir = await isDirectory(nonExistentPath) + isDir.should.be.false() + }) + }) }) diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 9f7af84e..48915496 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -45,3 +45,17 @@ export async function fileExistsAtPath(filePath: string): Promise { return false } } + +/** + * Checks if the path is a directory + * @param filePath - The path to check. + * @returns A promise that resolves to true if the path is a directory, false otherwise. + */ +export async function isDirectory(filePath: string): Promise { + try { + const stats = await fs.stat(filePath) + return stats.isDirectory() + } catch { + return false + } +}