1use crate::{AgentTool, ToolCallEventStream};
2use agent_client_protocol as acp;
3use anyhow::{Result, anyhow};
4use futures::StreamExt;
5use gpui::{App, Entity, SharedString, Task};
6use language::{OffsetRangeExt, ParseStatus, Point};
7use project::{
8 Project, WorktreeSettings,
9 search::{SearchQuery, SearchResult},
10};
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use settings::Settings;
14use std::{cmp, fmt::Write, sync::Arc};
15use util::RangeExt;
16use util::markdown::MarkdownInlineCode;
17use util::paths::PathMatcher;
18
19/// Searches the contents of files in the project with a regular expression
20///
21/// - Prefer this tool to path search when searching for symbols in the project, because you won't need to guess what path it's in.
22/// - Supports full regex syntax (eg. "log.*Error", "function\\s+\\w+", etc.)
23/// - Pass an `include_pattern` if you know how to narrow your search on the files system
24/// - Never use this tool to search for paths. Only search file contents with this tool.
25/// - Use this tool when you need to find files containing specific patterns
26/// - Results are paginated with 20 matches per page. Use the optional 'offset' parameter to request subsequent pages.
27/// - DO NOT use HTML entities solely to escape characters in the tool parameters.
28#[derive(Debug, Serialize, Deserialize, JsonSchema)]
29pub struct GrepToolInput {
30 /// A regex pattern to search for in the entire project. Note that the regex
31 /// will be parsed by the Rust `regex` crate.
32 ///
33 /// Do NOT specify a path here! This will only be matched against the code **content**.
34 pub regex: String,
35 /// A glob pattern for the paths of files to include in the search.
36 /// Supports standard glob patterns like "**/*.rs" or "src/**/*.ts".
37 /// If omitted, all files in the project will be searched.
38 pub include_pattern: Option<String>,
39 /// Optional starting position for paginated results (0-based).
40 /// When not provided, starts from the beginning.
41 #[serde(default)]
42 pub offset: u32,
43 /// Whether the regex is case-sensitive. Defaults to false (case-insensitive).
44 #[serde(default)]
45 pub case_sensitive: bool,
46}
47
48impl GrepToolInput {
49 /// Which page of search results this is.
50 pub fn page(&self) -> u32 {
51 1 + (self.offset / RESULTS_PER_PAGE)
52 }
53}
54
55const RESULTS_PER_PAGE: u32 = 20;
56
57pub struct GrepTool {
58 project: Entity<Project>,
59}
60
61impl GrepTool {
62 pub fn new(project: Entity<Project>) -> Self {
63 Self { project }
64 }
65}
66
67impl AgentTool for GrepTool {
68 type Input = GrepToolInput;
69 type Output = String;
70
71 fn name(&self) -> SharedString {
72 "grep".into()
73 }
74
75 fn kind(&self) -> acp::ToolKind {
76 acp::ToolKind::Search
77 }
78
79 fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString {
80 match input {
81 Ok(input) => {
82 let page = input.page();
83 let regex_str = MarkdownInlineCode(&input.regex);
84 let case_info = if input.case_sensitive {
85 " (case-sensitive)"
86 } else {
87 ""
88 };
89
90 if page > 1 {
91 format!("Get page {page} of search results for regex {regex_str}{case_info}")
92 } else {
93 format!("Search files for regex {regex_str}{case_info}")
94 }
95 }
96 Err(_) => "Search with regex".into(),
97 }
98 .into()
99 }
100
101 fn run(
102 self: Arc<Self>,
103 input: Self::Input,
104 _event_stream: ToolCallEventStream,
105 cx: &mut App,
106 ) -> Task<Result<Self::Output>> {
107 const CONTEXT_LINES: u32 = 2;
108 const MAX_ANCESTOR_LINES: u32 = 10;
109
110 let include_matcher = match PathMatcher::new(
111 input
112 .include_pattern
113 .as_ref()
114 .into_iter()
115 .collect::<Vec<_>>(),
116 ) {
117 Ok(matcher) => matcher,
118 Err(error) => {
119 return Task::ready(Err(anyhow!("invalid include glob pattern: {error}")));
120 }
121 };
122
123 // Exclude global file_scan_exclusions and private_files settings
124 let exclude_matcher = {
125 let global_settings = WorktreeSettings::get_global(cx);
126 let exclude_patterns = global_settings
127 .file_scan_exclusions
128 .sources()
129 .iter()
130 .chain(global_settings.private_files.sources().iter());
131
132 match PathMatcher::new(exclude_patterns) {
133 Ok(matcher) => matcher,
134 Err(error) => {
135 return Task::ready(Err(anyhow!("invalid exclude pattern: {error}")));
136 }
137 }
138 };
139
140 let query = match SearchQuery::regex(
141 &input.regex,
142 false,
143 input.case_sensitive,
144 false,
145 false,
146 include_matcher,
147 exclude_matcher,
148 true, // Always match file include pattern against *full project paths* that start with a project root.
149 None,
150 ) {
151 Ok(query) => query,
152 Err(error) => return Task::ready(Err(error)),
153 };
154
155 let results = self
156 .project
157 .update(cx, |project, cx| project.search(query, cx));
158
159 let project = self.project.downgrade();
160 cx.spawn(async move |cx| {
161 futures::pin_mut!(results);
162
163 let mut output = String::new();
164 let mut skips_remaining = input.offset;
165 let mut matches_found = 0;
166 let mut has_more_matches = false;
167
168 'outer: while let Some(SearchResult::Buffer { buffer, ranges }) = results.next().await {
169 if ranges.is_empty() {
170 continue;
171 }
172
173 let Ok((Some(path), mut parse_status)) = buffer.read_with(cx, |buffer, cx| {
174 (buffer.file().map(|file| file.full_path(cx)), buffer.parse_status())
175 }) else {
176 continue;
177 };
178
179 // Check if this file should be excluded based on its worktree settings
180 if let Ok(Some(project_path)) = project.read_with(cx, |project, cx| {
181 project.find_project_path(&path, cx)
182 })
183 && cx.update(|cx| {
184 let worktree_settings = WorktreeSettings::get(Some((&project_path).into()), cx);
185 worktree_settings.is_path_excluded(&project_path.path)
186 || worktree_settings.is_path_private(&project_path.path)
187 }).unwrap_or(false) {
188 continue;
189 }
190
191 while *parse_status.borrow() != ParseStatus::Idle {
192 parse_status.changed().await?;
193 }
194
195 let snapshot = buffer.read_with(cx, |buffer, _cx| buffer.snapshot())?;
196
197 let mut ranges = ranges
198 .into_iter()
199 .map(|range| {
200 let matched = range.to_point(&snapshot);
201 let matched_end_line_len = snapshot.line_len(matched.end.row);
202 let full_lines = Point::new(matched.start.row, 0)..Point::new(matched.end.row, matched_end_line_len);
203 let symbols = snapshot.symbols_containing(matched.start, None);
204
205 if let Some(ancestor_node) = snapshot.syntax_ancestor(full_lines.clone()) {
206 let full_ancestor_range = ancestor_node.byte_range().to_point(&snapshot);
207 let end_row = full_ancestor_range.end.row.min(full_ancestor_range.start.row + MAX_ANCESTOR_LINES);
208 let end_col = snapshot.line_len(end_row);
209 let capped_ancestor_range = Point::new(full_ancestor_range.start.row, 0)..Point::new(end_row, end_col);
210
211 if capped_ancestor_range.contains_inclusive(&full_lines) {
212 return (capped_ancestor_range, Some(full_ancestor_range), symbols)
213 }
214 }
215
216 let mut matched = matched;
217 matched.start.column = 0;
218 matched.start.row =
219 matched.start.row.saturating_sub(CONTEXT_LINES);
220 matched.end.row = cmp::min(
221 snapshot.max_point().row,
222 matched.end.row + CONTEXT_LINES,
223 );
224 matched.end.column = snapshot.line_len(matched.end.row);
225
226 (matched, None, symbols)
227 })
228 .peekable();
229
230 let mut file_header_written = false;
231
232 while let Some((mut range, ancestor_range, parent_symbols)) = ranges.next(){
233 if skips_remaining > 0 {
234 skips_remaining -= 1;
235 continue;
236 }
237
238 // We'd already found a full page of matches, and we just found one more.
239 if matches_found >= RESULTS_PER_PAGE {
240 has_more_matches = true;
241 break 'outer;
242 }
243
244 while let Some((next_range, _, _)) = ranges.peek() {
245 if range.end.row >= next_range.start.row {
246 range.end = next_range.end;
247 ranges.next();
248 } else {
249 break;
250 }
251 }
252
253 if !file_header_written {
254 writeln!(output, "\n## Matches in {}", path.display())?;
255 file_header_written = true;
256 }
257
258 let end_row = range.end.row;
259 output.push_str("\n### ");
260
261 if let Some(parent_symbols) = &parent_symbols {
262 for symbol in parent_symbols {
263 write!(output, "{} › ", symbol.text)?;
264 }
265 }
266
267 if range.start.row == end_row {
268 writeln!(output, "L{}", range.start.row + 1)?;
269 } else {
270 writeln!(output, "L{}-{}", range.start.row + 1, end_row + 1)?;
271 }
272
273 output.push_str("```\n");
274 output.extend(snapshot.text_for_range(range));
275 output.push_str("\n```\n");
276
277 if let Some(ancestor_range) = ancestor_range
278 && end_row < ancestor_range.end.row {
279 let remaining_lines = ancestor_range.end.row - end_row;
280 writeln!(output, "\n{} lines remaining in ancestor node. Read the file to see all.", remaining_lines)?;
281 }
282
283 matches_found += 1;
284 }
285 }
286
287 if matches_found == 0 {
288 Ok("No matches found".into())
289 } else if has_more_matches {
290 Ok(format!(
291 "Showing matches {}-{} (there were more matches found; use offset: {} to see next page):\n{output}",
292 input.offset + 1,
293 input.offset + matches_found,
294 input.offset + RESULTS_PER_PAGE,
295 ))
296 } else {
297 Ok(format!("Found {matches_found} matches:\n{output}"))
298 }
299 })
300 }
301}
302
303#[cfg(test)]
304mod tests {
305 use crate::ToolCallEventStream;
306
307 use super::*;
308 use gpui::{TestAppContext, UpdateGlobal};
309 use language::{Language, LanguageConfig, LanguageMatcher};
310 use project::{FakeFs, Project, WorktreeSettings};
311 use serde_json::json;
312 use settings::SettingsStore;
313 use unindent::Unindent;
314 use util::path;
315
316 #[gpui::test]
317 async fn test_grep_tool_with_include_pattern(cx: &mut TestAppContext) {
318 init_test(cx);
319 cx.executor().allow_parking();
320
321 let fs = FakeFs::new(cx.executor());
322 fs.insert_tree(
323 path!("/root"),
324 serde_json::json!({
325 "src": {
326 "main.rs": "fn main() {\n println!(\"Hello, world!\");\n}",
327 "utils": {
328 "helper.rs": "fn helper() {\n println!(\"I'm a helper!\");\n}",
329 },
330 },
331 "tests": {
332 "test_main.rs": "fn test_main() {\n assert!(true);\n}",
333 }
334 }),
335 )
336 .await;
337
338 let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
339
340 // Test with include pattern for Rust files inside the root of the project
341 let input = GrepToolInput {
342 regex: "println".to_string(),
343 include_pattern: Some("root/**/*.rs".to_string()),
344 offset: 0,
345 case_sensitive: false,
346 };
347
348 let result = run_grep_tool(input, project.clone(), cx).await;
349 assert!(result.contains("main.rs"), "Should find matches in main.rs");
350 assert!(
351 result.contains("helper.rs"),
352 "Should find matches in helper.rs"
353 );
354 assert!(
355 !result.contains("test_main.rs"),
356 "Should not include test_main.rs even though it's a .rs file (because it doesn't have the pattern)"
357 );
358
359 // Test with include pattern for src directory only
360 let input = GrepToolInput {
361 regex: "fn".to_string(),
362 include_pattern: Some("root/**/src/**".to_string()),
363 offset: 0,
364 case_sensitive: false,
365 };
366
367 let result = run_grep_tool(input, project.clone(), cx).await;
368 assert!(
369 result.contains("main.rs"),
370 "Should find matches in src/main.rs"
371 );
372 assert!(
373 result.contains("helper.rs"),
374 "Should find matches in src/utils/helper.rs"
375 );
376 assert!(
377 !result.contains("test_main.rs"),
378 "Should not include test_main.rs as it's not in src directory"
379 );
380
381 // Test with empty include pattern (should default to all files)
382 let input = GrepToolInput {
383 regex: "fn".to_string(),
384 include_pattern: None,
385 offset: 0,
386 case_sensitive: false,
387 };
388
389 let result = run_grep_tool(input, project.clone(), cx).await;
390 assert!(result.contains("main.rs"), "Should find matches in main.rs");
391 assert!(
392 result.contains("helper.rs"),
393 "Should find matches in helper.rs"
394 );
395 assert!(
396 result.contains("test_main.rs"),
397 "Should include test_main.rs"
398 );
399 }
400
401 #[gpui::test]
402 async fn test_grep_tool_with_case_sensitivity(cx: &mut TestAppContext) {
403 init_test(cx);
404 cx.executor().allow_parking();
405
406 let fs = FakeFs::new(cx.executor());
407 fs.insert_tree(
408 path!("/root"),
409 serde_json::json!({
410 "case_test.txt": "This file has UPPERCASE and lowercase text.\nUPPERCASE patterns should match only with case_sensitive: true",
411 }),
412 )
413 .await;
414
415 let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
416
417 // Test case-insensitive search (default)
418 let input = GrepToolInput {
419 regex: "uppercase".to_string(),
420 include_pattern: Some("**/*.txt".to_string()),
421 offset: 0,
422 case_sensitive: false,
423 };
424
425 let result = run_grep_tool(input, project.clone(), cx).await;
426 assert!(
427 result.contains("UPPERCASE"),
428 "Case-insensitive search should match uppercase"
429 );
430
431 // Test case-sensitive search
432 let input = GrepToolInput {
433 regex: "uppercase".to_string(),
434 include_pattern: Some("**/*.txt".to_string()),
435 offset: 0,
436 case_sensitive: true,
437 };
438
439 let result = run_grep_tool(input, project.clone(), cx).await;
440 assert!(
441 !result.contains("UPPERCASE"),
442 "Case-sensitive search should not match uppercase"
443 );
444
445 // Test case-sensitive search
446 let input = GrepToolInput {
447 regex: "LOWERCASE".to_string(),
448 include_pattern: Some("**/*.txt".to_string()),
449 offset: 0,
450 case_sensitive: true,
451 };
452
453 let result = run_grep_tool(input, project.clone(), cx).await;
454
455 assert!(
456 !result.contains("lowercase"),
457 "Case-sensitive search should match lowercase"
458 );
459
460 // Test case-sensitive search for lowercase pattern
461 let input = GrepToolInput {
462 regex: "lowercase".to_string(),
463 include_pattern: Some("**/*.txt".to_string()),
464 offset: 0,
465 case_sensitive: true,
466 };
467
468 let result = run_grep_tool(input, project.clone(), cx).await;
469 assert!(
470 result.contains("lowercase"),
471 "Case-sensitive search should match lowercase text"
472 );
473 }
474
475 /// Helper function to set up a syntax test environment
476 async fn setup_syntax_test(cx: &mut TestAppContext) -> Entity<Project> {
477 use unindent::Unindent;
478 init_test(cx);
479 cx.executor().allow_parking();
480
481 let fs = FakeFs::new(cx.executor());
482
483 // Create test file with syntax structures
484 fs.insert_tree(
485 path!("/root"),
486 serde_json::json!({
487 "test_syntax.rs": r#"
488 fn top_level_function() {
489 println!("This is at the top level");
490 }
491
492 mod feature_module {
493 pub mod nested_module {
494 pub fn nested_function(
495 first_arg: String,
496 second_arg: i32,
497 ) {
498 println!("Function in nested module");
499 println!("{first_arg}");
500 println!("{second_arg}");
501 }
502 }
503 }
504
505 struct MyStruct {
506 field1: String,
507 field2: i32,
508 }
509
510 impl MyStruct {
511 fn method_with_block() {
512 let condition = true;
513 if condition {
514 println!("Inside if block");
515 }
516 }
517
518 fn long_function() {
519 println!("Line 1");
520 println!("Line 2");
521 println!("Line 3");
522 println!("Line 4");
523 println!("Line 5");
524 println!("Line 6");
525 println!("Line 7");
526 println!("Line 8");
527 println!("Line 9");
528 println!("Line 10");
529 println!("Line 11");
530 println!("Line 12");
531 }
532 }
533
534 trait Processor {
535 fn process(&self, input: &str) -> String;
536 }
537
538 impl Processor for MyStruct {
539 fn process(&self, input: &str) -> String {
540 format!("Processed: {}", input)
541 }
542 }
543 "#.unindent().trim(),
544 }),
545 )
546 .await;
547
548 let project = Project::test(fs.clone(), [path!("/root").as_ref()], cx).await;
549
550 project.update(cx, |project, _cx| {
551 project.languages().add(rust_lang().into())
552 });
553
554 project
555 }
556
557 #[gpui::test]
558 async fn test_grep_top_level_function(cx: &mut TestAppContext) {
559 let project = setup_syntax_test(cx).await;
560
561 // Test: Line at the top level of the file
562 let input = GrepToolInput {
563 regex: "This is at the top level".to_string(),
564 include_pattern: Some("**/*.rs".to_string()),
565 offset: 0,
566 case_sensitive: false,
567 };
568
569 let result = run_grep_tool(input, project.clone(), cx).await;
570 let expected = r#"
571 Found 1 matches:
572
573 ## Matches in root/test_syntax.rs
574
575 ### fn top_level_function › L1-3
576 ```
577 fn top_level_function() {
578 println!("This is at the top level");
579 }
580 ```
581 "#
582 .unindent();
583 assert_eq!(result, expected);
584 }
585
586 #[gpui::test]
587 async fn test_grep_function_body(cx: &mut TestAppContext) {
588 let project = setup_syntax_test(cx).await;
589
590 // Test: Line inside a function body
591 let input = GrepToolInput {
592 regex: "Function in nested module".to_string(),
593 include_pattern: Some("**/*.rs".to_string()),
594 offset: 0,
595 case_sensitive: false,
596 };
597
598 let result = run_grep_tool(input, project.clone(), cx).await;
599 let expected = r#"
600 Found 1 matches:
601
602 ## Matches in root/test_syntax.rs
603
604 ### mod feature_module › pub mod nested_module › pub fn nested_function › L10-14
605 ```
606 ) {
607 println!("Function in nested module");
608 println!("{first_arg}");
609 println!("{second_arg}");
610 }
611 ```
612 "#
613 .unindent();
614 assert_eq!(result, expected);
615 }
616
617 #[gpui::test]
618 async fn test_grep_function_args_and_body(cx: &mut TestAppContext) {
619 let project = setup_syntax_test(cx).await;
620
621 // Test: Line with a function argument
622 let input = GrepToolInput {
623 regex: "second_arg".to_string(),
624 include_pattern: Some("**/*.rs".to_string()),
625 offset: 0,
626 case_sensitive: false,
627 };
628
629 let result = run_grep_tool(input, project.clone(), cx).await;
630 let expected = r#"
631 Found 1 matches:
632
633 ## Matches in root/test_syntax.rs
634
635 ### mod feature_module › pub mod nested_module › pub fn nested_function › L7-14
636 ```
637 pub fn nested_function(
638 first_arg: String,
639 second_arg: i32,
640 ) {
641 println!("Function in nested module");
642 println!("{first_arg}");
643 println!("{second_arg}");
644 }
645 ```
646 "#
647 .unindent();
648 assert_eq!(result, expected);
649 }
650
651 #[gpui::test]
652 async fn test_grep_if_block(cx: &mut TestAppContext) {
653 use unindent::Unindent;
654 let project = setup_syntax_test(cx).await;
655
656 // Test: Line inside an if block
657 let input = GrepToolInput {
658 regex: "Inside if block".to_string(),
659 include_pattern: Some("**/*.rs".to_string()),
660 offset: 0,
661 case_sensitive: false,
662 };
663
664 let result = run_grep_tool(input, project.clone(), cx).await;
665 let expected = r#"
666 Found 1 matches:
667
668 ## Matches in root/test_syntax.rs
669
670 ### impl MyStruct › fn method_with_block › L26-28
671 ```
672 if condition {
673 println!("Inside if block");
674 }
675 ```
676 "#
677 .unindent();
678 assert_eq!(result, expected);
679 }
680
681 #[gpui::test]
682 async fn test_grep_long_function_top(cx: &mut TestAppContext) {
683 use unindent::Unindent;
684 let project = setup_syntax_test(cx).await;
685
686 // Test: Line in the middle of a long function - should show message about remaining lines
687 let input = GrepToolInput {
688 regex: "Line 5".to_string(),
689 include_pattern: Some("**/*.rs".to_string()),
690 offset: 0,
691 case_sensitive: false,
692 };
693
694 let result = run_grep_tool(input, project.clone(), cx).await;
695 let expected = r#"
696 Found 1 matches:
697
698 ## Matches in root/test_syntax.rs
699
700 ### impl MyStruct › fn long_function › L31-41
701 ```
702 fn long_function() {
703 println!("Line 1");
704 println!("Line 2");
705 println!("Line 3");
706 println!("Line 4");
707 println!("Line 5");
708 println!("Line 6");
709 println!("Line 7");
710 println!("Line 8");
711 println!("Line 9");
712 println!("Line 10");
713 ```
714
715 3 lines remaining in ancestor node. Read the file to see all.
716 "#
717 .unindent();
718 assert_eq!(result, expected);
719 }
720
721 #[gpui::test]
722 async fn test_grep_long_function_bottom(cx: &mut TestAppContext) {
723 use unindent::Unindent;
724 let project = setup_syntax_test(cx).await;
725
726 // Test: Line in the long function
727 let input = GrepToolInput {
728 regex: "Line 12".to_string(),
729 include_pattern: Some("**/*.rs".to_string()),
730 offset: 0,
731 case_sensitive: false,
732 };
733
734 let result = run_grep_tool(input, project.clone(), cx).await;
735 let expected = r#"
736 Found 1 matches:
737
738 ## Matches in root/test_syntax.rs
739
740 ### impl MyStruct › fn long_function › L41-45
741 ```
742 println!("Line 10");
743 println!("Line 11");
744 println!("Line 12");
745 }
746 }
747 ```
748 "#
749 .unindent();
750 assert_eq!(result, expected);
751 }
752
753 async fn run_grep_tool(
754 input: GrepToolInput,
755 project: Entity<Project>,
756 cx: &mut TestAppContext,
757 ) -> String {
758 let tool = Arc::new(GrepTool { project });
759 let task = cx.update(|cx| tool.run(input, ToolCallEventStream::test().0, cx));
760
761 match task.await {
762 Ok(result) => {
763 if cfg!(windows) {
764 result.replace("root\\", "root/")
765 } else {
766 result
767 }
768 }
769 Err(e) => panic!("Failed to run grep tool: {}", e),
770 }
771 }
772
773 fn init_test(cx: &mut TestAppContext) {
774 cx.update(|cx| {
775 let settings_store = SettingsStore::test(cx);
776 cx.set_global(settings_store);
777 language::init(cx);
778 Project::init_settings(cx);
779 });
780 }
781
782 fn rust_lang() -> Language {
783 Language::new(
784 LanguageConfig {
785 name: "Rust".into(),
786 matcher: LanguageMatcher {
787 path_suffixes: vec!["rs".to_string()],
788 ..Default::default()
789 },
790 ..Default::default()
791 },
792 Some(tree_sitter_rust::LANGUAGE.into()),
793 )
794 .with_outline_query(include_str!("../../../languages/src/rust/outline.scm"))
795 .unwrap()
796 }
797
798 #[gpui::test]
799 async fn test_grep_security_boundaries(cx: &mut TestAppContext) {
800 init_test(cx);
801
802 let fs = FakeFs::new(cx.executor());
803
804 fs.insert_tree(
805 path!("/"),
806 json!({
807 "project_root": {
808 "allowed_file.rs": "fn main() { println!(\"This file is in the project\"); }",
809 ".mysecrets": "SECRET_KEY=abc123\nfn secret() { /* private */ }",
810 ".secretdir": {
811 "config": "fn special_configuration() { /* excluded */ }"
812 },
813 ".mymetadata": "fn custom_metadata() { /* excluded */ }",
814 "subdir": {
815 "normal_file.rs": "fn normal_file_content() { /* Normal */ }",
816 "special.privatekey": "fn private_key_content() { /* private */ }",
817 "data.mysensitive": "fn sensitive_data() { /* private */ }"
818 }
819 },
820 "outside_project": {
821 "sensitive_file.rs": "fn outside_function() { /* This file is outside the project */ }"
822 }
823 }),
824 )
825 .await;
826
827 cx.update(|cx| {
828 use gpui::UpdateGlobal;
829 use project::WorktreeSettings;
830 use settings::SettingsStore;
831 SettingsStore::update_global(cx, |store, cx| {
832 store.update_user_settings::<WorktreeSettings>(cx, |settings| {
833 settings.file_scan_exclusions = Some(vec![
834 "**/.secretdir".to_string(),
835 "**/.mymetadata".to_string(),
836 ]);
837 settings.private_files = Some(vec![
838 "**/.mysecrets".to_string(),
839 "**/*.privatekey".to_string(),
840 "**/*.mysensitive".to_string(),
841 ]);
842 });
843 });
844 });
845
846 let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
847
848 // Searching for files outside the project worktree should return no results
849 let result = run_grep_tool(
850 GrepToolInput {
851 regex: "outside_function".to_string(),
852 include_pattern: None,
853 offset: 0,
854 case_sensitive: false,
855 },
856 project.clone(),
857 cx,
858 )
859 .await;
860 let paths = extract_paths_from_results(&result);
861 assert!(
862 paths.is_empty(),
863 "grep_tool should not find files outside the project worktree"
864 );
865
866 // Searching within the project should succeed
867 let result = run_grep_tool(
868 GrepToolInput {
869 regex: "main".to_string(),
870 include_pattern: None,
871 offset: 0,
872 case_sensitive: false,
873 },
874 project.clone(),
875 cx,
876 )
877 .await;
878 let paths = extract_paths_from_results(&result);
879 assert!(
880 paths.iter().any(|p| p.contains("allowed_file.rs")),
881 "grep_tool should be able to search files inside worktrees"
882 );
883
884 // Searching files that match file_scan_exclusions should return no results
885 let result = run_grep_tool(
886 GrepToolInput {
887 regex: "special_configuration".to_string(),
888 include_pattern: None,
889 offset: 0,
890 case_sensitive: false,
891 },
892 project.clone(),
893 cx,
894 )
895 .await;
896 let paths = extract_paths_from_results(&result);
897 assert!(
898 paths.is_empty(),
899 "grep_tool should not search files in .secretdir (file_scan_exclusions)"
900 );
901
902 let result = run_grep_tool(
903 GrepToolInput {
904 regex: "custom_metadata".to_string(),
905 include_pattern: None,
906 offset: 0,
907 case_sensitive: false,
908 },
909 project.clone(),
910 cx,
911 )
912 .await;
913 let paths = extract_paths_from_results(&result);
914 assert!(
915 paths.is_empty(),
916 "grep_tool should not search .mymetadata files (file_scan_exclusions)"
917 );
918
919 // Searching private files should return no results
920 let result = run_grep_tool(
921 GrepToolInput {
922 regex: "SECRET_KEY".to_string(),
923 include_pattern: None,
924 offset: 0,
925 case_sensitive: false,
926 },
927 project.clone(),
928 cx,
929 )
930 .await;
931 let paths = extract_paths_from_results(&result);
932 assert!(
933 paths.is_empty(),
934 "grep_tool should not search .mysecrets (private_files)"
935 );
936
937 let result = run_grep_tool(
938 GrepToolInput {
939 regex: "private_key_content".to_string(),
940 include_pattern: None,
941 offset: 0,
942 case_sensitive: false,
943 },
944 project.clone(),
945 cx,
946 )
947 .await;
948 let paths = extract_paths_from_results(&result);
949
950 assert!(
951 paths.is_empty(),
952 "grep_tool should not search .privatekey files (private_files)"
953 );
954
955 let result = run_grep_tool(
956 GrepToolInput {
957 regex: "sensitive_data".to_string(),
958 include_pattern: None,
959 offset: 0,
960 case_sensitive: false,
961 },
962 project.clone(),
963 cx,
964 )
965 .await;
966 let paths = extract_paths_from_results(&result);
967 assert!(
968 paths.is_empty(),
969 "grep_tool should not search .mysensitive files (private_files)"
970 );
971
972 // Searching a normal file should still work, even with private_files configured
973 let result = run_grep_tool(
974 GrepToolInput {
975 regex: "normal_file_content".to_string(),
976 include_pattern: None,
977 offset: 0,
978 case_sensitive: false,
979 },
980 project.clone(),
981 cx,
982 )
983 .await;
984 let paths = extract_paths_from_results(&result);
985 assert!(
986 paths.iter().any(|p| p.contains("normal_file.rs")),
987 "Should be able to search normal files"
988 );
989
990 // Path traversal attempts with .. in include_pattern should not escape project
991 let result = run_grep_tool(
992 GrepToolInput {
993 regex: "outside_function".to_string(),
994 include_pattern: Some("../outside_project/**/*.rs".to_string()),
995 offset: 0,
996 case_sensitive: false,
997 },
998 project.clone(),
999 cx,
1000 )
1001 .await;
1002 let paths = extract_paths_from_results(&result);
1003 assert!(
1004 paths.is_empty(),
1005 "grep_tool should not allow escaping project boundaries with relative paths"
1006 );
1007 }
1008
1009 #[gpui::test]
1010 async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1011 init_test(cx);
1012
1013 let fs = FakeFs::new(cx.executor());
1014
1015 // Create first worktree with its own private files
1016 fs.insert_tree(
1017 path!("/worktree1"),
1018 json!({
1019 ".zed": {
1020 "settings.json": r#"{
1021 "file_scan_exclusions": ["**/fixture.*"],
1022 "private_files": ["**/secret.rs"]
1023 }"#
1024 },
1025 "src": {
1026 "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1027 "secret.rs": "const API_KEY: &str = \"secret_value\";",
1028 "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1029 },
1030 "tests": {
1031 "test.rs": "fn test_secret() { assert!(true); }",
1032 "fixture.sql": "SELECT * FROM secret_table;"
1033 }
1034 }),
1035 )
1036 .await;
1037
1038 // Create second worktree with different private files
1039 fs.insert_tree(
1040 path!("/worktree2"),
1041 json!({
1042 ".zed": {
1043 "settings.json": r#"{
1044 "file_scan_exclusions": ["**/internal.*"],
1045 "private_files": ["**/private.js", "**/data.json"]
1046 }"#
1047 },
1048 "lib": {
1049 "public.js": "export function getSecret() { return 'public'; }",
1050 "private.js": "const SECRET_KEY = \"private_value\";",
1051 "data.json": "{\"secret_data\": \"hidden\"}"
1052 },
1053 "docs": {
1054 "README.md": "# Documentation with secret info",
1055 "internal.md": "Internal secret documentation"
1056 }
1057 }),
1058 )
1059 .await;
1060
1061 // Set global settings
1062 cx.update(|cx| {
1063 SettingsStore::update_global(cx, |store, cx| {
1064 store.update_user_settings::<WorktreeSettings>(cx, |settings| {
1065 settings.file_scan_exclusions =
1066 Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1067 settings.private_files = Some(vec!["**/.env".to_string()]);
1068 });
1069 });
1070 });
1071
1072 let project = Project::test(
1073 fs.clone(),
1074 [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1075 cx,
1076 )
1077 .await;
1078
1079 // Wait for worktrees to be fully scanned
1080 cx.executor().run_until_parked();
1081
1082 // Search for "secret" - should exclude files based on worktree-specific settings
1083 let result = run_grep_tool(
1084 GrepToolInput {
1085 regex: "secret".to_string(),
1086 include_pattern: None,
1087 offset: 0,
1088 case_sensitive: false,
1089 },
1090 project.clone(),
1091 cx,
1092 )
1093 .await;
1094 let paths = extract_paths_from_results(&result);
1095
1096 // Should find matches in non-private files
1097 assert!(
1098 paths.iter().any(|p| p.contains("main.rs")),
1099 "Should find 'secret' in worktree1/src/main.rs"
1100 );
1101 assert!(
1102 paths.iter().any(|p| p.contains("test.rs")),
1103 "Should find 'secret' in worktree1/tests/test.rs"
1104 );
1105 assert!(
1106 paths.iter().any(|p| p.contains("public.js")),
1107 "Should find 'secret' in worktree2/lib/public.js"
1108 );
1109 assert!(
1110 paths.iter().any(|p| p.contains("README.md")),
1111 "Should find 'secret' in worktree2/docs/README.md"
1112 );
1113
1114 // Should NOT find matches in private/excluded files based on worktree settings
1115 assert!(
1116 !paths.iter().any(|p| p.contains("secret.rs")),
1117 "Should not search in worktree1/src/secret.rs (local private_files)"
1118 );
1119 assert!(
1120 !paths.iter().any(|p| p.contains("fixture.sql")),
1121 "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1122 );
1123 assert!(
1124 !paths.iter().any(|p| p.contains("private.js")),
1125 "Should not search in worktree2/lib/private.js (local private_files)"
1126 );
1127 assert!(
1128 !paths.iter().any(|p| p.contains("data.json")),
1129 "Should not search in worktree2/lib/data.json (local private_files)"
1130 );
1131 assert!(
1132 !paths.iter().any(|p| p.contains("internal.md")),
1133 "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1134 );
1135
1136 // Test with `include_pattern` specific to one worktree
1137 let result = run_grep_tool(
1138 GrepToolInput {
1139 regex: "secret".to_string(),
1140 include_pattern: Some("worktree1/**/*.rs".to_string()),
1141 offset: 0,
1142 case_sensitive: false,
1143 },
1144 project.clone(),
1145 cx,
1146 )
1147 .await;
1148
1149 let paths = extract_paths_from_results(&result);
1150
1151 // Should only find matches in worktree1 *.rs files (excluding private ones)
1152 assert!(
1153 paths.iter().any(|p| p.contains("main.rs")),
1154 "Should find match in worktree1/src/main.rs"
1155 );
1156 assert!(
1157 paths.iter().any(|p| p.contains("test.rs")),
1158 "Should find match in worktree1/tests/test.rs"
1159 );
1160 assert!(
1161 !paths.iter().any(|p| p.contains("secret.rs")),
1162 "Should not find match in excluded worktree1/src/secret.rs"
1163 );
1164 assert!(
1165 paths.iter().all(|p| !p.contains("worktree2")),
1166 "Should not find any matches in worktree2"
1167 );
1168 }
1169
1170 // Helper function to extract file paths from grep results
1171 fn extract_paths_from_results(results: &str) -> Vec<String> {
1172 results
1173 .lines()
1174 .filter(|line| line.starts_with("## Matches in "))
1175 .map(|line| {
1176 line.strip_prefix("## Matches in ")
1177 .unwrap()
1178 .trim()
1179 .to_string()
1180 })
1181 .collect()
1182 }
1183}