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(
860 vec![
861 "**/.mysecrets".to_string(),
862 "**/*.privatekey".to_string(),
863 "**/*.mysensitive".to_string(),
864 ]
865 .into(),
866 );
867 });
868 });
869 });
870
871 let project = Project::test(fs.clone(), [path!("/project_root").as_ref()], cx).await;
872 let action_log = cx.new(|_| ActionLog::new(project.clone()));
873 let model = Arc::new(FakeLanguageModel::default());
874
875 // Searching for files outside the project worktree should return no results
876 let result = cx
877 .update(|cx| {
878 let input = json!({
879 "regex": "outside_function"
880 });
881 Arc::new(GrepTool)
882 .run(
883 input,
884 Arc::default(),
885 project.clone(),
886 action_log.clone(),
887 model.clone(),
888 None,
889 cx,
890 )
891 .output
892 })
893 .await;
894 let results = result.unwrap();
895 let paths = extract_paths_from_results(results.content.as_str().unwrap());
896 assert!(
897 paths.is_empty(),
898 "grep_tool should not find files outside the project worktree"
899 );
900
901 // Searching within the project should succeed
902 let result = cx
903 .update(|cx| {
904 let input = json!({
905 "regex": "main"
906 });
907 Arc::new(GrepTool)
908 .run(
909 input,
910 Arc::default(),
911 project.clone(),
912 action_log.clone(),
913 model.clone(),
914 None,
915 cx,
916 )
917 .output
918 })
919 .await;
920 let results = result.unwrap();
921 let paths = extract_paths_from_results(results.content.as_str().unwrap());
922 assert!(
923 paths.iter().any(|p| p.contains("allowed_file.rs")),
924 "grep_tool should be able to search files inside worktrees"
925 );
926
927 // Searching files that match file_scan_exclusions should return no results
928 let result = cx
929 .update(|cx| {
930 let input = json!({
931 "regex": "special_configuration"
932 });
933 Arc::new(GrepTool)
934 .run(
935 input,
936 Arc::default(),
937 project.clone(),
938 action_log.clone(),
939 model.clone(),
940 None,
941 cx,
942 )
943 .output
944 })
945 .await;
946 let results = result.unwrap();
947 let paths = extract_paths_from_results(results.content.as_str().unwrap());
948 assert!(
949 paths.is_empty(),
950 "grep_tool should not search files in .secretdir (file_scan_exclusions)"
951 );
952
953 let result = cx
954 .update(|cx| {
955 let input = json!({
956 "regex": "custom_metadata"
957 });
958 Arc::new(GrepTool)
959 .run(
960 input,
961 Arc::default(),
962 project.clone(),
963 action_log.clone(),
964 model.clone(),
965 None,
966 cx,
967 )
968 .output
969 })
970 .await;
971 let results = result.unwrap();
972 let paths = extract_paths_from_results(results.content.as_str().unwrap());
973 assert!(
974 paths.is_empty(),
975 "grep_tool should not search .mymetadata files (file_scan_exclusions)"
976 );
977
978 // Searching private files should return no results
979 let result = cx
980 .update(|cx| {
981 let input = json!({
982 "regex": "SECRET_KEY"
983 });
984 Arc::new(GrepTool)
985 .run(
986 input,
987 Arc::default(),
988 project.clone(),
989 action_log.clone(),
990 model.clone(),
991 None,
992 cx,
993 )
994 .output
995 })
996 .await;
997 let results = result.unwrap();
998 let paths = extract_paths_from_results(results.content.as_str().unwrap());
999 assert!(
1000 paths.is_empty(),
1001 "grep_tool should not search .mysecrets (private_files)"
1002 );
1003
1004 let result = cx
1005 .update(|cx| {
1006 let input = json!({
1007 "regex": "private_key_content"
1008 });
1009 Arc::new(GrepTool)
1010 .run(
1011 input,
1012 Arc::default(),
1013 project.clone(),
1014 action_log.clone(),
1015 model.clone(),
1016 None,
1017 cx,
1018 )
1019 .output
1020 })
1021 .await;
1022 let results = result.unwrap();
1023 let paths = extract_paths_from_results(results.content.as_str().unwrap());
1024 assert!(
1025 paths.is_empty(),
1026 "grep_tool should not search .privatekey files (private_files)"
1027 );
1028
1029 let result = cx
1030 .update(|cx| {
1031 let input = json!({
1032 "regex": "sensitive_data"
1033 });
1034 Arc::new(GrepTool)
1035 .run(
1036 input,
1037 Arc::default(),
1038 project.clone(),
1039 action_log.clone(),
1040 model.clone(),
1041 None,
1042 cx,
1043 )
1044 .output
1045 })
1046 .await;
1047 let results = result.unwrap();
1048 let paths = extract_paths_from_results(results.content.as_str().unwrap());
1049 assert!(
1050 paths.is_empty(),
1051 "grep_tool should not search .mysensitive files (private_files)"
1052 );
1053
1054 // Searching a normal file should still work, even with private_files configured
1055 let result = cx
1056 .update(|cx| {
1057 let input = json!({
1058 "regex": "normal_file_content"
1059 });
1060 Arc::new(GrepTool)
1061 .run(
1062 input,
1063 Arc::default(),
1064 project.clone(),
1065 action_log.clone(),
1066 model.clone(),
1067 None,
1068 cx,
1069 )
1070 .output
1071 })
1072 .await;
1073 let results = result.unwrap();
1074 let paths = extract_paths_from_results(results.content.as_str().unwrap());
1075 assert!(
1076 paths.iter().any(|p| p.contains("normal_file.rs")),
1077 "Should be able to search normal files"
1078 );
1079
1080 // Path traversal attempts with .. in include_pattern should not escape project
1081 let result = cx
1082 .update(|cx| {
1083 let input = json!({
1084 "regex": "outside_function",
1085 "include_pattern": "../outside_project/**/*.rs"
1086 });
1087 Arc::new(GrepTool)
1088 .run(
1089 input,
1090 Arc::default(),
1091 project.clone(),
1092 action_log.clone(),
1093 model.clone(),
1094 None,
1095 cx,
1096 )
1097 .output
1098 })
1099 .await;
1100 let results = result.unwrap();
1101 let paths = extract_paths_from_results(results.content.as_str().unwrap());
1102 assert!(
1103 paths.is_empty(),
1104 "grep_tool should not allow escaping project boundaries with relative paths"
1105 );
1106 }
1107
1108 #[gpui::test]
1109 async fn test_grep_with_multiple_worktree_settings(cx: &mut TestAppContext) {
1110 init_test(cx);
1111
1112 let fs = FakeFs::new(cx.executor());
1113
1114 // Create first worktree with its own private files
1115 fs.insert_tree(
1116 path!("/worktree1"),
1117 json!({
1118 ".zed": {
1119 "settings.json": r#"{
1120 "file_scan_exclusions": ["**/fixture.*"],
1121 "private_files": ["**/secret.rs"]
1122 }"#
1123 },
1124 "src": {
1125 "main.rs": "fn main() { let secret_key = \"hidden\"; }",
1126 "secret.rs": "const API_KEY: &str = \"secret_value\";",
1127 "utils.rs": "pub fn get_config() -> String { \"config\".to_string() }"
1128 },
1129 "tests": {
1130 "test.rs": "fn test_secret() { assert!(true); }",
1131 "fixture.sql": "SELECT * FROM secret_table;"
1132 }
1133 }),
1134 )
1135 .await;
1136
1137 // Create second worktree with different private files
1138 fs.insert_tree(
1139 path!("/worktree2"),
1140 json!({
1141 ".zed": {
1142 "settings.json": r#"{
1143 "file_scan_exclusions": ["**/internal.*"],
1144 "private_files": ["**/private.js", "**/data.json"]
1145 }"#
1146 },
1147 "lib": {
1148 "public.js": "export function getSecret() { return 'public'; }",
1149 "private.js": "const SECRET_KEY = \"private_value\";",
1150 "data.json": "{\"secret_data\": \"hidden\"}"
1151 },
1152 "docs": {
1153 "README.md": "# Documentation with secret info",
1154 "internal.md": "Internal secret documentation"
1155 }
1156 }),
1157 )
1158 .await;
1159
1160 // Set global settings
1161 cx.update(|cx| {
1162 SettingsStore::update_global(cx, |store, cx| {
1163 store.update_user_settings(cx, |settings| {
1164 settings.project.worktree.file_scan_exclusions =
1165 Some(vec!["**/.git".to_string(), "**/node_modules".to_string()]);
1166 settings.project.worktree.private_files =
1167 Some(vec!["**/.env".to_string()].into());
1168 });
1169 });
1170 });
1171
1172 let project = Project::test(
1173 fs.clone(),
1174 [path!("/worktree1").as_ref(), path!("/worktree2").as_ref()],
1175 cx,
1176 )
1177 .await;
1178
1179 // Wait for worktrees to be fully scanned
1180 cx.executor().run_until_parked();
1181
1182 let action_log = cx.new(|_| ActionLog::new(project.clone()));
1183 let model = Arc::new(FakeLanguageModel::default());
1184
1185 // Search for "secret" - should exclude files based on worktree-specific settings
1186 let result = cx
1187 .update(|cx| {
1188 let input = json!({
1189 "regex": "secret",
1190 "case_sensitive": false
1191 });
1192 Arc::new(GrepTool)
1193 .run(
1194 input,
1195 Arc::default(),
1196 project.clone(),
1197 action_log.clone(),
1198 model.clone(),
1199 None,
1200 cx,
1201 )
1202 .output
1203 })
1204 .await
1205 .unwrap();
1206
1207 let content = result.content.as_str().unwrap();
1208 let paths = extract_paths_from_results(content);
1209
1210 // Should find matches in non-private files
1211 assert!(
1212 paths.iter().any(|p| p.contains("main.rs")),
1213 "Should find 'secret' in worktree1/src/main.rs"
1214 );
1215 assert!(
1216 paths.iter().any(|p| p.contains("test.rs")),
1217 "Should find 'secret' in worktree1/tests/test.rs"
1218 );
1219 assert!(
1220 paths.iter().any(|p| p.contains("public.js")),
1221 "Should find 'secret' in worktree2/lib/public.js"
1222 );
1223 assert!(
1224 paths.iter().any(|p| p.contains("README.md")),
1225 "Should find 'secret' in worktree2/docs/README.md"
1226 );
1227
1228 // Should NOT find matches in private/excluded files based on worktree settings
1229 assert!(
1230 !paths.iter().any(|p| p.contains("secret.rs")),
1231 "Should not search in worktree1/src/secret.rs (local private_files)"
1232 );
1233 assert!(
1234 !paths.iter().any(|p| p.contains("fixture.sql")),
1235 "Should not search in worktree1/tests/fixture.sql (local file_scan_exclusions)"
1236 );
1237 assert!(
1238 !paths.iter().any(|p| p.contains("private.js")),
1239 "Should not search in worktree2/lib/private.js (local private_files)"
1240 );
1241 assert!(
1242 !paths.iter().any(|p| p.contains("data.json")),
1243 "Should not search in worktree2/lib/data.json (local private_files)"
1244 );
1245 assert!(
1246 !paths.iter().any(|p| p.contains("internal.md")),
1247 "Should not search in worktree2/docs/internal.md (local file_scan_exclusions)"
1248 );
1249
1250 // Test with `include_pattern` specific to one worktree
1251 let result = cx
1252 .update(|cx| {
1253 let input = json!({
1254 "regex": "secret",
1255 "include_pattern": "worktree1/**/*.rs"
1256 });
1257 Arc::new(GrepTool)
1258 .run(
1259 input,
1260 Arc::default(),
1261 project.clone(),
1262 action_log.clone(),
1263 model.clone(),
1264 None,
1265 cx,
1266 )
1267 .output
1268 })
1269 .await
1270 .unwrap();
1271
1272 let content = result.content.as_str().unwrap();
1273 let paths = extract_paths_from_results(content);
1274
1275 // Should only find matches in worktree1 *.rs files (excluding private ones)
1276 assert!(
1277 paths.iter().any(|p| p.contains("main.rs")),
1278 "Should find match in worktree1/src/main.rs"
1279 );
1280 assert!(
1281 paths.iter().any(|p| p.contains("test.rs")),
1282 "Should find match in worktree1/tests/test.rs"
1283 );
1284 assert!(
1285 !paths.iter().any(|p| p.contains("secret.rs")),
1286 "Should not find match in excluded worktree1/src/secret.rs"
1287 );
1288 assert!(
1289 paths.iter().all(|p| !p.contains("worktree2")),
1290 "Should not find any matches in worktree2"
1291 );
1292 }
1293
1294 // Helper function to extract file paths from grep results
1295 fn extract_paths_from_results(results: &str) -> Vec<String> {
1296 results
1297 .lines()
1298 .filter(|line| line.starts_with("## Matches in "))
1299 .map(|line| {
1300 line.strip_prefix("## Matches in ")
1301 .unwrap()
1302 .trim()
1303 .to_string()
1304 })
1305 .collect()
1306 }
1307}