Skip to content
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

Add support for a .rooignore file #1367

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions src/__mocks__/vscode.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ const vscode = {
this.uri = uri
}
},
RelativePattern: class {
constructor(base, pattern) {
this.base = base
this.pattern = pattern
}
},
}

module.exports = vscode
100 changes: 87 additions & 13 deletions src/core/Cline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { calculateApiCost } from "../utils/cost"
import { fileExistsAtPath } from "../utils/fs"
import { arePathsEqual, getReadablePath } from "../utils/path"
import { parseMentions } from "./mentions"
import { RooIgnoreController, LOCK_TEXT_SYMBOL } from "./ignore/RooIgnoreController"
import { AssistantMessageContent, parseAssistantMessage, ToolParamName, ToolUseName } from "./assistant-message"
import { formatResponse } from "./prompts/responses"
import { SYSTEM_PROMPT } from "./prompts/system"
Expand Down Expand Up @@ -100,6 +101,7 @@ export class Cline {

apiConversationHistory: (Anthropic.MessageParam & { ts?: number })[] = []
clineMessages: ClineMessage[] = []
rooIgnoreController?: RooIgnoreController
private askResponse?: ClineAskResponse
private askResponseText?: string
private askResponseImages?: string[]
Expand Down Expand Up @@ -148,6 +150,11 @@ export class Cline {
throw new Error("Either historyItem or task/images must be provided")
}

this.rooIgnoreController = new RooIgnoreController(cwd)
this.rooIgnoreController.initialize().catch((error) => {
console.error("Failed to initialize RooIgnoreController:", error)
})

this.taskId = historyItem ? historyItem.id : crypto.randomUUID()

this.apiConfiguration = apiConfiguration
Expand Down Expand Up @@ -791,6 +798,7 @@ export class Cline {
this.terminalManager.disposeAll()
this.urlContentFetcher.closeBrowser()
this.browserSession.closeBrowser()
this.rooIgnoreController?.dispose()

// If we're not streaming then `abortStream` (which reverts the diff
// view changes) won't be called, so we need to revert the changes here.
Expand Down Expand Up @@ -942,6 +950,8 @@ export class Cline {
})
}

const rooIgnoreInstructions = this.rooIgnoreController?.getInstructions()

const {
browserViewportSize,
mode,
Expand Down Expand Up @@ -972,6 +982,7 @@ export class Cline {
this.diffEnabled,
experiments,
enableMcpServerCreation,
rooIgnoreInstructions,
)
})()

Expand Down Expand Up @@ -1346,6 +1357,15 @@ export class Cline {
// wait so we can determine if it's a new file or editing an existing file
break
}

const accessAllowed = this.rooIgnoreController?.validateAccess(relPath)
if (!accessAllowed) {
await this.say("rooignore_error", relPath)
pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath)))

break
}

// Check if file exists using cached map or fs.access
let fileExists: boolean
if (this.diffViewProvider.editType !== undefined) {
Expand Down Expand Up @@ -1555,6 +1575,14 @@ export class Cline {
break
}

const accessAllowed = this.rooIgnoreController?.validateAccess(relPath)
if (!accessAllowed) {
await this.say("rooignore_error", relPath)
pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath)))

break
}

const absolutePath = path.resolve(cwd, relPath)
const fileExists = await fileExistsAtPath(absolutePath)

Expand Down Expand Up @@ -1988,6 +2016,15 @@ export class Cline {
pushToolResult(await this.sayAndCreateMissingParamError("read_file", "path"))
break
}

const accessAllowed = this.rooIgnoreController?.validateAccess(relPath)
if (!accessAllowed) {
await this.say("rooignore_error", relPath)
pushToolResult(formatResponse.toolError(formatResponse.rooIgnoreError(relPath)))

break
}

this.consecutiveMistakeCount = 0
const absolutePath = path.resolve(cwd, relPath)
const completeMessage = JSON.stringify({
Expand Down Expand Up @@ -2033,7 +2070,12 @@ export class Cline {
this.consecutiveMistakeCount = 0
const absolutePath = path.resolve(cwd, relDirPath)
const [files, didHitLimit] = await listFiles(absolutePath, recursive, 200)
const result = formatResponse.formatFilesList(absolutePath, files, didHitLimit)
const result = formatResponse.formatFilesList(
absolutePath,
files,
didHitLimit,
this.rooIgnoreController,
)
const completeMessage = JSON.stringify({
...sharedMessageProps,
content: result,
Expand Down Expand Up @@ -2074,7 +2116,10 @@ export class Cline {
}
this.consecutiveMistakeCount = 0
const absolutePath = path.resolve(cwd, relDirPath)
const result = await parseSourceCodeForDefinitionsTopLevel(absolutePath)
const result = await parseSourceCodeForDefinitionsTopLevel(
absolutePath,
this.rooIgnoreController,
)
const completeMessage = JSON.stringify({
...sharedMessageProps,
content: result,
Expand Down Expand Up @@ -2122,7 +2167,13 @@ export class Cline {
}
this.consecutiveMistakeCount = 0
const absolutePath = path.resolve(cwd, relDirPath)
const results = await regexSearchFiles(cwd, absolutePath, regex, filePattern)
const results = await regexSearchFiles(
cwd,
absolutePath,
regex,
filePattern,
this.rooIgnoreController,
)
const completeMessage = JSON.stringify({
...sharedMessageProps,
content: results,
Expand Down Expand Up @@ -2301,6 +2352,19 @@ export class Cline {
)
break
}

const ignoredFileAttemptedToAccess = this.rooIgnoreController?.validateCommand(command)
if (ignoredFileAttemptedToAccess) {
await this.say("rooignore_error", ignoredFileAttemptedToAccess)
pushToolResult(
formatResponse.toolError(
formatResponse.rooIgnoreError(ignoredFileAttemptedToAccess),
),
)

break
}

this.consecutiveMistakeCount = 0

const didApprove = await askApproval("command", command)
Expand Down Expand Up @@ -3161,29 +3225,39 @@ export class Cline {

// It could be useful for cline to know if the user went from one or no file to another between messages, so we always include this context
details += "\n\n# VSCode Visible Files"
const visibleFiles = vscode.window.visibleTextEditors
const visibleFilePaths = vscode.window.visibleTextEditors
?.map((editor) => editor.document?.uri?.fsPath)
.filter(Boolean)
.map((absolutePath) => path.relative(cwd, absolutePath).toPosix())
.join("\n")
if (visibleFiles) {
details += `\n${visibleFiles}`
.map((absolutePath) => path.relative(cwd, absolutePath))

// Filter paths through rooIgnoreController
const allowedVisibleFiles = this.rooIgnoreController
? this.rooIgnoreController.filterPaths(visibleFilePaths)
: visibleFilePaths.map((p) => p.toPosix()).join("\n")

if (allowedVisibleFiles) {
details += `\n${allowedVisibleFiles}`
} else {
details += "\n(No visible files)"
}

details += "\n\n# VSCode Open Tabs"
const { maxOpenTabsContext } = (await this.providerRef.deref()?.getState()) ?? {}
const maxTabs = maxOpenTabsContext ?? 20
const openTabs = vscode.window.tabGroups.all
const openTabPaths = vscode.window.tabGroups.all
.flatMap((group) => group.tabs)
.map((tab) => (tab.input as vscode.TabInputText)?.uri?.fsPath)
.filter(Boolean)
.map((absolutePath) => path.relative(cwd, absolutePath).toPosix())
.slice(0, maxTabs)
.join("\n")
if (openTabs) {
details += `\n${openTabs}`

// Filter paths through rooIgnoreController
const allowedOpenTabs = this.rooIgnoreController
? this.rooIgnoreController.filterPaths(openTabPaths)
: openTabPaths.map((p) => p.toPosix()).join("\n")

if (allowedOpenTabs) {
details += `\n${allowedOpenTabs}`
} else {
details += "\n(No open tabs)"
}
Expand Down Expand Up @@ -3342,7 +3416,7 @@ export class Cline {
details += "(Desktop files not shown automatically. Use list_files to explore if needed.)"
} else {
const [files, didHitLimit] = await listFiles(cwd, true, 200)
const result = formatResponse.formatFilesList(cwd, files, didHitLimit)
const result = formatResponse.formatFilesList(cwd, files, didHitLimit, this.rooIgnoreController)
details += result
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/__tests__/Cline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import * as vscode from "vscode"
import * as os from "os"
import * as path from "path"

// Mock RooIgnoreController
jest.mock("../ignore/RooIgnoreController")

// Mock all MCP-related modules
jest.mock(
"@modelcontextprotocol/sdk/types.js",
Expand Down
Loading