boundary 0.27.0

A static analysis tool for evaluating DDD and Hexagonal Architecture
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
use std::path::{Path, PathBuf};
use std::process;

use anyhow::{Context, Result};
use clap::{Parser, Subcommand, ValueEnum};
use rayon::prelude::*;
use walkdir::WalkDir;

use boundary_core::analyzer::LanguageAnalyzer;
use boundary_core::config::Config;
use boundary_core::graph::DependencyGraph;
use boundary_core::layer::LayerClassifier;
use boundary_core::metrics;
use boundary_core::pipeline::{self, reclassify_infra_handlers, AnalysisPipeline};
use boundary_core::types::{DependencyKind, Severity};

use boundary_go::GoAnalyzer;
use boundary_java::JavaAnalyzer;
use boundary_report::{json, text};
use boundary_rust::RustAnalyzer;
use boundary_typescript::TypeScriptAnalyzer;

#[derive(Debug, Clone, Copy, ValueEnum)]
enum OutputFormat {
    Text,
    Json,
    Markdown,
}

#[derive(Parser)]
#[command(name = "boundary")]
#[command(about = "Analyze and score DDD/Hexagonal architecture boundaries")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Analyze a codebase and print a full architecture report
    Analyze {
        /// Path to the project root
        path: PathBuf,
        /// Config file path (defaults to .boundary.toml in project root)
        #[arg(short, long)]
        config: Option<PathBuf>,
        /// Output format
        #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
        format: OutputFormat,
        /// Compact output (single-line JSON, no colors for text)
        #[arg(long)]
        compact: bool,
        /// Languages to analyze (auto-detect if not specified)
        #[arg(long, value_delimiter = ',')]
        languages: Option<Vec<String>>,
        /// Use incremental analysis (cache unchanged files)
        #[arg(long)]
        incremental: bool,
        /// Analyze each service independently (monorepo support)
        #[arg(long)]
        per_service: bool,
        /// Output only the architecture score (one line)
        #[arg(long)]
        score_only: bool,
        /// Ignore specific rule IDs (comma-separated, e.g. PA001,L005)
        #[arg(long, value_delimiter = ',')]
        ignore: Option<Vec<String>>,
    },
    /// Analyze and exit with code 0 (pass) or 1 (fail)
    Check {
        /// Path to the project root
        path: PathBuf,
        /// Minimum severity to cause failure
        #[arg(long, default_value = "error")]
        fail_on: String,
        /// Config file path
        #[arg(short, long)]
        config: Option<PathBuf>,
        /// Output format
        #[arg(long, value_enum, default_value_t = OutputFormat::Text)]
        format: OutputFormat,
        /// Compact output (single-line JSON, no colors for text)
        #[arg(long)]
        compact: bool,
        /// Languages to analyze (auto-detect if not specified)
        #[arg(long, value_delimiter = ',')]
        languages: Option<Vec<String>>,
        /// Save analysis snapshot for evolution tracking
        #[arg(long)]
        track: bool,
        /// Fail if architecture score regresses from last snapshot
        #[arg(long)]
        no_regression: bool,
        /// Use incremental analysis (cache unchanged files)
        #[arg(long)]
        incremental: bool,
        /// Analyze each service independently (monorepo support)
        #[arg(long)]
        per_service: bool,
        /// Ignore specific rule IDs (comma-separated, e.g. PA001,L005)
        #[arg(long, value_delimiter = ',')]
        ignore: Option<Vec<String>>,
    },
    /// Create a default .boundary.toml configuration file
    Init {
        /// Overwrite existing config
        #[arg(long)]
        force: bool,
    },
    /// Generate an architecture diagram (Mermaid or DOT format)
    Diagram {
        /// Path to the project root
        path: PathBuf,
        /// Config file path
        #[arg(short, long)]
        config: Option<PathBuf>,
        /// Diagram type
        #[arg(long, value_enum, default_value_t = DiagramType::Layers)]
        diagram_type: DiagramType,
        /// Languages to analyze (auto-detect if not specified)
        #[arg(long, value_delimiter = ',')]
        languages: Option<Vec<String>>,
    },
    /// Generate a detailed forensics report for a module
    Forensics {
        /// Path to the module directory
        path: PathBuf,
        /// Project root (auto-detected if not specified)
        #[arg(long)]
        project_root: Option<PathBuf>,
        /// Config file path
        #[arg(short, long)]
        config: Option<PathBuf>,
        /// Languages to analyze (auto-detect if not specified)
        #[arg(long, value_delimiter = ',')]
        languages: Option<Vec<String>>,
        /// Write output to file instead of stdout
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum DiagramType {
    Layers,
    Dependencies,
    Dot,
    DotDependencies,
}

fn main() {
    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Analyze {
            path,
            config,
            format,
            compact,
            languages,
            incremental,
            per_service,
            score_only,
            ignore,
        } => cmd_analyze(
            &path,
            config.as_deref(),
            format,
            compact,
            languages.as_deref(),
            incremental,
            per_service,
            score_only,
            ignore.as_deref(),
        ),
        Commands::Check {
            path,
            fail_on,
            config,
            format,
            compact,
            languages,
            track,
            no_regression,
            incremental,
            per_service,
            ignore,
        } => cmd_check(
            &path,
            &fail_on,
            config.as_deref(),
            format,
            compact,
            languages.as_deref(),
            track,
            no_regression,
            incremental,
            per_service,
            ignore.as_deref(),
        ),
        Commands::Init { force } => cmd_init(force),
        Commands::Diagram {
            path,
            config,
            diagram_type,
            languages,
        } => cmd_diagram(&path, config.as_deref(), diagram_type, languages.as_deref()),
        Commands::Forensics {
            path,
            project_root,
            config,
            languages,
            output,
        } => cmd_forensics(
            &path,
            project_root.as_deref(),
            config.as_deref(),
            languages.as_deref(),
            output.as_deref(),
        ),
    };

    if let Err(e) = result {
        eprintln!("Error: {e:#}");
        process::exit(2);
    }
}

fn validate_path(path: &Path) -> Result<()> {
    if !path.exists() {
        anyhow::bail!("path '{}' does not exist", path.display());
    }
    if !path.is_dir() {
        anyhow::bail!("path '{}' is not a directory", path.display());
    }
    Ok(())
}

#[allow(clippy::too_many_arguments)]
fn cmd_analyze(
    path: &Path,
    config_path: Option<&Path>,
    format: OutputFormat,
    compact: bool,
    languages: Option<&[String]>,
    incremental: bool,
    per_service: bool,
    score_only: bool,
    ignore: Option<&[String]>,
) -> Result<()> {
    validate_path(path)?;
    let project_root = resolve_project_root(path, config_path);
    let config = load_config(&project_root, config_path)?;

    if per_service {
        let analyzers = create_analyzers(path, &config, languages)?;
        let pipeline = AnalysisPipeline::new(analyzers, config);
        let multi = pipeline.analyze_per_service(path)?;

        if score_only {
            for svc in &multi.services {
                print_score_only(&svc.service_name, svc.result.score.as_ref(), format);
            }
            return Ok(());
        }

        let report = match format {
            OutputFormat::Text => text::format_multi_service_report(&multi),
            OutputFormat::Json => json::format_multi_service_report(&multi, compact),
            OutputFormat::Markdown => {
                boundary_report::markdown::format_multi_service_report(&multi)
            }
        };
        println!("{report}");
        return Ok(());
    }

    let mut analysis = run_analysis(path, &project_root, &config, languages, incremental)?;
    filter_ignored_violations(&mut analysis.result, ignore);

    if score_only {
        let module_name = path
            .file_name()
            .map(|n| n.to_string_lossy().into_owned())
            .unwrap_or_else(|| path.to_string_lossy().into_owned());
        print_score_only(&module_name, analysis.result.score.as_ref(), format);
        return Ok(());
    }

    let report = match format {
        OutputFormat::Text => text::format_report(&analysis.result),
        OutputFormat::Json => json::format_report(&analysis.result, compact),
        OutputFormat::Markdown => boundary_report::markdown::format_report(&analysis.result),
    };
    println!("{report}");
    Ok(())
}

fn print_score_only(
    module: &str,
    score: Option<&metrics::ArchitectureScore>,
    format: OutputFormat,
) {
    let overall = score.map(|s| s.overall).unwrap_or(0.0);
    let presence = score.map(|s| s.structural_presence).unwrap_or(0.0);
    let conformance = score.map(|s| s.layer_conformance).unwrap_or(0.0);
    let compliance = score.map(|s| s.dependency_compliance).unwrap_or(0.0);
    let iface = score.map(|s| s.interface_coverage).unwrap_or(0.0);
    match format {
        OutputFormat::Json => {
            println!(
                "{{\"module\":\"{module}\",\"overall\":{overall:.1},\"structural_presence\":{presence:.1},\"layer_conformance\":{conformance:.1},\"dependency_compliance\":{compliance:.1},\"interface_coverage\":{iface:.1}}}"
            );
        }
        OutputFormat::Text | OutputFormat::Markdown => {
            println!(
                "{module}: {overall:.1}/100 (Presence: {presence:.1}, Conformance: {conformance:.1}, Compliance: {compliance:.1}, Interfaces: {iface:.1})"
            );
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn cmd_check(
    path: &Path,
    fail_on_str: &str,
    config_path: Option<&Path>,
    format: OutputFormat,
    compact: bool,
    languages: Option<&[String]>,
    track: bool,
    no_regression: bool,
    incremental: bool,
    per_service: bool,
    ignore: Option<&[String]>,
) -> Result<()> {
    validate_path(path)?;
    let project_root = resolve_project_root(path, config_path);
    let config = load_config(&project_root, config_path)?;
    let fail_on: Severity = fail_on_str.parse()?;

    if per_service {
        let analyzers = create_analyzers(path, &config, languages)?;
        let pipeline = AnalysisPipeline::new(analyzers, config);
        let multi = pipeline.analyze_per_service(path)?;

        let report = match format {
            OutputFormat::Text => text::format_multi_service_report(&multi),
            OutputFormat::Json => json::format_multi_service_report(&multi, compact),
            OutputFormat::Markdown => {
                boundary_report::markdown::format_multi_service_report(&multi)
            }
        };
        println!("{report}");

        // Check if any service has failing violations
        let has_failures = multi
            .services
            .iter()
            .any(|s| s.result.violations.iter().any(|v| v.severity >= fail_on));
        if has_failures {
            process::exit(1);
        }
        return Ok(());
    }

    let mut analysis = run_analysis(path, &project_root, &config, languages, incremental)?;
    filter_ignored_violations(&mut analysis.result, ignore);

    // Evolution tracking
    if track {
        boundary_core::evolution::save_snapshot(path, &analysis.result)?;
    }
    if no_regression {
        if let Some(trend) = boundary_core::evolution::check_regression(path, &analysis.result)? {
            let (report, _) = match format {
                OutputFormat::Text => text::format_check(&analysis.result, fail_on),
                OutputFormat::Json => json::format_check(&analysis.result, fail_on, compact),
                OutputFormat::Markdown => {
                    boundary_report::markdown::format_check(&analysis.result, fail_on)
                }
            };
            println!("{report}");
            eprintln!("Architecture regression detected!");
            eprintln!(
                "  Score: {:.1} -> {:.1} ({:+.1})",
                trend.previous_score, trend.current_score, trend.score_delta
            );
            eprintln!(
                "  Violations: {} -> {} ({:+})",
                trend.previous_violations, trend.current_violations, trend.violation_delta
            );
            for rt in &trend.rule_trends {
                if rt.delta != 0 {
                    eprintln!(
                        "    {}: {} -> {} ({:+})",
                        rt.rule_id, rt.previous_count, rt.current_count, rt.delta
                    );
                }
            }
            process::exit(1);
        }
    }

    let (report, passed) = match format {
        OutputFormat::Text => text::format_check(&analysis.result, fail_on),
        OutputFormat::Json => json::format_check(&analysis.result, fail_on, compact),
        OutputFormat::Markdown => {
            boundary_report::markdown::format_check(&analysis.result, fail_on)
        }
    };
    println!("{report}");
    if !passed {
        process::exit(1);
    }
    Ok(())
}

fn cmd_init(force: bool) -> Result<()> {
    let target = PathBuf::from(".boundary.toml");
    if target.exists() && !force {
        anyhow::bail!(".boundary.toml already exists. Use --force to overwrite.");
    }
    std::fs::write(&target, Config::default_toml())?;
    println!("Created .boundary.toml with default configuration.");
    Ok(())
}

fn cmd_diagram(
    path: &Path,
    config_path: Option<&Path>,
    diagram_type: DiagramType,
    languages: Option<&[String]>,
) -> Result<()> {
    validate_path(path)?;
    let project_root = resolve_project_root(path, config_path);
    let config = load_config(&project_root, config_path)?;
    let analysis = run_analysis(path, &project_root, &config, languages, false)?;

    let diagram = match diagram_type {
        DiagramType::Layers => boundary_report::diagram::generate_layer_diagram(&analysis.graph),
        DiagramType::Dependencies => {
            boundary_report::diagram::generate_dependency_flow(&analysis.graph)
        }
        DiagramType::Dot => boundary_report::dot::generate_layer_diagram(&analysis.graph),
        DiagramType::DotDependencies => {
            boundary_report::dot::generate_dependency_flow(&analysis.graph)
        }
    };
    println!("{diagram}");
    Ok(())
}

fn cmd_forensics(
    module_path: &Path,
    project_root_override: Option<&Path>,
    config_path: Option<&Path>,
    languages: Option<&[String]>,
    output_path: Option<&Path>,
) -> Result<()> {
    validate_path(module_path)?;

    // Canonicalize so find_project_root walks absolute ancestors
    let module_path = module_path
        .canonicalize()
        .with_context(|| format!("failed to resolve path '{}'", module_path.display()))?;

    // Determine project root
    let project_root = if let Some(root) = project_root_override {
        root.to_path_buf()
    } else {
        pipeline::find_project_root(&module_path).unwrap_or_else(|| module_path.to_path_buf())
    };

    validate_path(&project_root)?;

    let config = load_config(&project_root, config_path)?;
    let analyzers = create_analyzers(&project_root, &config, languages)?;
    let pipeline = AnalysisPipeline::new(analyzers, config);

    let full_analysis = pipeline.analyze_module(&module_path, &project_root)?;
    let forensics =
        boundary_core::forensics::build_forensics(&full_analysis, &module_path, &project_root);
    let report = boundary_report::forensics::format_forensics_report(&forensics);

    if let Some(out_path) = output_path {
        std::fs::write(out_path, &report)
            .with_context(|| format!("failed to write output to {}", out_path.display()))?;
        eprintln!("Forensics report written to {}", out_path.display());
    } else {
        println!("{report}");
    }

    Ok(())
}

/// Remove violations whose rule ID matches any of the ignored rules.
fn filter_ignored_violations(result: &mut metrics::AnalysisResult, ignore: Option<&[String]>) {
    if let Some(rules) = ignore {
        result
            .violations
            .retain(|v| !rules.iter().any(|r| r == v.kind.rule_id().as_str()));
    }
}

fn load_config(project_path: &Path, config_path: Option<&Path>) -> Result<Config> {
    match config_path {
        Some(p) => Config::load(p),
        None => Ok(Config::load_or_default(project_path)),
    }
}

/// Resolve the project root directory for path normalization.
///
/// When `--config` is explicit, derives root from the config file's parent.
/// Otherwise walks ancestors looking for `.boundary.toml` or `.git`.
/// Falls back to `analysis_path` if nothing found.
fn resolve_project_root(analysis_path: &Path, config_path: Option<&Path>) -> PathBuf {
    if let Some(cp) = config_path {
        if let Some(parent) = cp.parent() {
            if parent.exists() {
                return parent.to_path_buf();
            }
        }
    }
    pipeline::find_project_root(analysis_path).unwrap_or_else(|| analysis_path.to_path_buf())
}

/// Full analysis output including the graph for diagram generation.
pub struct FullAnalysis {
    pub result: metrics::AnalysisResult,
    pub graph: DependencyGraph,
}

/// A dependency with its resolved layer info and architecture context.
type ClassifiedDependency = (
    boundary_core::types::Dependency,
    Option<boundary_core::types::ArchLayer>,
    Option<boundary_core::types::ArchLayer>,
    bool,
    boundary_core::types::ArchitectureMode,
    bool, // to_is_cross_cutting
);

/// Extracted per-file data before merging into the graph.
struct FileResult {
    components: Vec<(
        boundary_core::types::Component,
        Option<boundary_core::types::ArchLayer>,
    )>,
    dependencies: Vec<ClassifiedDependency>,
}

/// Create analyzers based on languages config or auto-detection.
fn create_analyzers(
    project_path: &Path,
    config: &Config,
    language_override: Option<&[String]>,
) -> Result<Vec<Box<dyn LanguageAnalyzer>>> {
    let languages: Vec<String> = if let Some(langs) = language_override {
        langs.to_vec()
    } else if config.project.languages.is_empty() {
        // Auto-detect based on file extensions present
        auto_detect_languages(project_path)
    } else {
        config.project.languages.clone()
    };

    let mut analyzers: Vec<Box<dyn LanguageAnalyzer>> = Vec::new();

    for lang in &languages {
        match lang.as_str() {
            "go" => {
                analyzers.push(Box::new(
                    GoAnalyzer::new().context("failed to init Go analyzer")?,
                ));
            }
            "rust" => {
                analyzers.push(Box::new(
                    RustAnalyzer::new().context("failed to init Rust analyzer")?,
                ));
            }
            "typescript" | "ts" => {
                analyzers.push(Box::new(
                    TypeScriptAnalyzer::new().context("failed to init TypeScript analyzer")?,
                ));
            }
            "java" => {
                analyzers.push(Box::new(
                    JavaAnalyzer::new().context("failed to init Java analyzer")?,
                ));
            }
            other => {
                eprintln!("Warning: unsupported language '{other}', skipping");
            }
        }
    }

    if analyzers.is_empty() {
        anyhow::bail!("no supported language analyzers could be initialized");
    }

    Ok(analyzers)
}

/// Auto-detect languages by scanning for file extensions.
fn auto_detect_languages(project_path: &Path) -> Vec<String> {
    let mut has_go = false;
    let mut has_rust = false;
    let mut has_ts = false;
    let mut has_java = false;

    for entry in WalkDir::new(project_path)
        .into_iter()
        .filter_map(|e| e.ok())
        .take(1000)
    {
        if let Some(ext) = entry.path().extension() {
            match ext.to_str() {
                Some("go") => has_go = true,
                Some("rs") => has_rust = true,
                Some("ts" | "tsx") => {
                    // Skip .d.ts files
                    if !entry.path().to_string_lossy().ends_with(".d.ts") {
                        has_ts = true;
                    }
                }
                Some("java") => has_java = true,
                _ => {}
            }
        }
        if has_go && has_rust && has_ts && has_java {
            break;
        }
    }

    let mut languages = Vec::new();
    if has_go {
        languages.push("go".to_string());
    }
    if has_rust {
        languages.push("rust".to_string());
    }
    if has_ts {
        languages.push("typescript".to_string());
    }
    if has_java {
        languages.push("java".to_string());
    }
    if languages.is_empty() {
        // Fallback to Go for backward compat
        languages.push("go".to_string());
    }
    languages
}

fn run_analysis(
    project_path: &Path,
    project_root: &Path,
    config: &Config,
    language_override: Option<&[String]>,
    incremental: bool,
) -> Result<FullAnalysis> {
    let analyzers = create_analyzers(project_path, config, language_override)?;
    let classifier = LayerClassifier::new(&config.layers);
    let mut graph = DependencyGraph::new();
    let mut total_deps = 0usize;
    let mut total_files = 0usize;
    let mut all_components = Vec::new();
    let mut all_dependencies: Vec<boundary_core::types::Dependency> = Vec::new();

    // Load cache if incremental
    let mut cache = if incremental {
        boundary_core::cache::AnalysisCache::load(project_path).unwrap_or_default()
    } else {
        boundary_core::cache::AnalysisCache::new()
    };

    for analyzer in &analyzers {
        let extensions: Vec<&str> = analyzer.file_extensions().to_vec();

        // Walk directory and find matching files
        let source_files: Vec<PathBuf> = WalkDir::new(project_path)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| {
                let p = e.path();
                let matches_ext = p
                    .extension()
                    .is_some_and(|ext| extensions.iter().any(|e| ext == *e));
                if !matches_ext {
                    return false;
                }
                let path_str = p.to_string_lossy();
                // Common exclusions
                !path_str.contains("vendor/")
                    && !path_str.contains("/target/")
                    && !path_str.ends_with("_test.go")
                    && !path_str.ends_with(".d.ts")
            })
            .map(|e| e.into_path())
            .collect();

        if source_files.is_empty() {
            continue;
        }
        total_files += source_files.len();

        // Parse and extract in parallel
        let file_results: Vec<(String, FileResult, String)> = source_files
            .par_iter()
            .filter_map(|file_path| {
                let content = match std::fs::read_to_string(file_path) {
                    Ok(c) => c,
                    Err(e) => {
                        eprintln!("Warning: failed to read {}: {e}", file_path.display());
                        return None;
                    }
                };

                let rel_path = file_path
                    .strip_prefix(project_root)
                    .unwrap_or(file_path)
                    .to_string_lossy()
                    .to_string();

                let is_cross_cutting = classifier.is_cross_cutting(&rel_path);
                let arch_mode = classifier.architecture_mode(&rel_path);

                // Check cache for incremental analysis
                if incremental {
                    if let Some(cached) = cache.get(&rel_path, &content) {
                        let file_layer = classifier.classify(&rel_path);
                        let components: Vec<_> = cached
                            .components
                            .iter()
                            .map(|comp| {
                                let mut comp = comp.clone();
                                if comp.layer.is_none() {
                                    comp.layer = file_layer;
                                }
                                comp.is_cross_cutting = is_cross_cutting;
                                comp.architecture_mode = arch_mode;
                                reclassify_infra_handlers(&mut comp);
                                let layer = comp.layer;
                                (comp, layer)
                            })
                            .collect();

                        let dependencies: Vec<_> = cached
                            .dependencies
                            .iter()
                            .filter(|dep| {
                                matches!(dep.kind, DependencyKind::MethodCall)
                                    || !dep
                                        .import_path
                                        .as_deref()
                                        .is_some_and(|p| analyzer.is_stdlib_import(p))
                            })
                            .map(|dep| {
                                let to_layer = dep
                                    .import_path
                                    .as_deref()
                                    .and_then(|p| classifier.classify_import(p));
                                let to_is_cross_cutting = dep
                                    .import_path
                                    .as_deref()
                                    .is_some_and(|p| classifier.is_cross_cutting_import(p));
                                let from_layer = classifier.classify(&rel_path);
                                (
                                    dep.clone(),
                                    from_layer,
                                    to_layer,
                                    is_cross_cutting,
                                    arch_mode,
                                    to_is_cross_cutting,
                                )
                            })
                            .collect();

                        return Some((
                            rel_path,
                            FileResult {
                                components,
                                dependencies,
                            },
                            content,
                        ));
                    }
                }

                let parsed = match analyzer.parse_file(file_path, &content) {
                    Ok(p) => p,
                    Err(e) => {
                        eprintln!("Warning: failed to parse {}: {e}", file_path.display());
                        return None;
                    }
                };

                // Extract and classify components
                let mut components_raw = analyzer.extract_components(&parsed);
                let file_layer = classifier.classify(&rel_path);

                let components: Vec<_> = components_raw
                    .drain(..)
                    .map(|mut comp| {
                        if comp.layer.is_none() {
                            comp.layer = file_layer;
                        }
                        comp.is_cross_cutting = is_cross_cutting;
                        comp.architecture_mode = arch_mode;
                        reclassify_infra_handlers(&mut comp);
                        let layer = comp.layer;
                        (comp, layer)
                    })
                    .collect();

                // Extract dependencies with layer info
                let deps = analyzer.extract_dependencies(&parsed);
                let dependencies: Vec<_> = deps
                    .into_iter()
                    .filter(|dep| {
                        // MethodCall (init function) deps use local aliases, not module paths;
                        // never treat them as stdlib. Only filter Import-kind deps.
                        matches!(dep.kind, DependencyKind::MethodCall)
                            || !dep
                                .import_path
                                .as_deref()
                                .is_some_and(|p| analyzer.is_stdlib_import(p))
                    })
                    .map(|dep| {
                        let to_layer = dep
                            .import_path
                            .as_deref()
                            .and_then(|p| classifier.classify_import(p));
                        let to_is_cross_cutting = dep
                            .import_path
                            .as_deref()
                            .is_some_and(|p| classifier.is_cross_cutting_import(p));
                        let from_layer = classifier.classify(&rel_path);
                        (
                            dep,
                            from_layer,
                            to_layer,
                            is_cross_cutting,
                            arch_mode,
                            to_is_cross_cutting,
                        )
                    })
                    .collect();

                Some((
                    rel_path,
                    FileResult {
                        components,
                        dependencies,
                    },
                    content,
                ))
            })
            .collect();

        // Collect rel_paths for pruning
        let current_files: Vec<String> = file_results.iter().map(|(p, _, _)| p.clone()).collect();

        // First pass: add all source file components and update cache
        for (rel_path, fr, content) in &file_results {
            if incremental {
                let cached_components: Vec<_> =
                    fr.components.iter().map(|(comp, _)| comp.clone()).collect();
                let cached_deps: Vec<_> = fr
                    .dependencies
                    .iter()
                    .map(|(dep, _, _, _, _, _)| dep.clone())
                    .collect();
                cache.insert(
                    rel_path.clone(),
                    content,
                    boundary_core::cache::CachedFileResult {
                        hash: String::new(),
                        components: cached_components,
                        dependencies: cached_deps,
                    },
                );
            }

            for (comp, _) in &fr.components {
                graph.add_component(comp);
                all_components.push(comp.clone());
            }
        }

        // Second pass: add dependencies
        for (_rel_path, fr, _content) in file_results {
            for (dep, from_layer, to_layer, is_cc, arch_mode, to_is_cc) in &fr.dependencies {
                graph.ensure_node_with_mode(&dep.from, *from_layer, *is_cc, *arch_mode);
                graph.ensure_node(&dep.to, *to_layer, *to_is_cc);
                graph.add_dependency(dep);
                all_dependencies.push(dep.clone());
            }
            total_deps += fr.dependencies.len();
        }

        // Prune deleted files from cache
        if incremental {
            cache.prune(&current_files);
        }
    }

    // Save cache if incremental
    if incremental {
        if let Err(e) = cache.save(project_path) {
            eprintln!("Warning: failed to save analysis cache: {e}");
        }
    }

    // Mark dependency-only nodes as external if they don't correspond to any
    // analyzed source file. Source components (added via add_component) have
    // kind: Some(...); dependency-target nodes (via ensure_node) have kind: None.
    // Among kind:None nodes, check if the import path matches any source directory.
    let source_ids: std::collections::HashSet<_> = all_components.iter().map(|c| &c.id).collect();
    let source_rel_dirs: std::collections::HashSet<String> = all_components
        .iter()
        .filter_map(|c| {
            let rel = c
                .location
                .file
                .strip_prefix(project_root)
                .unwrap_or(&c.location.file);
            rel.parent().map(|p| p.to_string_lossy().replace('\\', "/"))
        })
        .collect();
    let project_root_str = project_root.to_string_lossy().replace('\\', "/");
    let external_ids: Vec<_> = graph
        .nodes()
        .iter()
        .filter(|n| {
            if source_ids.contains(&n.id) {
                return false;
            }
            // Extract the path portion before "::" (component IDs use path::name format)
            let id = n.id.0.replace('\\', "/");
            let path_part = id.split("::").next().unwrap_or(&id);
            // Relative imports (starting with . or ..) are always internal
            if path_part.starts_with('.') {
                return false;
            }
            // Rust crate-internal imports
            if path_part.starts_with("crate") {
                return false;
            }
            // Absolute paths under the project directory are internal
            if path_part.starts_with(project_root_str.as_str()) {
                return false;
            }
            // Also normalize dots to slashes for Java-style package names
            let path_normalized = path_part.replace('.', "/");
            // Check if this path corresponds to any analyzed source directory
            let is_internal = source_rel_dirs.iter().any(|dir| {
                if dir.is_empty() {
                    return false;
                }
                // Direct suffix match (Go-style fully-qualified imports)
                if path_part.ends_with(dir.as_str()) {
                    return true;
                }
                // Check if import path and source dir share consecutive path segments
                // (catches Java dot-notation imports like com.example.domain.user)
                let dir_segments: Vec<&str> = dir.split('/').collect();
                if dir_segments.len() >= 2 {
                    for window in dir_segments.windows(2) {
                        let pair = format!("{}/{}", window[0], window[1]);
                        if path_normalized.contains(&pair) {
                            return true;
                        }
                    }
                }
                false
            });
            !is_internal
        })
        .map(|n| n.id.clone())
        .collect();
    for id in &external_ids {
        graph.mark_external(id);
    }

    let result = metrics::build_result(
        &graph,
        config,
        total_deps,
        &all_components,
        total_files,
        &all_dependencies,
    );
    Ok(FullAnalysis { result, graph })
}