BuildFileGraphFromAnalyses builds the file graph from a pre-computed ast-grep scan, letting callers that already hold the analyses avoid a redundant full-repo ScanForDeps. BuildFileGraph is the convenience wrapper that scans.
(root string, analyses []FileAnalysis)
| 42 | // scan, letting callers that already hold the analyses avoid a redundant |
| 43 | // full-repo ScanForDeps. BuildFileGraph is the convenience wrapper that scans. |
| 44 | func BuildFileGraphFromAnalyses(root string, analyses []FileAnalysis) (*FileGraph, error) { |
| 45 | absRoot, err := filepath.Abs(root) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | |
| 50 | fg := &FileGraph{ |
| 51 | Root: absRoot, |
| 52 | Imports: make(map[string][]string), |
| 53 | Importers: make(map[string][]string), |
| 54 | Packages: make(map[string][]string), |
| 55 | PathAliases: make(map[string][]string), |
| 56 | } |
| 57 | |
| 58 | // Detect module name from go.mod (for Go import resolution) |
| 59 | fg.Module = detectModule(absRoot) |
| 60 | |
| 61 | // Detect path aliases from tsconfig.json (for TS/JS import resolution) |
| 62 | fg.PathAliases, fg.BaseURL = detectPathAliases(absRoot) |
| 63 | |
| 64 | // Scan all files |
| 65 | gitCache := NewGitIgnoreCache(root) |
| 66 | files, err := ScanFiles(root, gitCache, nil, nil) |
| 67 | if err != nil { |
| 68 | return nil, err |
| 69 | } |
| 70 | |
| 71 | // Build file index for fast fuzzy matching |
| 72 | idx := buildFileIndex(files, fg.Module) |
| 73 | fg.Packages = idx.goPkgs |
| 74 | |
| 75 | // Resolve imports to files using universal fuzzy matching |
| 76 | for _, a := range analyses { |
| 77 | var resolvedImports []string |
| 78 | |
| 79 | for _, imp := range a.Imports { |
| 80 | resolved := fuzzyResolve(imp, a.Path, idx, fg.Module, fg.PathAliases, fg.BaseURL) |
| 81 | // Exclude multi-file Go package imports to avoid inflating hub counts. |
| 82 | // Go package imports start with the module prefix and resolve to all |
| 83 | // files in that package. For all other imports (e.g., C# namespace |
| 84 | // imports that resolve via directory matching), allow multi-file |
| 85 | // resolution so inter-namespace dependencies are tracked. |
| 86 | isGoPkg := fg.Module != "" && strings.HasPrefix(imp, fg.Module) && len(resolved) > 1 |
| 87 | if !isGoPkg && len(resolved) > 0 { |
| 88 | resolvedImports = append(resolvedImports, resolved...) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if len(resolvedImports) > 0 { |
| 93 | fg.Imports[a.Path] = dedupe(resolvedImports) |
| 94 | |
| 95 | // Build reverse map |
| 96 | for _, imported := range fg.Imports[a.Path] { |
| 97 | fg.Importers[imported] = append(fg.Importers[imported], a.Path) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 |