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