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