cargo-arc 0.2.1

Visualize crate and module dependencies in Cargo workspaces
Documentation
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
use anyhow::Result;
use clap::Parser;
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;
use tracing_subscriber::EnvFilter;

use crate::analyze::{
    AnalysisBackend, FeatureConfig, ReExportMap, analyze_workspace, collect_crate_exports,
    collect_crate_reexports, externals::analyze_externals, normalize_crate_name,
};
use crate::graph::ArcGraph;
use crate::layout::{Cycle, ElementaryCycles, LayoutIR, build_layout};
use crate::model::{CrateExportMap, ModulePathMap, WorkspaceCrates};
use crate::render::{RenderConfig, render};
use crate::volatility::{VolatilityAnalyzer, VolatilityConfig};
use std::path::Path;

/// Cargo subcommand wrapper for `cargo arc`
#[derive(Parser)]
#[command(name = "cargo", bin_name = "cargo")]
pub enum Cargo {
    /// Visualize workspace dependencies as SVG
    #[command(name = "arc", version, author)]
    Arc(Args),
}

#[allow(clippy::struct_excessive_bools)] // CLI flags map 1:1 to fields
#[derive(Parser)]
pub struct Args {
    /// Output file (default: stdout)
    #[arg(short, long)]
    pub output: Option<PathBuf>,

    /// Path to Cargo.toml (default: ./Cargo.toml)
    #[arg(short, long, default_value = "Cargo.toml")]
    pub manifest_path: PathBuf,

    /// Comma-separated list of features to activate
    #[arg(long, value_delimiter = ',')]
    pub features: Vec<String>,

    /// Activate all available features
    #[arg(long)]
    pub all_features: bool,

    /// Do not activate the `default` feature
    #[arg(long)]
    pub no_default_features: bool,

    /// Include test code in analysis (unit tests, integration tests)
    #[arg(long)]
    pub include_tests: bool,

    /// Validate dependency graph (exit 1 if cycles found)
    #[arg(long)]
    pub check: bool,

    /// Enable debug output to stderr (shows filtering decisions)
    #[arg(long)]
    pub debug: bool,

    /// Print volatility report (text) instead of dependency SVG
    #[arg(long)]
    pub volatility: bool,

    /// Disable git volatility analysis in SVG output
    #[arg(long)]
    pub no_volatility: bool,

    /// Volatility analysis period in months (default: 6)
    #[arg(long, default_value = "6")]
    pub volatility_months: usize,

    /// Low volatility threshold (default: 2)
    #[arg(long, default_value = "2")]
    pub volatility_low: usize,

    /// High volatility threshold (default: 10)
    #[arg(long, default_value = "10")]
    pub volatility_high: usize,

    /// Include external crate dependencies in visualization
    #[arg(long)]
    pub externals: bool,

    /// Include transitive external dependencies (requires --externals)
    #[arg(long)]
    pub transitive_deps: bool,

    /// Initial expand level for SVG (0=crates only, 1=direct modules, etc.)
    #[arg(long)]
    pub expand_level: Option<usize>,

    /// Use rust-analyzer HIR backend instead of syn (slower but may catch more)
    #[cfg(feature = "hir")]
    #[arg(long)]
    pub hir: bool,
}

#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub fn run(args: Args) -> Result<()> {
    if args.debug {
        tracing_subscriber::fmt()
            .with_env_filter(
                EnvFilter::from_default_env().add_directive("cargo_arc=debug".parse().unwrap()),
            )
            .with_target(false)
            .with_writer(std::io::stderr)
            .init();
    }

    if args.check {
        let feature_config = FeatureConfig {
            features: args.features,
            all_features: args.all_features,
            no_default_features: args.no_default_features,
            include_tests: args.include_tests,
            debug: args.debug,
        };

        #[cfg(feature = "hir")]
        let use_hir = args.hir;
        #[cfg(not(feature = "hir"))]
        let use_hir = false;

        let graph = build_dependency_graph(
            &args.manifest_path,
            &feature_config,
            use_hir,
            args.externals,
            args.transitive_deps,
        )?;
        let cycles = graph.production_subgraph().elementary_cycles();
        if cycles.is_empty() {
            return Ok(());
        }
        eprint!("{}", format_cycle_errors(&graph, &cycles));
        anyhow::bail!("dependency cycle(s) detected");
    }

    let vol_config = VolatilityConfig {
        months: args.volatility_months,
        low_threshold: args.volatility_low,
        high_threshold: args.volatility_high,
    };

    if args.volatility {
        return run_volatility_report(&args.manifest_path, vol_config, args.output.as_ref());
    }

    let feature_config = FeatureConfig {
        features: args.features,
        all_features: args.all_features,
        no_default_features: args.no_default_features,
        include_tests: args.include_tests,
        debug: args.debug,
    };

    #[cfg(feature = "hir")]
    let use_hir = args.hir;
    #[cfg(not(feature = "hir"))]
    let use_hir = false;

    let graph = build_dependency_graph(
        &args.manifest_path,
        &feature_config,
        use_hir,
        args.externals,
        args.transitive_deps,
    )?;
    let cycles = graph.production_subgraph().elementary_cycles();
    let mut layout = build_layout(&graph, &cycles);

    if !args.no_volatility {
        enrich_volatility(&mut layout, &args.manifest_path, vol_config);
    }

    let config = RenderConfig {
        expand_level: args.expand_level,
        ..RenderConfig::default()
    };
    let svg = render(&layout, &config);
    write_output(&svg, args.output.as_ref())
}

fn resolve_repo_path(manifest_path: &Path) -> &Path {
    manifest_path
        .parent()
        .filter(|parent| !parent.as_os_str().is_empty())
        .unwrap_or(Path::new("."))
}

fn write_output(content: &str, output: Option<&PathBuf>) -> Result<()> {
    match output {
        Some(path) => fs::write(path, content)?,
        None => io::stdout().write_all(content.as_bytes())?,
    }
    Ok(())
}

fn run_volatility_report(
    manifest_path: &Path,
    vol_config: VolatilityConfig,
    output: Option<&PathBuf>,
) -> Result<()> {
    let repo_path = resolve_repo_path(manifest_path);
    let mut analyzer = VolatilityAnalyzer::new(vol_config);
    analyzer.analyze(repo_path)?;
    let report = analyzer.format_report();
    write_output(&report, output)
}

fn build_dependency_graph(
    manifest_path: &Path,
    feature_config: &FeatureConfig,
    use_hir: bool,
    externals: bool,
    transitive_deps: bool,
) -> Result<ArcGraph> {
    let crates = analyze_workspace(manifest_path, feature_config)?;
    let workspace_crates: WorkspaceCrates = crates.iter().map(|krate| krate.name.clone()).collect();
    let backend = AnalysisBackend::new(manifest_path, feature_config, use_hir)?;

    let all_module_paths: ModulePathMap = crates
        .iter()
        .map(|krate| {
            let name = normalize_crate_name(&krate.name);
            let paths = backend.collect_module_paths(krate);
            (name, paths)
        })
        .collect();

    let crate_exports: CrateExportMap = crates
        .iter()
        .map(|krate| {
            let name = normalize_crate_name(&krate.name);
            let exports = collect_crate_exports(&krate.path);
            (name, exports)
        })
        .collect();

    let reexport_map: ReExportMap = crates
        .iter()
        .map(|krate| {
            let name = normalize_crate_name(&krate.name);
            let exports = collect_crate_reexports(
                krate,
                &all_module_paths,
                &workspace_crates,
                &crate_exports,
            );
            (name, exports)
        })
        .collect();

    // Run externals analysis before module analysis so crate_name_map
    // is available for use-parser resolution of external crate imports.
    let ext_result = if externals {
        use cargo_metadata::MetadataCommand;
        let metadata = MetadataCommand::new().manifest_path(manifest_path).exec()?;
        Some(analyze_externals(&metadata, transitive_deps))
    } else {
        None
    };

    let empty_name_map = std::collections::HashMap::new();
    let modules: Vec<_> = crates
        .iter()
        .filter_map(|krate| {
            let name = normalize_crate_name(&krate.name);
            let ext_names = ext_result
                .as_ref()
                .and_then(|r| r.crate_name_map.get(&name))
                .unwrap_or(&empty_name_map);
            match backend.analyze_modules(
                krate,
                &workspace_crates,
                &all_module_paths,
                &crate_exports,
                &reexport_map,
                ext_names,
            ) {
                Ok(tree) => Some(tree),
                Err(err) => {
                    tracing::warn!("Skipping crate {}: {err}", krate.name);
                    None
                }
            }
        })
        .collect();

    Ok(ArcGraph::build(&crates, &modules, ext_result.as_ref()))
}

/// Format detected cycles as compiler-style error messages.
///
/// Returns an empty string when `cycles` is empty. Otherwise produces one
/// `error[cycle]:` line per cycle (using `<->` for direct / `->` chains for
/// transitive) followed by a summary line.
fn format_cycle_errors(graph: &ArcGraph, cycles: &[Cycle]) -> String {
    use std::fmt::Write;

    if cycles.is_empty() {
        return String::new();
    }

    let mut output = String::new();
    for cycle in cycles {
        let names: Vec<&str> = cycle.path.iter().map(|&idx| graph[idx].name()).collect();
        if names.len() == 2 {
            let _ = writeln!(output, "error[cycle]: {} <-> {}", names[0], names[1]);
        } else {
            let _ = writeln!(
                output,
                "error[cycle]: {} -> {}",
                names.join(" -> "),
                names[0]
            );
        }
    }
    let _ = write!(
        output,
        "\nerror: found {} cycle(s) in dependency graph\n",
        cycles.len()
    );
    output
}

fn enrich_volatility(layout: &mut LayoutIR, manifest_path: &Path, vol_config: VolatilityConfig) {
    let repo_path = resolve_repo_path(manifest_path);
    let mut analyzer = VolatilityAnalyzer::new(vol_config);
    match analyzer.analyze(repo_path) {
        Ok(()) => {
            for item in &mut layout.items {
                if let Some(ref path) = item.source_path {
                    let vol = analyzer.get_volatility(path);
                    let count = analyzer.get_change_count(path);
                    item.volatility = Some((vol, count));
                }
            }
        }
        Err(err) => {
            tracing::warn!("Volatility analysis skipped: {err}");
        }
    }
}

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

    /// Helper to parse Args via Cargo wrapper
    fn parse_args(args: &[&str]) -> Args {
        let Cargo::Arc(args) = Cargo::parse_from(args);
        args
    }

    use crate::graph::Node;
    use crate::layout::Cycle;
    use petgraph::graph::NodeIndex;

    /// Build a test graph with named module nodes.
    fn test_graph(names: &[&str]) -> (ArcGraph, Vec<NodeIndex>) {
        let mut graph = ArcGraph::new();
        let crate_idx = graph.add_node(Node::Crate {
            name: "test".into(),
            path: "/test".into(),
        });
        let indices: Vec<_> = names
            .iter()
            .map(|name| {
                graph.add_node(Node::Module {
                    name: (*name).into(),
                    crate_idx,
                })
            })
            .collect();
        (graph, indices)
    }

    #[test]
    fn test_parse_check_flag() {
        let args = parse_args(&["cargo", "arc", "--check"]);
        assert!(args.check);
    }

    #[test]
    fn test_parse_check_flag_default() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(!args.check);
    }

    #[test]
    fn test_format_cycle_errors_transitive() {
        let (graph, idx) = test_graph(&["A", "B", "C"]);
        let cycles = vec![Cycle {
            path: vec![idx[0], idx[1], idx[2]],
        }];
        let output = format_cycle_errors(&graph, &cycles);
        assert!(output.contains("error[cycle]: A -> B -> C -> A"));
    }

    #[test]
    fn test_format_cycle_errors_direct() {
        let (graph, idx) = test_graph(&["A", "B"]);
        let cycles = vec![Cycle {
            path: vec![idx[0], idx[1]],
        }];
        let output = format_cycle_errors(&graph, &cycles);
        assert!(output.contains("error[cycle]: A <-> B"));
    }

    #[test]
    fn test_format_cycle_errors_empty() {
        let (graph, _) = test_graph(&["A", "B"]);
        let output = format_cycle_errors(&graph, &[]);
        assert!(output.is_empty());
    }

    #[test]
    fn test_format_cycle_errors_summary() {
        let (graph, idx) = test_graph(&["A", "B", "C", "D"]);
        let cycles = vec![
            Cycle {
                path: vec![idx[0], idx[1]],
            },
            Cycle {
                path: vec![idx[2], idx[3]],
            },
        ];
        let output = format_cycle_errors(&graph, &cycles);
        assert!(output.contains("error: found 2 cycle(s) in dependency graph"));
    }

    #[test]
    fn test_cli_default_args() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(args.output.is_none());
        assert_eq!(args.manifest_path, PathBuf::from("Cargo.toml"));
    }

    #[test]
    fn test_cli_features_parsing() {
        let args = parse_args(&["cargo", "arc", "--features", "web,server"]);
        assert_eq!(args.features, vec!["web", "server"]);
    }

    #[test]
    fn test_cli_all_features() {
        let args = parse_args(&["cargo", "arc", "--all-features"]);
        assert!(args.all_features);
    }

    #[test]
    fn test_cli_include_tests_flag() {
        let args = parse_args(&["cargo", "arc", "--include-tests"]);
        assert!(args.include_tests);
    }

    #[test]
    fn test_cli_no_default_features_flag() {
        let args = parse_args(&["cargo", "arc", "--no-default-features"]);
        assert!(args.no_default_features);
    }

    #[test]
    fn test_cli_volatility_flag() {
        let args = parse_args(&["cargo", "arc", "--volatility"]);
        assert!(args.volatility);
    }

    #[test]
    fn test_cli_no_volatility_flag() {
        let args = parse_args(&["cargo", "arc", "--no-volatility"]);
        assert!(args.no_volatility);
    }

    #[test]
    fn test_cli_volatility_months() {
        let args = parse_args(&["cargo", "arc", "--volatility-months", "3"]);
        assert_eq!(args.volatility_months, 3);
    }

    #[test]
    fn test_cli_volatility_thresholds() {
        let args = parse_args(&[
            "cargo",
            "arc",
            "--volatility-low",
            "5",
            "--volatility-high",
            "20",
        ]);
        assert_eq!(args.volatility_low, 5);
        assert_eq!(args.volatility_high, 20);
    }

    #[test]
    fn test_parse_externals_flag() {
        let args = parse_args(&["cargo", "arc", "--externals"]);
        assert!(args.externals);
    }

    #[test]
    fn test_parse_externals_flag_default() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(!args.externals);
    }

    #[test]
    fn test_parse_transitive_deps_flag() {
        let args = parse_args(&["cargo", "arc", "--externals", "--transitive-deps"]);
        assert!(args.externals);
        assert!(args.transitive_deps);
    }

    #[test]
    fn test_parse_transitive_deps_flag_default() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(!args.transitive_deps);
    }

    #[test]
    fn test_parse_expand_level() {
        let args = parse_args(&["cargo", "arc", "--expand-level", "0"]);
        assert_eq!(args.expand_level, Some(0));
    }

    #[test]
    fn test_parse_expand_level_two() {
        let args = parse_args(&["cargo", "arc", "--expand-level", "2"]);
        assert_eq!(args.expand_level, Some(2));
    }

    #[test]
    fn test_parse_expand_level_default() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(args.expand_level.is_none());
    }

    #[test]
    fn test_cli_volatility_config_defaults() {
        let args = parse_args(&["cargo", "arc"]);
        assert!(!args.no_volatility);
        assert_eq!(args.volatility_months, 6);
        assert_eq!(args.volatility_low, 2);
        assert_eq!(args.volatility_high, 10);
    }

    #[test]
    #[ignore] // Smoke test - requires rust-analyzer (~30s)
    fn test_run_with_output_file() {
        let temp = tempfile::NamedTempFile::new().unwrap();
        let args = Args {
            output: Some(temp.path().to_path_buf()),
            manifest_path: PathBuf::from("Cargo.toml"),
            features: vec![],
            all_features: false,
            no_default_features: false,
            include_tests: false,
            check: false,
            debug: false,
            volatility: false,
            no_volatility: false,
            volatility_months: 6,
            volatility_low: 2,
            volatility_high: 10,
            externals: false,
            transitive_deps: false,
            expand_level: None,
            #[cfg(feature = "hir")]
            hir: false,
        };
        let result = run(args);
        assert!(result.is_ok());
        let content = std::fs::read_to_string(temp.path()).unwrap();
        assert!(content.contains("<svg"));
    }
}