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
use std::path::{Path, PathBuf};

use petgraph::visit::EdgeRef;

use crate::graph::{CodeGraph, edge::EdgeKind, node::GraphNode};

// ---------------------------------------------------------------------------
// Data structures
// ---------------------------------------------------------------------------

/// Classification category of an import.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum ImportCategory {
    Internal,  // Same package/crate
    Workspace, // Sibling crate in same workspace (Rust only)
    External,  // npm/crates.io dependency
    Builtin,   // std/core/alloc or node builtins
}

/// A single import entry.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ImportEntry {
    pub specifier: String,
    pub category: ImportCategory,
    pub is_reexport: bool,
}

// ---------------------------------------------------------------------------
// Classification helpers
// ---------------------------------------------------------------------------

/// Classify a RustImport path (e.g. "std::collections::HashMap") into a category.
///
/// - `std::` / `core::` / `alloc::` -> Builtin
/// - `crate::` or matches source file's own crate_name -> Internal
/// - anything else -> External
fn classify_rust_import(path: &str, source_crate: Option<&str>) -> ImportCategory {
    let first_segment = path.split("::").next().unwrap_or("");

    match first_segment {
        "std" | "core" | "alloc" => ImportCategory::Builtin,
        "crate" | "super" | "self" => ImportCategory::Internal,
        seg => {
            // Check if it's the source file's own crate
            if let Some(crate_name) = source_crate
                && seg == crate_name
            {
                return ImportCategory::Internal;
            }
            ImportCategory::External
        }
    }
}

// ---------------------------------------------------------------------------
// Main query function
// ---------------------------------------------------------------------------

/// Build a classified list of imports for a single file in the graph.
///
/// Returns imports in edge iteration order (approximate source-file order).
/// Returns `Err` if the file path is not found in the graph.
pub fn file_imports(
    graph: &CodeGraph,
    root: &Path,
    file_path: &Path,
) -> Result<Vec<ImportEntry>, String> {
    // Resolve path: relative paths are joined to root.
    let abs_path: PathBuf = if file_path.is_absolute() {
        file_path.to_path_buf()
    } else {
        root.join(file_path)
    };

    let file_idx = graph
        .file_index
        .get(&abs_path)
        .copied()
        .ok_or_else(|| format!("File not found: {}", file_path.display()))?;

    // Get source crate_name for RustImport classification
    let source_crate: Option<String> = match &graph.graph[file_idx] {
        GraphNode::File(fi) => fi.crate_name.clone(),
        _ => None,
    };

    let mut entries: Vec<ImportEntry> = Vec::new();

    for edge_ref in graph.graph.edges(file_idx) {
        match edge_ref.weight() {
            EdgeKind::ResolvedImport { specifier } => {
                let target_idx = edge_ref.target();
                let category = match &graph.graph[target_idx] {
                    GraphNode::File(fi) => {
                        // Check if source and target are from different crates (workspace)
                        if let (Some(src_crate), Some(tgt_crate)) =
                            (source_crate.as_deref(), fi.crate_name.as_deref())
                        {
                            if src_crate != tgt_crate {
                                ImportCategory::Workspace
                            } else {
                                ImportCategory::Internal
                            }
                        } else {
                            ImportCategory::Internal
                        }
                    }
                    GraphNode::ExternalPackage(_) => ImportCategory::External,
                    GraphNode::Builtin { .. } => ImportCategory::Builtin,
                    GraphNode::UnresolvedImport { .. } => {
                        // Skip unresolved imports — they add noise
                        continue;
                    }
                    _ => continue,
                };

                entries.push(ImportEntry {
                    specifier: specifier.clone(),
                    category,
                    is_reexport: false,
                });
            }

            EdgeKind::ReExport { path } => {
                entries.push(ImportEntry {
                    specifier: path.clone(),
                    category: ImportCategory::Internal,
                    is_reexport: true,
                });
            }

            EdgeKind::BarrelReExportAll => {
                let target_idx = edge_ref.target();
                // Use relative path of target file as specifier
                let specifier = match &graph.graph[target_idx] {
                    GraphNode::File(fi) => fi
                        .path
                        .strip_prefix(root)
                        .map(|p| p.to_string_lossy().into_owned())
                        .unwrap_or_else(|_| fi.path.to_string_lossy().into_owned()),
                    _ => continue,
                };

                entries.push(ImportEntry {
                    specifier,
                    category: ImportCategory::Internal,
                    is_reexport: true,
                });
            }

            EdgeKind::RustImport { path } => {
                let category = classify_rust_import(path, source_crate.as_deref());
                entries.push(ImportEntry {
                    specifier: path.clone(),
                    category,
                    is_reexport: false,
                });
            }

            // Skip all other edge kinds
            _ => {}
        }
    }

    Ok(entries)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use super::*;
    use crate::graph::{
        CodeGraph,
        edge::EdgeKind,
        node::{FileInfo, FileKind, GraphNode},
    };

    #[allow(dead_code)]
    fn make_file_info(path: PathBuf, crate_name: Option<&str>) -> FileInfo {
        FileInfo {
            path,
            language: "rust".into(),
            crate_name: crate_name.map(|s| s.to_string()),
            kind: FileKind::Source,
        }
    }

    #[allow(dead_code)]
    fn make_graph_with_source(
        root: &Path,
        file_name: &str,
        crate_name: Option<&str>,
    ) -> (CodeGraph, PathBuf) {
        let mut graph = CodeGraph::new();
        let file_path = root.join(file_name);
        let file_idx = graph.add_file(file_path.clone(), "rust");
        // Update crate_name if provided
        if let Some(cn) = crate_name
            && let Some(GraphNode::File(fi)) = graph.graph.node_weight_mut(file_idx)
        {
            fi.crate_name = Some(cn.to_string());
        }
        (graph, file_path)
    }

    #[test]
    fn test_resolved_import_internal() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/a.rs");
        let tgt_path = root.join("src/b.rs");

        let src_idx = graph.add_file(src_path.clone(), "rust");
        let tgt_idx = graph.add_file(tgt_path.clone(), "rust");

        // Both in same crate (crate_name = None -> Internal)
        graph.graph.add_edge(
            src_idx,
            tgt_idx,
            EdgeKind::ResolvedImport {
                specifier: "./b".into(),
            },
        );

        let entries = file_imports(&graph, &root, &src_path).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].category, ImportCategory::Internal);
        assert!(!entries[0].is_reexport);
    }

    #[test]
    fn test_resolved_import_external() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/a.ts");
        let src_idx = graph.add_file(src_path.clone(), "typescript");

        // Add an external package node
        let _pkg_idx = graph.add_external_package(src_idx, "react", "react");

        // The add_external_package already adds the edge, so let's just verify
        let entries = file_imports(&graph, &root, &src_path).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].category, ImportCategory::External);
        assert!(!entries[0].is_reexport);
    }

    #[test]
    fn test_resolved_import_builtin() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/main.rs");
        let src_idx = graph.add_file(src_path.clone(), "rust");

        // Add a builtin node
        graph.add_builtin_node(src_idx, "std", "std::collections::HashMap");

        let entries = file_imports(&graph, &root, &src_path).unwrap();
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].category, ImportCategory::Builtin);
        assert!(!entries[0].is_reexport);
    }

    #[test]
    fn test_reexport_label() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/lib.rs");
        let src_idx = graph.add_file(src_path.clone(), "rust");

        // Add a ReExport edge
        graph.graph.add_edge(
            src_idx,
            src_idx, // self-edge placeholder (as in real resolver)
            EdgeKind::ReExport {
                path: "crate::utils::helper".into(),
            },
        );

        let entries = file_imports(&graph, &root, &src_path).unwrap();
        assert!(
            entries.iter().any(|e| e.is_reexport),
            "ReExport edge should set is_reexport=true"
        );
        let reexport = entries.iter().find(|e| e.is_reexport).unwrap();
        assert_eq!(reexport.category, ImportCategory::Internal);
    }

    #[test]
    fn test_barrel_reexport() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let barrel_path = root.join("src/index.ts");
        let source_path = root.join("src/utils.ts");

        let barrel_idx = graph.add_file(barrel_path.clone(), "typescript");
        let source_idx = graph.add_file(source_path.clone(), "typescript");

        // Add BarrelReExportAll edge
        graph.add_barrel_reexport_all(barrel_idx, source_idx);

        let entries = file_imports(&graph, &root, &barrel_path).unwrap();
        assert_eq!(entries.len(), 1);
        assert!(
            entries[0].is_reexport,
            "BarrelReExportAll should set is_reexport=true"
        );
        assert_eq!(entries[0].category, ImportCategory::Internal);
    }

    #[test]
    fn test_rust_import_builtin() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/main.rs");
        let src_idx = graph.add_file(src_path.clone(), "rust");

        // Add a RustImport edge with std:: path
        graph.graph.add_edge(
            src_idx,
            src_idx, // placeholder target
            EdgeKind::RustImport {
                path: "std::collections::HashMap".into(),
            },
        );

        let entries = file_imports(&graph, &root, &src_path).unwrap();
        let rust_imports: Vec<_> = entries
            .iter()
            .filter(|e| e.specifier == "std::collections::HashMap")
            .collect();
        assert_eq!(rust_imports.len(), 1);
        assert_eq!(rust_imports[0].category, ImportCategory::Builtin);
    }

    #[test]
    fn test_rust_import_external() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/main.rs");
        let src_idx = graph.add_file(src_path.clone(), "rust");

        // Add a RustImport edge with external crate path (serde)
        graph.graph.add_edge(
            src_idx,
            src_idx, // placeholder target
            EdgeKind::RustImport {
                path: "serde::Deserialize".into(),
            },
        );

        let entries = file_imports(&graph, &root, &src_path).unwrap();
        let serde_imports: Vec<_> = entries
            .iter()
            .filter(|e| e.specifier == "serde::Deserialize")
            .collect();
        assert_eq!(serde_imports.len(), 1);
        assert_eq!(serde_imports[0].category, ImportCategory::External);
    }

    #[test]
    fn test_import_order_preserved() {
        let root = PathBuf::from("/tmp/test_project");
        let mut graph = CodeGraph::new();

        let src_path = root.join("src/main.rs");
        let src_idx = graph.add_file(src_path.clone(), "rust");

        // Add 3 edges: the order in which petgraph returns edges should be preserved
        let _pkg1_idx = graph.add_external_package(src_idx, "alpha", "alpha");
        let _pkg2_idx = graph.add_external_package(src_idx, "beta", "beta");
        let _pkg3_idx = graph.add_external_package(src_idx, "gamma", "gamma");

        let entries = file_imports(&graph, &root, &src_path).unwrap();

        // We should get 3 entries (order is insertion order from petgraph)
        assert_eq!(entries.len(), 3, "All 3 imports should be present");
        // Verify all 3 are present (order is guaranteed by petgraph edge insertion order)
        let specifiers: Vec<&str> = entries.iter().map(|e| e.specifier.as_str()).collect();
        assert!(specifiers.contains(&"alpha"), "alpha should be present");
        assert!(specifiers.contains(&"beta"), "beta should be present");
        assert!(specifiers.contains(&"gamma"), "gamma should be present");
    }

    #[test]
    fn test_file_not_found() {
        let graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");
        let missing_path = root.join("src/nonexistent.rs");

        let result = file_imports(&graph, &root, &missing_path);
        assert!(result.is_err(), "Missing file should return Err");
        assert!(
            result.unwrap_err().contains("File not found"),
            "Error message should mention 'File not found'"
        );
    }
}