code-graph-cli 3.0.3

Code intelligence engine for TypeScript/JavaScript/Rust/Python/Go — query the dependency graph instead of reading source files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
pub mod barrel;
pub mod cargo_workspace;
pub mod file_resolver;
pub mod go_resolver;
pub mod python_resolver;
pub mod rust_mod_tree;
pub mod rust_resolver;
pub mod workspace;

pub use file_resolver::{
    ResolutionOutcome, build_resolver, resolve_import, workspace_map_to_aliases,
};
pub use workspace::discover_workspace_packages;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use petgraph::visit::EdgeRef;

use crate::graph::CodeGraph;
use crate::parser::ParseResult;
use crate::parser::relationships::RelationshipKind;

/// Statistics collected during the resolution pipeline.
#[derive(Debug, Default)]
pub struct ResolveStats {
    /// Number of imports successfully resolved to a local file.
    pub resolved: usize,
    /// Number of imports that could not be resolved.
    pub unresolved: usize,
    /// Number of imports resolved to an external package (node_modules).
    pub external: usize,
    /// Number of imports resolved to Node.js built-in modules (fs, path, etc.).
    pub builtin: usize,
    /// Number of symbol-level relationship edges added to the graph.
    pub relationships_added: usize,
    /// Number of direct ResolvedImport edges added by the named re-export chain pass.
    /// These edges bypass barrel files and point directly to the defining file.
    pub named_reexport_edges: usize,

    // --- Rust-specific (Step 6) ---
    /// Rust use paths resolved to a file node (intra-crate or cross-workspace).
    pub rust_resolved: usize,
    /// Rust use paths resolved to an `ExternalPackage` node.
    pub rust_external: usize,
    /// Rust use paths resolved to a `Builtin` node (std/core/alloc).
    pub rust_builtin: usize,
    /// Rust use paths that could not be resolved — `UnresolvedImport` nodes created.
    pub rust_unresolved: usize,

    // --- Go-specific (Step 8) ---
    /// Go imports resolved to local file nodes.
    pub go_resolved: usize,
    /// Go imports classified as stdlib (external).
    pub go_stdlib: usize,
    /// Go imports classified as external packages.
    pub go_external: usize,
    /// Go imports that could not be resolved.
    pub go_unresolved: usize,
}

/// Run the full import resolution pipeline on the code graph.
///
/// Executes five sequential steps:
///
/// 1. **Workspace detection** — discover monorepo workspace packages so
///    cross-package imports can be resolved to local source directories.
/// 2. **Resolver construction** — build a single `oxc_resolver::Resolver`
///    configured for TypeScript (tsconfig paths, extension aliases, workspace aliases).
/// 3. **File-level resolution** — for every import in every parsed file, call
///    `resolve_import()` and classify the outcome as Resolved / External / Builtin /
///    Unresolved, adding the appropriate graph edge.
/// 4. **Barrel chain pass** — add `BarrelReExportAll` edges for `export * from` statements.
/// 5. **Symbol relationship pass** — wire Extends / Implements / InterfaceExtends / Calls /
///    TypeReference edges between symbol nodes where both endpoints are in the graph.
///
/// # Parameters
/// - `graph`: the mutable code graph to enrich with resolution edges
/// - `project_root`: the project root directory (used for tsconfig, workspace detection)
/// - `parse_results`: all parsed files and their extracted import/export/relationship data
/// - `verbose`: if `true`, emit diagnostic messages to stderr
///
/// # Returns
/// A [`ResolveStats`] struct with counts for each category of resolution outcome.
pub fn resolve_all(
    graph: &mut CodeGraph,
    project_root: &Path,
    parse_results: &HashMap<PathBuf, ParseResult>,
    verbose: bool,
) -> ResolveStats {
    let mut stats = ResolveStats::default();

    // -----------------------------------------------------------------------
    // Step 1: Build workspace map.
    // -----------------------------------------------------------------------
    let workspace_map = discover_workspace_packages(project_root);
    if verbose && !workspace_map.is_empty() {
        eprintln!("  Workspace packages found: {}", workspace_map.len());
        for (name, path) in &workspace_map {
            eprintln!("    {} -> {}", name, path.display());
        }
    }

    // -----------------------------------------------------------------------
    // Step 2: Build resolver (one instance — reuse for all files).
    // -----------------------------------------------------------------------
    let aliases = workspace_map_to_aliases(&workspace_map);
    let resolver = build_resolver(project_root, aliases);

    // -----------------------------------------------------------------------
    // Step 3: File-level resolution pass.
    // -----------------------------------------------------------------------
    // Collect all (file_path, imports) pairs first to avoid borrow conflicts.
    let file_imports: Vec<(PathBuf, Vec<crate::parser::imports::ImportInfo>)> = parse_results
        .iter()
        .map(|(path, result)| (path.clone(), result.imports.clone()))
        .collect();

    for (file_path, imports) in &file_imports {
        let from_idx = match graph.file_index.get(file_path).copied() {
            Some(idx) => idx,
            None => {
                // File wasn't added to graph (shouldn't happen, but defensive).
                continue;
            }
        };

        for import in imports {
            let specifier = &import.module_path;
            let outcome = resolve_import(&resolver, file_path, specifier);

            match outcome {
                ResolutionOutcome::Resolved(target_path) => {
                    // Check if the resolved target is in the graph (was indexed).
                    if let Some(&target_idx) = graph.file_index.get(&target_path) {
                        graph.add_resolved_import(from_idx, target_idx, specifier);
                        stats.resolved += 1;
                    } else {
                        // Resolved to a path not in the graph (e.g. JSON, .node file, or
                        // a file outside the indexed project). Treat as unresolved.
                        if verbose {
                            eprintln!(
                                "  resolve: {} imports '{}' -> {} (not indexed, skipping edge)",
                                file_path.display(),
                                specifier,
                                target_path.display()
                            );
                        }
                        stats.resolved += 1; // resolver succeeded; we just didn't index it
                    }
                }
                ResolutionOutcome::BuiltinModule(name) => {
                    // Node.js built-in — record as unresolved with "builtin" reason.
                    graph.add_unresolved_import(from_idx, specifier, "builtin");
                    stats.builtin += 1;
                    if verbose {
                        eprintln!(
                            "  resolve: {} imports '{}' -> builtin:{}",
                            file_path.display(),
                            specifier,
                            name
                        );
                    }
                }
                ResolutionOutcome::Unresolved(_reason) => {
                    // Classify: is this an external package or truly unresolvable?
                    if is_external_package(specifier) {
                        let pkg_name = extract_package_name(specifier);
                        graph.add_external_package(from_idx, pkg_name, specifier);
                        stats.external += 1;
                        if verbose {
                            eprintln!(
                                "  resolve: {} imports '{}' -> external:{}",
                                file_path.display(),
                                specifier,
                                pkg_name
                            );
                        }
                    } else {
                        graph.add_unresolved_import(from_idx, specifier, &_reason);
                        stats.unresolved += 1;
                        if verbose {
                            eprintln!(
                                "  resolve: {} imports '{}' -> unresolved: {}",
                                file_path.display(),
                                specifier,
                                _reason
                            );
                        }
                    }
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // Step 4: Barrel chain pass.
    // -----------------------------------------------------------------------
    barrel::resolve_barrel_chains(graph, parse_results, verbose);

    // Step 4b: Named re-export chain pass.
    // Adds direct ResolvedImport edges from importing files to the defining file,
    // bypassing barrel files for named re-exports (export { Foo } from './module').
    let named_reexport_edges = barrel::resolve_named_reexport_chains(graph, parse_results, verbose);
    stats.named_reexport_edges = named_reexport_edges;
    if verbose {
        eprintln!("  Named re-export edges added: {}", named_reexport_edges);
    }

    // -----------------------------------------------------------------------
    // Step 5: Symbol relationship pass.
    // -----------------------------------------------------------------------
    // Collect all relationship data first to avoid double-borrow of graph.
    let file_relationships: Vec<(PathBuf, Vec<crate::parser::relationships::RelationshipInfo>)> =
        parse_results
            .iter()
            .map(|(path, result)| (path.clone(), result.relationships.clone()))
            .collect();

    for (_file_path, relationships) in &file_relationships {
        let from_file_idx = match graph.file_index.get(_file_path).copied() {
            Some(idx) => idx,
            None => continue,
        };

        for rel in relationships {
            match rel.kind {
                RelationshipKind::Extends
                | RelationshipKind::Implements
                | RelationshipKind::InterfaceExtends => {
                    // Both from_name and to_name should be present for inheritance.
                    let from_name = match &rel.from_name {
                        Some(n) => n,
                        None => continue,
                    };

                    let from_candidates = graph
                        .symbol_index
                        .get(from_name)
                        .cloned()
                        .unwrap_or_default();
                    let to_candidates = graph
                        .symbol_index
                        .get(&rel.to_name)
                        .cloned()
                        .unwrap_or_default();

                    if from_candidates.is_empty() || to_candidates.is_empty() {
                        continue;
                    }

                    // Pick the from_candidate in the same file if possible; else use first.
                    let from_sym_idx = from_candidates
                        .iter()
                        .copied()
                        .find(|&idx| {
                            // Check if this symbol belongs to the current file.
                            graph.graph.edges(from_file_idx).any(|e| e.target() == idx)
                        })
                        .unwrap_or(from_candidates[0]);

                    // For to_name: prefer same file; if ambiguous, add edges to all candidates.
                    let same_file_to: Vec<_> = to_candidates
                        .iter()
                        .copied()
                        .filter(|&idx| graph.graph.edges(from_file_idx).any(|e| e.target() == idx))
                        .collect();

                    let to_indices = if same_file_to.is_empty() {
                        to_candidates.clone()
                    } else {
                        same_file_to
                    };

                    for to_sym_idx in to_indices {
                        match rel.kind {
                            RelationshipKind::Extends => {
                                graph.add_extends_edge(from_sym_idx, to_sym_idx);
                                stats.relationships_added += 1;
                            }
                            RelationshipKind::Implements => {
                                graph.add_implements_edge(from_sym_idx, to_sym_idx);
                                stats.relationships_added += 1;
                            }
                            RelationshipKind::InterfaceExtends => {
                                // Interface extends uses the same Extends edge kind.
                                graph.add_extends_edge(from_sym_idx, to_sym_idx);
                                stats.relationships_added += 1;
                            }
                            _ => unreachable!(),
                        }
                    }
                }

                RelationshipKind::Calls
                | RelationshipKind::MethodCall
                | RelationshipKind::TypeReference => {
                    // Look up the callee / type name in the symbol index.
                    let to_candidates = match graph.symbol_index.get(&rel.to_name) {
                        Some(c) if !c.is_empty() => c.clone(),
                        _ => continue,
                    };

                    // Only add edge if exactly one candidate (unambiguous).
                    // Cross-file call ambiguity is a documented limitation per research.
                    if to_candidates.len() == 1 {
                        let callee_idx = to_candidates[0];
                        graph.add_calls_edge(from_file_idx, callee_idx);
                        stats.relationships_added += 1;
                    }
                    // If multiple candidates: skip (ambiguous cross-file call — documented limitation)
                }
            }
        }
    }

    // -----------------------------------------------------------------------
    // Step 6: Rust use/pub-use resolution.
    // -----------------------------------------------------------------------
    // Only run when there are Rust files in the graph (avoids workspace discovery
    // overhead on pure TypeScript/JavaScript projects).
    let has_rust_files = graph.graph.node_indices().any(|idx| {
        if let crate::graph::node::GraphNode::File(ref f) = graph.graph[idx] {
            f.language == "rust"
        } else {
            false
        }
    });
    if has_rust_files {
        let rust_stats =
            rust_resolver::resolve_rust_uses(graph, project_root, parse_results, verbose);
        stats.rust_resolved = rust_stats.resolved;
        stats.rust_external = rust_stats.external;
        stats.rust_builtin = rust_stats.builtin;
        stats.rust_unresolved = rust_stats.unresolved;
        if verbose {
            eprintln!(
                "  Rust resolution: {} resolved, {} external, {} builtin, {} unresolved",
                rust_stats.resolved, rust_stats.external, rust_stats.builtin, rust_stats.unresolved
            );
        }
    }

    // -----------------------------------------------------------------------
    // Step 7: Python import resolution.
    // -----------------------------------------------------------------------
    // Only run when there are Python files in the graph (avoids overhead
    // on pure TypeScript/JavaScript/Rust projects).
    let has_python_files = graph.graph.node_indices().any(|idx| {
        if let crate::graph::node::GraphNode::File(ref f) = graph.graph[idx] {
            f.language == "python"
        } else {
            false
        }
    });
    if has_python_files {
        let py_stats = python_resolver::resolve_python_imports(graph, parse_results, project_root);
        stats.resolved += py_stats.resolved;
        stats.unresolved += py_stats.unresolved;
        if verbose {
            eprintln!(
                "  Python resolution: {} resolved, {} unresolved, {} conditional",
                py_stats.resolved, py_stats.unresolved, py_stats.conditional,
            );
        }
    }

    // -----------------------------------------------------------------------
    // Step 8: Go import resolution.
    // -----------------------------------------------------------------------
    let has_go_files = graph.graph.node_indices().any(|idx| {
        if let crate::graph::node::GraphNode::File(ref f) = graph.graph[idx] {
            f.language == "go"
        } else {
            false
        }
    });
    if has_go_files {
        let go_stats = go_resolver::resolve_go_imports(graph, parse_results, project_root, verbose);
        stats.go_resolved = go_stats.resolved;
        stats.go_stdlib = go_stats.stdlib;
        stats.go_external = go_stats.external;
        stats.go_unresolved = go_stats.unresolved;
        if verbose {
            eprintln!(
                "  Go resolution: {} resolved, {} stdlib, {} external, {} unresolved",
                go_stats.resolved, go_stats.stdlib, go_stats.external, go_stats.unresolved
            );
        }
    }

    stats
}

// ---------------------------------------------------------------------------
// Helper functions
// ---------------------------------------------------------------------------

/// Returns `true` if the specifier looks like an external package reference.
///
/// External packages:
/// - Do not start with `.` (relative) or `/` (absolute)
/// - Are not tsconfig path aliases starting with `@/` (project-internal)
///
/// This heuristic matches npm package patterns: `react`, `@scope/pkg`, `lodash/merge`.
fn is_external_package(specifier: &str) -> bool {
    !specifier.starts_with('.') && !specifier.starts_with('/')
}

/// Extract the canonical package name from a module specifier.
///
/// - `react` → `react`
/// - `@org/utils` → `@org/utils`  (scoped package — keep both parts)
/// - `lodash/merge` → `lodash`    (subpath import)
/// - `@org/utils/helpers` → `@org/utils`  (scoped package subpath)
fn extract_package_name(specifier: &str) -> &str {
    if specifier.starts_with('@') {
        // Scoped package: `@scope/name[/subpath]` — keep first two segments.
        let parts: Vec<&str> = specifier.splitn(3, '/').collect();
        if parts.len() >= 2 {
            // Return everything up to and including the second segment.
            let scope_end = parts[0].len() + 1 + parts[1].len();
            &specifier[..scope_end]
        } else {
            specifier
        }
    } else {
        // Unscoped: `name[/subpath]` — keep first segment.
        match specifier.find('/') {
            Some(idx) => &specifier[..idx],
            None => specifier,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_external_package() {
        assert!(is_external_package("react"));
        assert!(is_external_package("@org/utils"));
        assert!(is_external_package("lodash/merge"));
        assert!(!is_external_package("./local"));
        assert!(!is_external_package("../parent"));
        assert!(!is_external_package("/absolute"));
    }

    #[test]
    fn test_extract_package_name() {
        assert_eq!(extract_package_name("react"), "react");
        assert_eq!(extract_package_name("@org/utils"), "@org/utils");
        assert_eq!(extract_package_name("@org/utils/helpers"), "@org/utils");
        assert_eq!(extract_package_name("lodash/merge"), "lodash");
        assert_eq!(extract_package_name("lodash"), "lodash");
    }
}