Skip to main content

reflex/parsers/
go.rs

1//! Go language parser using Tree-sitter
2//!
3//! Extracts symbols from Go source code:
4//! - Functions (func)
5//! - Types (struct, interface)
6//! - Methods (with receiver type)
7//! - Constants (const declarations and blocks)
8//! - Variables (var declarations and short declarations with :=)
9//! - Packages/Imports
10
11use crate::models::{Language, SearchResult, Span, SymbolKind};
12use anyhow::{Context, Result};
13use streaming_iterator::StreamingIterator;
14use tree_sitter::{Parser, QueryCursor};
15
16/// Parse Go source code and extract symbols
17pub fn parse(path: &str, source: &str) -> Result<Vec<SearchResult>> {
18    let mut parser = Parser::new();
19    let language = tree_sitter_go::LANGUAGE;
20
21    parser
22        .set_language(&language.into())
23        .context("Failed to set Go language")?;
24
25    let tree = parser
26        .parse(source, None)
27        .context("Failed to parse Go source")?;
28
29    let root_node = tree.root_node();
30    let table = SYMBOL_QUERIES.run(&language.into(), &root_node, source)?;
31
32    let mut symbols = Vec::new();
33
34    // Extract different types of symbols using Tree-sitter queries
35    symbols.extend(extract_functions(source, &table)?);
36    symbols.extend(extract_types(source, &table)?);
37    symbols.extend(extract_interfaces(source, &table)?);
38    symbols.extend(extract_methods(source, &table)?);
39    symbols.extend(extract_constants(source, &table)?);
40    symbols.extend(extract_variables(source, &table)?);
41
42    // Add file path to all symbols
43    for symbol in &mut symbols {
44        symbol.path = path.to_string();
45        symbol.lang = Language::Go;
46    }
47
48    Ok(symbols)
49}
50
51/// Extract function declarations
52fn extract_functions(
53    source: &str,
54    table: &crate::parsers::MatchTable<'_>,
55) -> Result<Vec<SearchResult>> {
56    extract_symbols(source, table, 0, SymbolKind::Function, None)
57}
58
59/// Extract type declarations (structs)
60fn extract_types(
61    source: &str,
62    table: &crate::parsers::MatchTable<'_>,
63) -> Result<Vec<SearchResult>> {
64    extract_symbols(source, table, 1, SymbolKind::Struct, None)
65}
66
67/// Extract interface declarations
68fn extract_interfaces(
69    source: &str,
70    table: &crate::parsers::MatchTable<'_>,
71) -> Result<Vec<SearchResult>> {
72    extract_symbols(source, table, 2, SymbolKind::Interface, None)
73}
74
75/// Extract method declarations (functions with receivers)
76fn extract_methods(
77    source: &str,
78    table: &crate::parsers::MatchTable<'_>,
79) -> Result<Vec<SearchResult>> {
80    let query = table.query();
81    let matches = table.sub(3);
82
83    let mut symbols = Vec::new();
84
85    for match_ in matches {
86        let mut receiver_type = None;
87        let mut method_name = None;
88        let mut method_node = None;
89
90        for capture in &match_.captures {
91            let capture_name: &str = query.capture_names()[capture.index as usize];
92            match capture_name {
93                "receiver_type" => {
94                    receiver_type = Some(
95                        capture
96                            .node
97                            .utf8_text(source.as_bytes())
98                            .unwrap_or("")
99                            .to_string(),
100                    );
101                }
102                "method_name" => {
103                    method_name = Some(
104                        capture
105                            .node
106                            .utf8_text(source.as_bytes())
107                            .unwrap_or("")
108                            .to_string(),
109                    );
110                }
111                "method" => {
112                    method_node = Some(capture.node);
113                }
114                _ => {}
115            }
116        }
117
118        if let (Some(receiver_type), Some(method_name), Some(node)) =
119            (receiver_type, method_name, method_node)
120        {
121            // Clean up receiver type (remove * if pointer)
122            let clean_receiver = receiver_type.trim_start_matches('*');
123            let scope = format!("type {}", clean_receiver);
124            let span = node_to_span(&node);
125            let preview = extract_preview(source, &node);
126
127            symbols.push(SearchResult::new(
128                String::new(),
129                Language::Go,
130                SymbolKind::Method,
131                Some(method_name),
132                span,
133                Some(scope),
134                preview,
135            ));
136        }
137    }
138
139    Ok(symbols)
140}
141
142/// Extract constant declarations
143fn extract_constants(
144    source: &str,
145    table: &crate::parsers::MatchTable<'_>,
146) -> Result<Vec<SearchResult>> {
147    extract_symbols(source, table, 4, SymbolKind::Constant, None)
148}
149
150/// Extract variable declarations (var and := short declarations)
151fn extract_variables(
152    source: &str,
153    table: &crate::parsers::MatchTable<'_>,
154) -> Result<Vec<SearchResult>> {
155    // Match both var_spec and short_var_declaration to capture all variable declarations
156
157    let query = table.query();
158    let matches = table.sub(5);
159
160    let mut symbols = Vec::new();
161
162    for match_ in matches {
163        let mut name = None;
164        let mut decl_node = None;
165
166        for capture in &match_.captures {
167            let capture_name: &str = query.capture_names()[capture.index as usize];
168            match capture_name {
169                "name" => {
170                    name = Some(
171                        capture
172                            .node
173                            .utf8_text(source.as_bytes())
174                            .unwrap_or("")
175                            .to_string(),
176                    );
177                }
178                "var" | "short_var" => {
179                    decl_node = Some(capture.node);
180                }
181                _ => {}
182            }
183        }
184
185        if let (Some(name), Some(node)) = (name, decl_node) {
186            let span = node_to_span(&node);
187            let preview = extract_preview(source, &node);
188
189            symbols.push(SearchResult::new(
190                String::new(),
191                Language::Go,
192                SymbolKind::Variable,
193                Some(name),
194                span,
195                None,
196                preview,
197            ));
198        }
199    }
200
201    Ok(symbols)
202}
203
204/// Generic symbol extraction helper
205fn extract_symbols(
206    source: &str,
207    table: &crate::parsers::MatchTable<'_>,
208    sub: usize,
209    kind: SymbolKind,
210    scope: Option<String>,
211) -> Result<Vec<SearchResult>> {
212    let query = table.query();
213    let matches = table.sub(sub);
214
215    let mut symbols = Vec::new();
216
217    for match_ in matches {
218        // Find the name capture and the full node
219        let mut name = None;
220        let mut full_node = None;
221
222        for capture in &match_.captures {
223            let capture_name: &str = query.capture_names()[capture.index as usize];
224            if capture_name == "name" {
225                name = Some(
226                    capture
227                        .node
228                        .utf8_text(source.as_bytes())
229                        .unwrap_or("")
230                        .to_string(),
231                );
232            } else {
233                // Assume any other capture is the full node
234                full_node = Some(capture.node);
235            }
236        }
237
238        if let (Some(name), Some(node)) = (name, full_node) {
239            let span = node_to_span(&node);
240            let preview = extract_preview(source, &node);
241
242            symbols.push(SearchResult::new(
243                String::new(),
244                Language::Go,
245                kind.clone(),
246                Some(name),
247                span,
248                scope.clone(),
249                preview,
250            ));
251        }
252    }
253
254    Ok(symbols)
255}
256
257/// Convert a Tree-sitter node to a Span
258fn node_to_span(node: &tree_sitter::Node) -> Span {
259    let start = node.start_position();
260    let end = node.end_position();
261
262    Span::new(
263        start.row + 1, // Convert 0-indexed to 1-indexed
264        start.column,
265        end.row + 1,
266        end.column,
267    )
268}
269
270/// Extract a preview (7 lines) around the symbol
271fn extract_preview(source: &str, node: &tree_sitter::Node) -> String {
272    // Starts at the node, not at byte 0: see `crate::parsers::preview`.
273    crate::parsers::preview::extract_preview_for_node(source, node)
274}
275
276#[cfg(test)]
277mod tests {
278    use super::*;
279
280    #[test]
281    fn test_parse_function() {
282        let source = r#"
283package main
284
285func helloWorld() string {
286    return "Hello, world!"
287}
288        "#;
289
290        let symbols = parse("test.go", source).unwrap();
291        assert_eq!(symbols.len(), 1);
292        assert_eq!(symbols[0].symbol.as_deref(), Some("helloWorld"));
293        assert!(matches!(symbols[0].kind, SymbolKind::Function));
294    }
295
296    #[test]
297    fn test_parse_struct() {
298        let source = r#"
299package main
300
301type User struct {
302    Name string
303    Age  int
304}
305        "#;
306
307        let symbols = parse("test.go", source).unwrap();
308        assert_eq!(symbols.len(), 1);
309        assert_eq!(symbols[0].symbol.as_deref(), Some("User"));
310        assert!(matches!(symbols[0].kind, SymbolKind::Struct));
311    }
312
313    #[test]
314    fn test_parse_interface() {
315        let source = r#"
316package main
317
318type Reader interface {
319    Read(p []byte) (n int, err error)
320}
321        "#;
322
323        let symbols = parse("test.go", source).unwrap();
324        assert_eq!(symbols.len(), 1);
325        assert_eq!(symbols[0].symbol.as_deref(), Some("Reader"));
326        assert!(matches!(symbols[0].kind, SymbolKind::Interface));
327    }
328
329    #[test]
330    fn test_parse_method() {
331        let source = r#"
332package main
333
334type User struct {
335    Name string
336}
337
338func (u *User) GetName() string {
339    return u.Name
340}
341
342func (u User) SetName(name string) {
343    u.Name = name
344}
345        "#;
346
347        let symbols = parse("test.go", source).unwrap();
348
349        let method_symbols: Vec<_> = symbols
350            .iter()
351            .filter(|s| matches!(s.kind, SymbolKind::Method))
352            .collect();
353
354        assert_eq!(method_symbols.len(), 2);
355        assert!(
356            method_symbols
357                .iter()
358                .any(|s| s.symbol.as_deref() == Some("GetName"))
359        );
360        assert!(
361            method_symbols
362                .iter()
363                .any(|s| s.symbol.as_deref() == Some("SetName"))
364        );
365
366        // Check scope
367        for _method in method_symbols {
368            // Removed: scope field no longer exists: assert_eq!(method.scope.as_ref().unwrap(), "type User");
369        }
370    }
371
372    #[test]
373    fn test_parse_constants() {
374        let source = r#"
375package main
376
377const MaxSize = 100
378const DefaultTimeout = 30
379
380const (
381    StatusActive   = 1
382    StatusInactive = 2
383)
384        "#;
385
386        let symbols = parse("test.go", source).unwrap();
387
388        let const_symbols: Vec<_> = symbols
389            .iter()
390            .filter(|s| matches!(s.kind, SymbolKind::Constant))
391            .collect();
392
393        assert_eq!(const_symbols.len(), 4);
394        assert!(
395            const_symbols
396                .iter()
397                .any(|s| s.symbol.as_deref() == Some("MaxSize"))
398        );
399        assert!(
400            const_symbols
401                .iter()
402                .any(|s| s.symbol.as_deref() == Some("DefaultTimeout"))
403        );
404        assert!(
405            const_symbols
406                .iter()
407                .any(|s| s.symbol.as_deref() == Some("StatusActive"))
408        );
409        assert!(
410            const_symbols
411                .iter()
412                .any(|s| s.symbol.as_deref() == Some("StatusInactive"))
413        );
414    }
415
416    #[test]
417    fn test_parse_variables() {
418        let source = r#"
419package main
420
421var GlobalConfig Config
422var (
423    Logger  *log.Logger
424    Version = "1.0.0"
425)
426        "#;
427
428        let symbols = parse("test.go", source).unwrap();
429
430        let var_symbols: Vec<_> = symbols
431            .iter()
432            .filter(|s| matches!(s.kind, SymbolKind::Variable))
433            .collect();
434
435        assert_eq!(var_symbols.len(), 3);
436        assert!(
437            var_symbols
438                .iter()
439                .any(|s| s.symbol.as_deref() == Some("GlobalConfig"))
440        );
441        assert!(
442            var_symbols
443                .iter()
444                .any(|s| s.symbol.as_deref() == Some("Logger"))
445        );
446        assert!(
447            var_symbols
448                .iter()
449                .any(|s| s.symbol.as_deref() == Some("Version"))
450        );
451    }
452
453    #[test]
454    fn test_parse_mixed_symbols() {
455        let source = r#"
456package main
457
458const DefaultPort = 8080
459
460type Server struct {
461    Port int
462}
463
464type Handler interface {
465    Handle(req *Request) error
466}
467
468func (s *Server) Start() error {
469    return nil
470}
471
472func NewServer(port int) *Server {
473    return &Server{Port: port}
474}
475
476var globalServer *Server
477        "#;
478
479        let symbols = parse("test.go", source).unwrap();
480
481        // Should find: const, struct, interface, method, function, var
482        assert!(symbols.len() >= 6);
483
484        let kinds: Vec<&SymbolKind> = symbols.iter().map(|s| &s.kind).collect();
485        assert!(kinds.contains(&&SymbolKind::Constant));
486        assert!(kinds.contains(&&SymbolKind::Struct));
487        assert!(kinds.contains(&&SymbolKind::Interface));
488        assert!(kinds.contains(&&SymbolKind::Method));
489        assert!(kinds.contains(&&SymbolKind::Function));
490        assert!(kinds.contains(&&SymbolKind::Variable));
491    }
492
493    #[test]
494    fn test_parse_multiple_methods() {
495        let source = r#"
496package main
497
498type Calculator struct{}
499
500func (c *Calculator) Add(a, b int) int {
501    return a + b
502}
503
504func (c *Calculator) Subtract(a, b int) int {
505    return a - b
506}
507
508func (c *Calculator) Multiply(a, b int) int {
509    return a * b
510}
511        "#;
512
513        let symbols = parse("test.go", source).unwrap();
514
515        let method_symbols: Vec<_> = symbols
516            .iter()
517            .filter(|s| matches!(s.kind, SymbolKind::Method))
518            .collect();
519
520        assert_eq!(method_symbols.len(), 3);
521        assert!(
522            method_symbols
523                .iter()
524                .any(|s| s.symbol.as_deref() == Some("Add"))
525        );
526        assert!(
527            method_symbols
528                .iter()
529                .any(|s| s.symbol.as_deref() == Some("Subtract"))
530        );
531        assert!(
532            method_symbols
533                .iter()
534                .any(|s| s.symbol.as_deref() == Some("Multiply"))
535        );
536    }
537
538    #[test]
539    fn test_parse_type_alias() {
540        let source = r#"
541package main
542
543type UserID string
544type Age int
545
546type Config struct {
547    Host string
548    Port int
549}
550        "#;
551
552        let symbols = parse("test.go", source).unwrap();
553
554        // Should find the Config struct (type aliases UserID and Age are type_spec but not struct_type)
555        let struct_symbols: Vec<_> = symbols
556            .iter()
557            .filter(|s| matches!(s.kind, SymbolKind::Struct))
558            .collect();
559
560        assert_eq!(struct_symbols.len(), 1);
561        assert_eq!(struct_symbols[0].symbol.as_deref(), Some("Config"));
562    }
563
564    #[test]
565    fn test_parse_embedded_interface() {
566        let source = r#"
567package main
568
569type Reader interface {
570    Read(p []byte) (n int, err error)
571}
572
573type Writer interface {
574    Write(p []byte) (n int, err error)
575}
576
577type ReadWriter interface {
578    Reader
579    Writer
580}
581        "#;
582
583        let symbols = parse("test.go", source).unwrap();
584
585        let interface_symbols: Vec<_> = symbols
586            .iter()
587            .filter(|s| matches!(s.kind, SymbolKind::Interface))
588            .collect();
589
590        assert_eq!(interface_symbols.len(), 3);
591        assert!(
592            interface_symbols
593                .iter()
594                .any(|s| s.symbol.as_deref() == Some("Reader"))
595        );
596        assert!(
597            interface_symbols
598                .iter()
599                .any(|s| s.symbol.as_deref() == Some("Writer"))
600        );
601        assert!(
602            interface_symbols
603                .iter()
604                .any(|s| s.symbol.as_deref() == Some("ReadWriter"))
605        );
606    }
607
608    #[test]
609    fn test_local_variables_included() {
610        let source = r#"
611package main
612
613var globalCount int = 10
614
615func calculate(x int) int {
616    localVar := x * 2
617    var anotherLocal int = 5
618    return localVar + anotherLocal
619}
620        "#;
621
622        let symbols = parse("test.go", source).unwrap();
623
624        let var_symbols: Vec<_> = symbols
625            .iter()
626            .filter(|s| matches!(s.kind, SymbolKind::Variable))
627            .collect();
628
629        // Should find globalCount, localVar (short declaration), and anotherLocal (var declaration)
630        assert_eq!(var_symbols.len(), 3);
631        assert!(
632            var_symbols
633                .iter()
634                .any(|s| s.symbol.as_deref() == Some("globalCount"))
635        );
636        assert!(
637            var_symbols
638                .iter()
639                .any(|s| s.symbol.as_deref() == Some("localVar"))
640        );
641        assert!(
642            var_symbols
643                .iter()
644                .any(|s| s.symbol.as_deref() == Some("anotherLocal"))
645        );
646    }
647
648    #[test]
649    fn test_extract_go_imports() {
650        let source = r#"package main
651
652import (
653	"fmt"
654	"encoding/json"
655	"github.com/gin-gonic/gin"
656	"myproject/internal/models"
657)
658
659func main() {
660	fmt.Println("Hello")
661}
662"#;
663
664        let deps = GoDependencyExtractor::extract_dependencies(source).unwrap();
665
666        assert_eq!(deps.len(), 4, "Should extract 4 import statements");
667        assert!(deps.iter().any(|d| d.imported_path == "fmt"));
668        assert!(deps.iter().any(|d| d.imported_path == "encoding/json"));
669        assert!(
670            deps.iter()
671                .any(|d| d.imported_path == "github.com/gin-gonic/gin")
672        );
673        assert!(
674            deps.iter()
675                .any(|d| d.imported_path == "myproject/internal/models")
676        );
677
678        // Check stdlib classification
679        let fmt_dep = deps.iter().find(|d| d.imported_path == "fmt").unwrap();
680        assert!(
681            matches!(fmt_dep.import_type, ImportType::Stdlib),
682            "fmt should be classified as Stdlib"
683        );
684
685        let json_dep = deps
686            .iter()
687            .find(|d| d.imported_path == "encoding/json")
688            .unwrap();
689        assert!(
690            matches!(json_dep.import_type, ImportType::Stdlib),
691            "encoding/json should be classified as Stdlib"
692        );
693
694        // Check external classification
695        let gin_dep = deps
696            .iter()
697            .find(|d| d.imported_path == "github.com/gin-gonic/gin")
698            .unwrap();
699        assert!(
700            matches!(gin_dep.import_type, ImportType::External),
701            "github.com/gin-gonic/gin should be classified as External"
702        );
703
704        // Check myproject classification (ambiguous but should be External)
705        let models_dep = deps
706            .iter()
707            .find(|d| d.imported_path == "myproject/internal/models")
708            .unwrap();
709        assert!(
710            matches!(models_dep.import_type, ImportType::External),
711            "myproject/internal/models should be classified as External"
712        );
713    }
714
715    #[test]
716    fn test_extract_go_imports_with_comments() {
717        // Real-world Go code from Kubernetes with inline comments
718        let source = r#"package main
719
720import (
721	"os"
722	_ "time/tzdata" // for timeZone support in CronJob
723
724	"k8s.io/component-base/cli"
725	_ "k8s.io/component-base/logs/json/register"          // for JSON log format registration
726	_ "k8s.io/component-base/metrics/prometheus/clientgo" // load all the prometheus client-go plugins
727)
728
729func main() {
730	os.Exit(0)
731}
732"#;
733
734        let deps = GoDependencyExtractor::extract_dependencies(source).unwrap();
735
736        println!("Extracted {} dependencies:", deps.len());
737        for dep in &deps {
738            println!("  - {} (line {})", dep.imported_path, dep.line_number);
739        }
740
741        // Should extract all imports, even those with _ alias and comments
742        assert!(
743            deps.len() >= 4,
744            "Should extract at least 4 imports, got {}",
745            deps.len()
746        );
747        assert!(deps.iter().any(|d| d.imported_path == "os"));
748        assert!(deps.iter().any(|d| d.imported_path == "time/tzdata"));
749        assert!(
750            deps.iter()
751                .any(|d| d.imported_path == "k8s.io/component-base/cli")
752        );
753    }
754
755    #[test]
756    fn test_find_all_go_mods() {
757        use std::fs;
758        use tempfile::TempDir;
759
760        let temp = TempDir::new().unwrap();
761        let root = temp.path();
762
763        // Create multiple Go modules
764        let service1 = root.join("services/auth");
765        fs::create_dir_all(&service1).unwrap();
766        fs::write(
767            service1.join("go.mod"),
768            "module github.com/myorg/auth\n\ngo 1.21\n",
769        )
770        .unwrap();
771
772        let service2 = root.join("services/api");
773        fs::create_dir_all(&service2).unwrap();
774        fs::write(
775            service2.join("go.mod"),
776            "module github.com/myorg/api\n\ngo 1.21\n",
777        )
778        .unwrap();
779
780        // Create vendor directory that should be skipped
781        let vendor = root.join("vendor");
782        fs::create_dir_all(&vendor).unwrap();
783        fs::write(vendor.join("go.mod"), "module github.com/external/lib\n").unwrap();
784
785        let mods = find_all_go_mods(root).unwrap();
786
787        // Should find 2 modules (skipping vendor)
788        assert_eq!(mods.len(), 2);
789        assert!(mods.iter().any(|p| p.ends_with("services/auth/go.mod")));
790        assert!(mods.iter().any(|p| p.ends_with("services/api/go.mod")));
791    }
792
793    #[test]
794    fn test_parse_all_go_modules() {
795        use std::fs;
796        use tempfile::TempDir;
797
798        let temp = TempDir::new().unwrap();
799        let root = temp.path();
800
801        // Create multiple Go modules
802        let service1 = root.join("services/auth");
803        fs::create_dir_all(&service1).unwrap();
804        fs::write(
805            service1.join("go.mod"),
806            "module github.com/myorg/auth\n\ngo 1.21\n",
807        )
808        .unwrap();
809
810        let service2 = root.join("cmd/api");
811        fs::create_dir_all(&service2).unwrap();
812        fs::write(
813            service2.join("go.mod"),
814            "module github.com/myorg/api\n\ngo 1.21\n",
815        )
816        .unwrap();
817
818        let modules = parse_all_go_modules(root).unwrap();
819
820        // Should find 2 modules
821        assert_eq!(modules.len(), 2);
822
823        // Check module names
824        let names: Vec<_> = modules.iter().map(|m| m.name.as_str()).collect();
825        assert!(names.contains(&"github.com/myorg/auth"));
826        assert!(names.contains(&"github.com/myorg/api"));
827
828        // Check project roots
829        for module in &modules {
830            assert!(
831                module.project_root.starts_with("services/")
832                    || module.project_root.starts_with("cmd/")
833            );
834            assert!(module.abs_project_root.ends_with(&module.project_root));
835        }
836    }
837
838    fn module(name: &str, root: &str) -> GoModule {
839        GoModule {
840            name: name.to_string(),
841            project_root: root.to_string(),
842            abs_project_root: std::path::PathBuf::from("/ws").join(root),
843        }
844    }
845
846    #[test]
847    fn package_key_is_the_package_directory() {
848        let modules = [module("github.com/myorg/myapp", "myapp")];
849        assert_eq!(
850            go_package_key("github.com/myorg/myapp/pkg/models", &modules).as_deref(),
851            Some("go:myapp/pkg/models")
852        );
853        assert_eq!(
854            go_package_key("github.com/myorg/myapp", &modules).as_deref(),
855            Some("go:myapp")
856        );
857    }
858
859    #[test]
860    fn package_key_at_the_repo_root_has_no_leading_slash() {
861        let modules = [module("k8s.io/kubernetes", "")];
862        assert_eq!(
863            go_package_key("k8s.io/kubernetes/test/internal/metric", &modules).as_deref(),
864            Some("go:test/internal/metric")
865        );
866        assert_eq!(
867            go_package_key("k8s.io/kubernetes", &modules).as_deref(),
868            Some("go:")
869        );
870    }
871
872    #[test]
873    fn module_match_needs_path_boundary() {
874        // Walk order put `k8s.io/api` first; `k8s.io/apiserver/...` used to strip to
875        // `server/...` under it.
876        let modules = [
877            module("k8s.io/api", "staging/src/k8s.io/api"),
878            module("k8s.io/apiserver", "staging/src/k8s.io/apiserver"),
879        ];
880        assert_eq!(
881            go_package_key("k8s.io/apiserver/pkg/server", &modules).as_deref(),
882            Some("go:staging/src/k8s.io/apiserver/pkg/server")
883        );
884        assert_eq!(go_package_key("k8s.io/apimachinery/pkg", &modules), None);
885    }
886
887    #[test]
888    fn longest_module_wins() {
889        let modules = [
890            module("example.com/m", ""),
891            module("example.com/m/tools", "tools"),
892        ];
893        assert_eq!(
894            go_package_key("example.com/m/tools/gen", &modules).as_deref(),
895            Some("go:tools/gen")
896        );
897        assert_eq!(
898            go_package_key("example.com/m/pkg/a", &modules).as_deref(),
899            Some("go:pkg/a")
900        );
901    }
902
903    #[test]
904    fn import_outside_modules_has_no_key() {
905        let modules = [module("github.com/myorg/myapp", "myapp")];
906        assert_eq!(go_package_key("github.com/other/package", &modules), None);
907        assert_eq!(go_package_key("./utils", &modules), None);
908    }
909
910    #[test]
911    fn import_outside_modules_is_external() {
912        // Same domain as the module, but no module of the workspace owns it.
913        assert_eq!(
914            reclassify_go_import("k8s.io/klog/v2", Some("k8s.io/kubernetes")),
915            ImportType::External
916        );
917        assert_eq!(
918            reclassify_go_import("k8s.io/kubernetes/pkg/api", Some("k8s.io/kubernetes")),
919            ImportType::Internal
920        );
921        assert_eq!(
922            reclassify_go_import("k8s.io/kubernetesfoo/x", Some("k8s.io/kubernetes")),
923            ImportType::External
924        );
925    }
926
927    #[test]
928    fn package_membership_skips_test_files() {
929        assert_eq!(go_package_member("pkg/a/a.go").as_deref(), Some("go:pkg/a"));
930        assert_eq!(go_package_member("main.go").as_deref(), Some("go:"));
931        assert_eq!(go_package_member("pkg/a/a_test.go"), None);
932    }
933}
934
935// ============================================================================
936// Dependency Extraction
937// ============================================================================
938
939use crate::models::ImportType;
940use crate::parsers::{DependencyExtractor, ImportInfo};
941
942const SYMQ_0: &str = r#"
943        (function_declaration
944            name: (identifier) @name) @function
945    "#;
946const SYMQ_1: &str = r#"
947        (type_declaration
948            (type_spec
949                name: (type_identifier) @name
950                type: (struct_type))) @struct
951    "#;
952const SYMQ_2: &str = r#"
953        (type_declaration
954            (type_spec
955                name: (type_identifier) @name
956                type: (interface_type))) @interface
957    "#;
958const SYMQ_3: &str = r#"
959        (method_declaration
960            receiver: (parameter_list
961                (parameter_declaration
962                    type: [(type_identifier) (pointer_type (type_identifier))] @receiver_type))
963            name: (field_identifier) @method_name) @method
964    "#;
965const SYMQ_4: &str = r#"
966        (const_declaration
967            (const_spec
968                name: (identifier) @name)) @const
969    "#;
970const SYMQ_5: &str = r#"
971        (var_spec
972            name: (identifier) @name) @var
973
974        (short_var_declaration
975            left: (expression_list (identifier) @name)) @short_var
976    "#;
977
978/// Every symbol query of this module, run as ONE query per file (see
979/// `crate::parsers::LanguageQueries`).
980static SYMBOL_QUERIES: crate::parsers::LanguageQueries =
981    crate::parsers::LanguageQueries::new(&[SYMQ_0, SYMQ_1, SYMQ_2, SYMQ_3, SYMQ_4, SYMQ_5]);
982
983/// Go dependency extractor
984pub struct GoDependencyExtractor;
985
986impl DependencyExtractor for GoDependencyExtractor {
987    fn extract_dependencies(source: &str) -> Result<Vec<ImportInfo>> {
988        let mut parser = Parser::new();
989        let language = tree_sitter_go::LANGUAGE;
990
991        parser
992            .set_language(&language.into())
993            .context("Failed to set Go language")?;
994
995        let tree = parser
996            .parse(source, None)
997            .context("Failed to parse Go source")?;
998
999        let root_node = tree.root_node();
1000
1001        let mut imports = Vec::new();
1002
1003        // Extract import statements
1004        imports.extend(extract_go_imports(source, &root_node)?);
1005
1006        Ok(imports)
1007    }
1008}
1009
1010/// Extract Go import statements
1011fn extract_go_imports(source: &str, root: &tree_sitter::Node) -> Result<Vec<ImportInfo>> {
1012    let language = tree_sitter_go::LANGUAGE;
1013
1014    // Go imports can be single or in groups
1015    let query_str = r#"
1016        (import_declaration
1017            (import_spec
1018                path: (interpreted_string_literal) @import_path)) @import
1019
1020        (import_declaration
1021            (import_spec_list
1022                (import_spec
1023                    path: (interpreted_string_literal) @import_path))) @import
1024    "#;
1025
1026    static QUERY_1: crate::parsers::CachedQuery = crate::parsers::CachedQuery::new();
1027    let query = crate::parsers::cached_query(&QUERY_1, language, query_str)
1028        .context("Failed to create Go import query")?;
1029
1030    let mut cursor = QueryCursor::new();
1031    let mut matches = cursor.matches(query, *root, source.as_bytes());
1032
1033    let mut imports = Vec::new();
1034
1035    while let Some(match_) = matches.next() {
1036        let mut import_path = None;
1037        let mut import_node = None;
1038
1039        for capture in match_.captures {
1040            let capture_name: &str = query.capture_names()[capture.index as usize];
1041            match capture_name {
1042                "import_path" => {
1043                    // Remove quotes from string literal
1044                    let raw_path = capture.node.utf8_text(source.as_bytes()).unwrap_or("");
1045                    import_path = Some(raw_path.trim_matches('"').to_string());
1046                }
1047                "import" => {
1048                    import_node = Some(capture.node);
1049                }
1050                _ => {}
1051            }
1052        }
1053
1054        if let (Some(path), Some(node)) = (import_path, import_node) {
1055            let import_type = classify_go_import(&path);
1056            let line_number = node.start_position().row + 1;
1057
1058            imports.push(ImportInfo {
1059                imported_path: path,
1060                import_type,
1061                line_number,
1062                imported_symbols: None, // Go imports entire packages, not selective symbols
1063            });
1064        }
1065    }
1066
1067    Ok(imports)
1068}
1069
1070/// Find and parse go.mod to extract module name
1071/// Returns None if go.mod not found or module name can't be parsed
1072pub fn find_go_module_name(root: &std::path::Path) -> Option<String> {
1073    // Look for go.mod in root directory
1074    let go_mod_path = root.join("go.mod");
1075    if !go_mod_path.exists() {
1076        return None;
1077    }
1078
1079    // Read go.mod and extract module name
1080    let content = std::fs::read_to_string(&go_mod_path).ok()?;
1081    for line in content.lines() {
1082        let trimmed = line.trim();
1083        if trimmed.starts_with("module ") {
1084            // Extract module name: "module k8s.io/kubernetes" -> "k8s.io/kubernetes"
1085            let module_name = trimmed.strip_prefix("module ").unwrap_or("").trim();
1086            return Some(module_name.to_string());
1087        }
1088    }
1089
1090    None
1091}
1092
1093/// Reclassify a Go import based on the module prefix
1094/// This should be called by the indexer after extraction to correctly identify internal imports
1095pub fn reclassify_go_import(import_path: &str, module_prefix: Option<&str>) -> ImportType {
1096    classify_go_import_impl(import_path, module_prefix)
1097}
1098
1099/// Classify a Go import as internal, external, or stdlib
1100fn classify_go_import(import_path: &str) -> ImportType {
1101    classify_go_import_impl(import_path, None)
1102}
1103
1104/// Internal implementation of Go import classification
1105fn classify_go_import_impl(import_path: &str, module_prefix: Option<&str>) -> ImportType {
1106    // An import inside a module of the workspace is Internal. Only on a path
1107    // boundary: `k8s.io/apiserver` is not in `k8s.io/api`, and a module that merely
1108    // shares a domain (`k8s.io/klog` beside `k8s.io/kubernetes`) is not ours.
1109    if let Some(prefix) = module_prefix
1110        && in_module(import_path, prefix)
1111    {
1112        return ImportType::Internal;
1113    }
1114    // Relative imports (./ or ../) - rare in Go but possible
1115    if import_path.starts_with("./") || import_path.starts_with("../") {
1116        return ImportType::Internal;
1117    }
1118
1119    // Internal imports often start with company domain or project path
1120    // Check for common patterns like github.com/your-org/project
1121    // For now, we'll consider anything that looks like a full URL path as external
1122    // and short stdlib-like paths as stdlib
1123
1124    // Go standard library modules (common ones)
1125    const STDLIB_MODULES: &[&str] = &[
1126        "fmt",
1127        "io",
1128        "os",
1129        "path",
1130        "strings",
1131        "bytes",
1132        "bufio",
1133        "errors",
1134        "context",
1135        "sync",
1136        "time",
1137        "encoding/json",
1138        "encoding/xml",
1139        "encoding/csv",
1140        "net/http",
1141        "net/url",
1142        "net",
1143        "crypto",
1144        "crypto/tls",
1145        "crypto/sha256",
1146        "database/sql",
1147        "log",
1148        "math",
1149        "regexp",
1150        "strconv",
1151        "sort",
1152        "reflect",
1153        "runtime",
1154        "testing",
1155        "flag",
1156        "filepath",
1157        "unicode",
1158        "html",
1159        "text/template",
1160    ];
1161
1162    // Check if it's a stdlib module
1163    if STDLIB_MODULES.contains(&import_path) {
1164        return ImportType::Stdlib;
1165    }
1166
1167    // If it contains a domain (has dots and slashes), it's external
1168    if import_path.contains('/') && import_path.split('/').next().unwrap_or("").contains('.') {
1169        return ImportType::External;
1170    }
1171
1172    // Short paths without domains are likely stdlib
1173    if !import_path.contains('/') || import_path.split('/').count() <= 2 {
1174        return ImportType::Stdlib;
1175    }
1176
1177    // Everything else is external
1178    ImportType::External
1179}
1180
1181// ============================================================================
1182// Monorepo Support & Path Resolution
1183// ============================================================================
1184
1185/// Represents a Go module with its location
1186#[derive(Debug, Clone)]
1187pub struct GoModule {
1188    /// Module name (e.g., "k8s.io/kubernetes", "github.com/myorg/myproject")
1189    pub name: String,
1190    /// Project root relative to index root (e.g., "services/api")
1191    pub project_root: String,
1192    /// Absolute path to project root
1193    pub abs_project_root: std::path::PathBuf,
1194}
1195
1196/// Recursively find all go.mod files in the repository, respecting .gitignore
1197pub fn find_all_go_mods(index_root: &std::path::Path) -> Result<Vec<std::path::PathBuf>> {
1198    use ignore::WalkBuilder;
1199
1200    let mut go_mod_files = Vec::new();
1201
1202    let walker = WalkBuilder::new(index_root)
1203        .follow_links(false)
1204        .git_ignore(true)
1205        .build();
1206
1207    for entry in walker {
1208        let entry = entry?;
1209        let path = entry.path();
1210
1211        if !path.is_file() {
1212            continue;
1213        }
1214
1215        let filename = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
1216
1217        // Look for go.mod files
1218        if filename == "go.mod" {
1219            // Skip vendor directories. Normalize separators so the check
1220            // works on Windows (`\vendor\`) as well as Unix (`/vendor/`).
1221            let path_str = path.to_string_lossy().replace('\\', "/");
1222            if path_str.contains("/vendor/") {
1223                log::trace!("Skipping go.mod in vendor directory: {:?}", path);
1224                continue;
1225            }
1226
1227            go_mod_files.push(path.to_path_buf());
1228        }
1229    }
1230
1231    log::debug!("Found {} go.mod files", go_mod_files.len());
1232    Ok(go_mod_files)
1233}
1234
1235/// Parse all Go modules in a monorepo and track their project roots
1236pub fn parse_all_go_modules(index_root: &std::path::Path) -> Result<Vec<GoModule>> {
1237    let go_mod_files = find_all_go_mods(index_root)?;
1238    parse_go_modules_from(index_root, &go_mod_files)
1239}
1240
1241/// [`parse_all_go_modules`] for `go.mod` files already found (in walk order).
1242pub fn parse_go_modules_from(
1243    index_root: &std::path::Path,
1244    go_mod_files: &[std::path::PathBuf],
1245) -> Result<Vec<GoModule>> {
1246    if go_mod_files.is_empty() {
1247        log::debug!("No go.mod files found in {:?}", index_root);
1248        return Ok(Vec::new());
1249    }
1250
1251    let mut modules = Vec::new();
1252    let mod_count = go_mod_files.len();
1253
1254    for go_mod_path in go_mod_files {
1255        let project_root = go_mod_path
1256            .parent()
1257            .ok_or_else(|| anyhow::anyhow!("go.mod has no parent directory"))?;
1258
1259        // Read and parse go.mod to extract module name
1260        if let Ok(content) = std::fs::read_to_string(go_mod_path) {
1261            for line in content.lines() {
1262                let trimmed = line.trim();
1263                if trimmed.starts_with("module ") {
1264                    let module_name = trimmed
1265                        .strip_prefix("module ")
1266                        .unwrap_or("")
1267                        .trim()
1268                        .to_string();
1269
1270                    // Normalize to forward slashes so import resolution and
1271                    // assertions on project_root behave the same on every OS.
1272                    let relative_project_root = project_root
1273                        .strip_prefix(index_root)
1274                        .unwrap_or(project_root)
1275                        .to_string_lossy()
1276                        .replace('\\', "/");
1277
1278                    log::debug!(
1279                        "Found Go module '{}' at {:?}",
1280                        module_name,
1281                        relative_project_root
1282                    );
1283
1284                    modules.push(GoModule {
1285                        name: module_name,
1286                        project_root: relative_project_root,
1287                        abs_project_root: project_root.to_path_buf(),
1288                    });
1289                    break;
1290                }
1291            }
1292        }
1293    }
1294
1295    log::info!(
1296        "Loaded {} Go modules from {} go.mod files",
1297        modules.len(),
1298        mod_count
1299    );
1300
1301    Ok(modules)
1302}
1303
1304/// Whether `import_path` is module `module` or a package inside it.
1305fn in_module(import_path: &str, module: &str) -> bool {
1306    import_path
1307        .strip_prefix(module)
1308        .is_some_and(|rest| rest.is_empty() || rest.starts_with('/'))
1309}
1310
1311/// The package key (`go:<dir>`, the directory relative to the index root) of an
1312/// import in one of the workspace's modules. A Go import names a package, which
1313/// is every non-test `.go` file of one directory ([`go_package_member`]). The
1314/// longest matching module wins (a nested module owns its subtree).
1315pub fn go_package_key(import_path: &str, modules: &[GoModule]) -> Option<String> {
1316    let module = modules
1317        .iter()
1318        .filter(|m| in_module(import_path, &m.name))
1319        .max_by(|a, b| {
1320            a.name
1321                .len()
1322                .cmp(&b.name.len())
1323                .then_with(|| b.project_root.cmp(&a.project_root))
1324        })?;
1325    let sub = import_path[module.name.len()..].trim_start_matches('/');
1326    let dir = [module.project_root.as_str(), sub]
1327        .into_iter()
1328        .filter(|part| !part.is_empty())
1329        .collect::<Vec<_>>()
1330        .join("/");
1331    Some(format!("go:{dir}"))
1332}
1333
1334/// The package a Go file belongs to: its directory, unless it is a `_test.go`
1335/// file (never compiled into an importer).
1336pub fn go_package_member(rel_path: &str) -> Option<String> {
1337    if rel_path.ends_with("_test.go") {
1338        return None;
1339    }
1340    let dir = rel_path.rsplit_once('/').map_or("", |(dir, _)| dir);
1341    Some(format!("go:{dir}"))
1342}