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