meta-ast 0.7.0

Polyglot static-analysis engine: extract symbols and cross-language dependency graphs from 9 supported source languages, with optional MetaCall deployment manifest generation.
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
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
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
//! Per-language external dependency resolution.
//!
//! Classifies `ExternalNode` entries (created during graph builder import
//! resolution) into resolved dependencies with package name and version.
//! Lockfiles are preferred over manifests for exact pinning.
//! C/C++ relies on best-effort classification only.

use std::path::Path;

use crate::graph::node::{DependencySource, ExternalClassification, ExternalNode};
use crate::language::LangId;

/// A resolved dependency entry for the pod manifest.
#[derive(Debug, Clone, serde::Serialize)]
pub struct DependencyEntry {
    pub name: String,
    pub version: Option<String>,
    pub language: LangId,
    pub source: DependencySource,
}

/// Resolve all external nodes in a graph and return per-pod dependency lists.
///
/// Walks Import edges from each pod's files to ExternalNode targets,
/// classifies each external, and groups results by pod ID.
pub fn resolve_dependencies(
    graph: &crate::graph::CodeGraph,
    partition: &crate::deploy::pod::PodPartition,
    project_root: &Path,
) -> std::collections::HashMap<usize, Vec<DependencyEntry>> {
    let mut deps: std::collections::HashMap<usize, Vec<DependencyEntry>> =
        std::collections::HashMap::new();

    // Build FileId -> pod_id lookup.
    let mut file_to_pod: std::collections::HashMap<crate::model::FileId, usize> =
        std::collections::HashMap::new();
    for pod in &partition.pods {
        for &fid in &pod.files {
            file_to_pod.insert(fid, pod.id);
        }
    }

    let g = graph.graph();

    for edge_idx in g.edge_indices() {
        let weight = &g[edge_idx];
        if weight.kind != crate::graph::EdgeKind::Import {
            continue;
        }
        let Some((src, dst)) = g.edge_endpoints(edge_idx) else {
            continue;
        };

        // Source must be a file in a known pod.
        let src_fid = match &g[src] {
            crate::graph::NodeData::File(f) => f.id,
            crate::graph::NodeData::Symbol(s) => s.file_id,
            _ => continue,
        };
        let Some(&pod_id) = file_to_pod.get(&src_fid) else {
            continue;
        };

        // Target must be an ExternalNode.
        let ext = match &g[dst] {
            crate::graph::NodeData::External(e) => e,
            _ => continue,
        };

        let classification = classify_external(ext, project_root);
        let entry = match &classification {
            ExternalClassification::Classified {
                package_name,
                version,
                language,
                source,
            } => DependencyEntry {
                name: package_name.clone(),
                version: version.clone(),
                language: *language,
                source: *source,
            },
            ExternalClassification::Unresolved { .. } => continue,
        };

        let pod_deps = deps.entry(pod_id).or_default();
        if !pod_deps.iter().any(|d| d.name == entry.name) {
            pod_deps.push(entry);
        }
    }

    deps
}

// ── Dependency sources ─────────────────────────────────────────────

/// How a file states the version of one entry.
#[derive(Debug, Clone, Copy)]
enum EntryFormat {
    /// `name = "x"` with a `version = "..."` in the same entry, `name==1.2.3`,
    /// or yarn's `version "1.2.3"` line inside the entry.
    EntryValue,
    /// `[[package]]` blocks that carry `name` and `version`.
    TomlPackageArray,
    /// A JSON object whose dependency sections map a name to a version.
    JsonDependencyMap,
    /// `<name> (<version>)`, as Gemfile.lock writes it.
    ParenthesizedName,
    /// `<name> <version> <hash>`, matched on the exact first token.
    ExactToken,
    /// A `require` line, single or block form, carrying a `v` prefix.
    GoRequire,
    /// The file proves the ecosystem is in use and carries no version.
    Presence,
}

/// One file that can answer a dependency question.
struct SourceSpec {
    file: &'static str,
    format: EntryFormat,
    kind: DependencySource,
    /// A lockfile that does not carry the entry is skipped rather than used as
    /// the source. Python, Node and Go do that; Rust and Ruby keep the lockfile.
    needs_entry: bool,
    at_root: bool,
    in_subdirectory: bool,
}

const fn lockfile(
    file: &'static str,
    format: EntryFormat,
    needs_entry: bool,
    at_root: bool,
    in_subdirectory: bool,
) -> SourceSpec {
    SourceSpec {
        file,
        format,
        kind: DependencySource::Lockfile,
        needs_entry,
        at_root,
        in_subdirectory,
    }
}

const fn manifest(
    file: &'static str,
    format: EntryFormat,
    at_root: bool,
    in_subdirectory: bool,
) -> SourceSpec {
    SourceSpec {
        file,
        format,
        kind: DependencySource::Manifest,
        needs_entry: false,
        at_root,
        in_subdirectory,
    }
}

static PYTHON_SOURCES: [SourceSpec; 5] = [
    lockfile("uv.lock", EntryFormat::EntryValue, true, true, false),
    lockfile("poetry.lock", EntryFormat::EntryValue, true, true, false),
    lockfile("Pipfile.lock", EntryFormat::EntryValue, true, true, false),
    manifest("pyproject.toml", EntryFormat::Presence, true, true),
    manifest("requirements.txt", EntryFormat::Presence, true, true),
];

static NODE_SOURCES: [SourceSpec; 5] = [
    lockfile(
        "package-lock.json",
        EntryFormat::EntryValue,
        true,
        true,
        false,
    ),
    lockfile("yarn.lock", EntryFormat::EntryValue, true, true, false),
    lockfile("pnpm-lock.yaml", EntryFormat::EntryValue, true, true, false),
    lockfile(
        "package-lock.json",
        EntryFormat::EntryValue,
        true,
        false,
        true,
    ),
    manifest("package.json", EntryFormat::JsonDependencyMap, true, true),
];

static RUST_SOURCES: [SourceSpec; 2] = [
    lockfile(
        "Cargo.lock",
        EntryFormat::TomlPackageArray,
        false,
        true,
        false,
    ),
    manifest("Cargo.toml", EntryFormat::Presence, true, false),
];

static GO_SOURCES: [SourceSpec; 2] = [
    lockfile("go.sum", EntryFormat::ExactToken, true, true, false),
    manifest("go.mod", EntryFormat::GoRequire, true, false),
];

static RUBY_SOURCES: [SourceSpec; 2] = [
    lockfile(
        "Gemfile.lock",
        EntryFormat::ParenthesizedName,
        false,
        true,
        false,
    ),
    manifest("Gemfile", EntryFormat::Presence, true, false),
];

static C_SOURCES: [SourceSpec; 2] = [
    manifest("conanfile.txt", EntryFormat::Presence, true, false),
    manifest("vcpkg.json", EntryFormat::Presence, true, false),
];

/// The files one ecosystem answers dependency questions from, in the order
/// they are tried, plus the reason reported when none of them answers.
struct EcosystemSpec {
    sources: &'static [SourceSpec],
    unresolved: &'static str,
}

static PYTHON: EcosystemSpec = EcosystemSpec {
    sources: &PYTHON_SOURCES,
    unresolved: "no Python lockfile or manifest found",
};
static NODE: EcosystemSpec = EcosystemSpec {
    sources: &NODE_SOURCES,
    unresolved: "no Node.js lockfile or package.json found",
};
static RUST: EcosystemSpec = EcosystemSpec {
    sources: &RUST_SOURCES,
    unresolved: "no Cargo.lock or Cargo.toml found",
};
static GO: EcosystemSpec = EcosystemSpec {
    sources: &GO_SOURCES,
    unresolved: "no go.sum or go.mod found",
};
static RUBY: EcosystemSpec = EcosystemSpec {
    sources: &RUBY_SOURCES,
    unresolved: "no Gemfile.lock or Gemfile found",
};
static C_FAMILY: EcosystemSpec = EcosystemSpec {
    sources: &C_SOURCES,
    unresolved: "no C/C++ manifest convention found (conanfile.txt, vcpkg.json)",
};

fn ecosystem_spec(lang: LangId) -> &'static EcosystemSpec {
    match lang {
        LangId::Python => &PYTHON,
        LangId::JavaScript | LangId::TypeScript | LangId::Tsx => &NODE,
        LangId::Rust => &RUST,
        LangId::Go => &GO,
        LangId::Ruby => &RUBY,
        LangId::C | LangId::Cpp => &C_FAMILY,
    }
}

/// Classify a single external dependency from the ecosystem's source table.
///
/// Root sources are tried first, then the sources that may live in an
/// immediate subdirectory, so a monorepo layout answers without a project-wide
/// search. An ecosystem that declares no subdirectory source never reads the
/// directory listing.
pub fn classify_external(external: &ExternalNode, project_root: &Path) -> ExternalClassification {
    let spec = ecosystem_spec(external.language);

    for source in spec.sources.iter().filter(|source| source.at_root) {
        if let Some(classification) =
            classify_source(external, &project_root.join(source.file), source)
        {
            return classification;
        }
    }

    if spec.sources.iter().any(|source| source.in_subdirectory)
        && let Ok(entries) = std::fs::read_dir(project_root)
    {
        // Sorted, so two subdirectories offering the same file name do not
        // depend on the order the file system hands them out.
        let mut directories: Vec<std::path::PathBuf> = entries
            .flatten()
            .map(|entry| entry.path())
            .filter(|path| path.is_dir())
            .collect();
        directories.sort();
        for directory in directories {
            for source in spec.sources.iter().filter(|source| source.in_subdirectory) {
                if let Some(classification) =
                    classify_source(external, &directory.join(source.file), source)
                {
                    return classification;
                }
            }
        }
    }

    ExternalClassification::Unresolved {
        raw_path: external.raw_path.clone(),
        reason: spec.unresolved.to_string(),
    }
}

/// Classify one candidate file, or report that the file does not answer.
fn classify_source(
    external: &ExternalNode,
    path: &Path,
    source: &SourceSpec,
) -> Option<ExternalClassification> {
    if !path.exists() {
        return None;
    }
    let version = read_version(source.format, path, &external.raw_path);
    if version.is_none() && source.needs_entry {
        return None;
    }
    Some(ExternalClassification::Classified {
        package_name: external.raw_path.clone(),
        version,
        language: external.language,
        source: source.kind,
    })
}

/// Read the version a file states for a package, in the shape it writes.
fn read_version(format: EntryFormat, path: &Path, package: &str) -> Option<String> {
    match format {
        EntryFormat::EntryValue => entry_value_version(path, package),
        EntryFormat::TomlPackageArray => toml_package_version(path, package),
        EntryFormat::JsonDependencyMap => json_dependency_version(path, package),
        EntryFormat::ParenthesizedName => parenthesized_version(path, package),
        EntryFormat::ExactToken => exact_token_version(path, package),
        EntryFormat::GoRequire => go_require_version(path, package),
        EntryFormat::Presence => None,
    }
}

// ── Lockfile parsing helpers ───────────────────────────────────────

/// Best-effort version extraction from a lockfile by searching for the
/// package name followed by a version-like string. Returns None if the
/// package isn't found or the file can't be read.
/// True when the line names exactly this entry, in any lockfile grammar:
/// `name = "x"`, `x==1.2.3`, `"x": {`, `x (1.2.3)`, `"x@^1.2.3":`.
fn names_entry(line: &str, package: &str) -> bool {
    let trimmed = line.trim().trim_start_matches('-').trim();
    for token in trimmed.split(|c: char| {
        matches!(
            c,
            '=' | ' ' | '"' | '(' | ')' | '[' | ']' | ':' | ',' | '\'' | '{'
        )
    }) {
        let token = token.trim();
        if token == package {
            return true;
        }
        if let Some(rest) = token.strip_prefix(package)
            && matches!(
                rest.chars().next(),
                Some('@') | Some('^') | Some('~') | Some('>')
            )
        {
            return true;
        }
    }
    false
}

/// A `version = "x.y.z"` style assignment, whatever the surrounding syntax.
fn version_assignment(line: &str) -> Option<String> {
    let trimmed = line.trim().trim_start_matches('-').trim();
    let rest = trimmed.strip_prefix("version")?.trim_start();
    if !(rest.starts_with('=') || rest.starts_with(':') || rest.starts_with('"')) {
        return None;
    }
    extract_semver(rest)
}

fn entry_value_version(path: &Path, package: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    let mut inside_entry = false;

    for line in content.lines() {
        let trimmed = line.trim();

        // A new section or entry header closes the open entry.
        if trimmed.starts_with('[') {
            inside_entry = false;
        }

        if names_entry(trimmed, package) {
            if let Some(version) = version_assignment(trimmed) {
                return Some(version);
            }
            if (trimmed.contains("==") || trimmed.contains(">=") || trimmed.contains("~="))
                && let Some(version) = extract_semver(trimmed)
            {
                return Some(version);
            }
            inside_entry = true;
            continue;
        }

        if !inside_entry {
            continue;
        }
        if let Some(version) = version_assignment(trimmed) {
            return Some(version);
        }
        // A blank line ends a TOML entry that carried no version.
        if trimmed.is_empty() {
            inside_entry = false;
        }
    }
    None
}

fn json_dependency_version(path: &Path, package: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    let json: serde_json::Value = serde_json::from_str(&content).ok()?;
    // Check dependencies/devDependencies for the package.
    for section in ["dependencies", "devDependencies", "peerDependencies"] {
        if let Some(version) = json.get(section).and_then(|d| d.get(package))
            && let Some(s) = version.as_str()
        {
            return Some(s.to_string());
        }
    }
    None
}

fn toml_package_version(path: &Path, package: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    // Cargo.lock uses TOML; search for [[package]] sections with name = "..."
    let mut in_package_section = false;
    for line in content.lines() {
        if line.trim_start().starts_with("[[package]]") {
            in_package_section = false;
        }
        if let Some(rest) = line.strip_prefix("name = ") {
            let name = rest.trim().trim_matches('"');
            if name == package {
                in_package_section = true;
            }
        }
        if in_package_section && let Some(rest) = line.strip_prefix("version = ") {
            return Some(rest.trim().trim_matches('"').to_string());
        }
    }
    None
}

fn exact_token_version(path: &Path, package: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    // go.sum format: <module> <version> <hash>, one line per module version.
    for line in content.lines() {
        let mut parts = line.split_whitespace();
        if parts.next() != Some(package) {
            continue;
        }
        if let Some(version) = parts.next() {
            return Some(version.to_string());
        }
    }
    None
}

/// Read a `require` line, in single or block form, and drop the `v` prefix.
fn go_require_version(path: &Path, module: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    let mut inside_block = false;

    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("require (") {
            inside_block = true;
            continue;
        }
        if inside_block && trimmed.starts_with(')') {
            inside_block = false;
            continue;
        }
        let entry = if let Some(rest) = trimmed.strip_prefix("require ") {
            rest
        } else if inside_block {
            trimmed
        } else {
            continue;
        };
        let mut parts = entry.split_whitespace();
        if parts.next() != Some(module) {
            continue;
        }
        if let Some(version) = parts.next() {
            return Some(version.trim_start_matches('v').to_string());
        }
    }
    None
}

fn parenthesized_version(path: &Path, name: &str) -> Option<String> {
    let content = std::fs::read_to_string(path).ok()?;
    // Gemfile.lock lists each gem as "  <name> (<version>)" under a specs
    // section. The name has no quotes; match the indented line exactly.
    for line in content.lines() {
        let line = line.trim_start();
        if let Some(rest) = line
            .strip_prefix(&format!("{name} ("))
            .and_then(|rest| rest.strip_suffix(')'))
        {
            return Some(rest.trim().to_string());
        }
    }
    None
}

/// Extract the first semver-like substring from a line.
fn extract_semver(line: &str) -> Option<String> {
    let mut chars = line.chars().peekable();
    let mut start = None;
    let mut i = 0usize;
    while let Some(&c) = chars.peek() {
        if c.is_ascii_digit() {
            // Potential start of a version.
            let mut version = String::new();
            let mut dot_count = 0;
            while let Some(&c) = chars.peek() {
                if c.is_ascii_digit() {
                    version.push(c);
                    chars.next();
                } else if c == '.' {
                    version.push(c);
                    dot_count += 1;
                    chars.next();
                } else {
                    break;
                }
            }
            if dot_count >= 2 && !version.is_empty() {
                return Some(version);
            }
            start = Some(i);
        } else {
            chars.next();
        }
        i += 1;
    }
    let _ = start;
    None
}

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

    fn external_node(raw_path: &str) -> ExternalNode {
        ExternalNode {
            raw_path: raw_path.to_string(),
            language: LangId::Ruby,
            classification: None,
        }
    }

    fn python_node(raw_path: &str) -> ExternalNode {
        ExternalNode {
            raw_path: raw_path.to_string(),
            language: LangId::Python,
            classification: None,
        }
    }

    fn go_node(raw_path: &str) -> ExternalNode {
        ExternalNode {
            raw_path: raw_path.to_string(),
            language: LangId::Go,
            classification: None,
        }
    }

    fn test_dir(name: &str) -> std::path::PathBuf {
        let dir = std::env::temp_dir().join(format!("meta_ast_ruby_dep_{name}"));
        if dir.exists() {
            let _ = std::fs::remove_dir_all(&dir);
        }
        std::fs::create_dir_all(&dir).unwrap();
        dir
    }

    #[test]
    fn parenthesized_version_extracts_version() {
        let dir = test_dir("parse");
        let lf = dir.join("Gemfile.lock");
        std::fs::write(
            &lf,
            "GEM\n  remote: https://rubygems.org/\n  specs:\n    rails (7.0.8.4)\n      actioncable (= 7.0.8.4)\n",
        )
        .unwrap();

        assert_eq!(
            parenthesized_version(&lf, "rails"),
            Some("7.0.8.4".to_string())
        );
        assert_eq!(parenthesized_version(&lf, "missing"), None);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn classify_ruby_uses_gemfile_lock() {
        let dir = test_dir("classify_lock");
        std::fs::write(
            dir.join("Gemfile.lock"),
            "GEM\n  specs:\n    rails (7.0.8.4)\n",
        )
        .unwrap();

        let classification = classify_external(&external_node("rails"), &dir);
        match classification {
            ExternalClassification::Classified {
                package_name,
                version,
                language,
                source,
            } => {
                assert_eq!(package_name, "rails");
                assert_eq!(version.as_deref(), Some("7.0.8.4"));
                assert_eq!(language, LangId::Ruby);
                assert_eq!(source, DependencySource::Lockfile);
            }
            other => panic!("expected Classified, got {other:?}"),
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn classify_ruby_unresolved_without_gemfile() {
        let missing = std::env::temp_dir().join("meta_ast_ruby_dep_missing_dir");
        let classification = classify_external(&external_node("rails"), &missing);
        match classification {
            ExternalClassification::Unresolved { raw_path, reason } => {
                assert_eq!(raw_path, "rails");
                assert_eq!(reason, "no Gemfile.lock or Gemfile found");
            }
            other => panic!("expected Unresolved, got {other:?}"),
        }
    }

    /// A lockfile entry name must match the package exactly, not as a substring.
    #[test]
    fn lockfile_match_requires_the_exact_entry() {
        let dir = test_dir("lockfile_boundary");
        let lf = dir.join("uv.lock");
        std::fs::write(
            &lf,
            "[[package]]\nname = \"requests-toolbelt\"\nversion = \"4.0.0\"\n",
        )
        .unwrap();

        assert_eq!(
            entry_value_version(&lf, "requests"),
            None,
            "requests must not match the requests-toolbelt entry"
        );
        assert_eq!(
            entry_value_version(&lf, "requests-toolbelt").as_deref(),
            Some("4.0.0")
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// A lockfile that does not list the package must not be reported as the
    /// source of a resolved dependency.
    #[test]
    fn lockfile_without_the_entry_is_not_a_lockfile_hit() {
        let dir = test_dir("lockfile_missing_entry");
        std::fs::write(
            dir.join("uv.lock"),
            "[[package]]\nname = \"requests-toolbelt\"\nversion = \"4.0.0\"\n",
        )
        .unwrap();

        let classification = classify_external(&python_node("requests"), &dir);
        assert!(
            !matches!(
                classification,
                ExternalClassification::Classified {
                    source: DependencySource::Lockfile,
                    ..
                }
            ),
            "an entry the lockfile does not contain is not a lockfile hit: {classification:?}"
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// A go.sum module path must match exactly, not as a prefix.
    #[test]
    fn go_sum_requires_an_exact_module_path() {
        let dir = test_dir("go_sum_boundary");
        let lf = dir.join("go.sum");
        std::fs::write(&lf, "github.com/foo/bar/baz v1.0.0 h1:AAAA=\n").unwrap();

        assert_eq!(
            exact_token_version(&lf, "github.com/foo/bar"),
            None,
            "a prefix must not match a different module"
        );
        assert_eq!(
            exact_token_version(&lf, "github.com/foo/bar/baz").as_deref(),
            Some("v1.0.0")
        );
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// go.mod `require` lines carry the version.
    #[test]
    fn go_mod_require_line_yields_a_version() {
        let dir = test_dir("go_mod_require");
        std::fs::write(
            dir.join("go.mod"),
            "module example.com/app\n\ngo 1.21\n\nrequire github.com/foo/bar v1.2.3\n",
        )
        .unwrap();

        let classification = classify_external(&go_node("github.com/foo/bar"), &dir);
        match classification {
            ExternalClassification::Classified {
                version,
                language,
                source,
                ..
            } => {
                assert_eq!(language, LangId::Go);
                assert_eq!(source, DependencySource::Manifest);
                let version = version.unwrap_or_default();
                assert!(
                    version.contains("1.2.3"),
                    "the go.mod require version must be read, got {version:?}"
                );
            }
            other => panic!("expected Classified, got {other:?}"),
        }
        let _ = std::fs::remove_dir_all(&dir);
    }

    /// Every source form the resolver table lists must keep resolving the same
    /// way: which file wins, whether a version is found, and the source label.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    enum Expectation {
        Lockfile(Option<&'static str>),
        Manifest(Option<&'static str>),
        Unresolved,
    }

    struct SourceCase {
        name: &'static str,
        language: LangId,
        package: &'static str,
        file: &'static str,
        content: &'static str,
        nested: bool,
        expectation: Expectation,
    }

    #[test]
    fn ecosystem_sources_resolve_through_one_table() {
        use LangId::{C, Cpp, Go, JavaScript, Python, Ruby, Rust as RustLang};

        let cases = [
            SourceCase {
                name: "uv lock",
                language: Python,
                package: "requests",
                file: "uv.lock",
                content: "[[package]]\nname = \"requests\"\nversion = \"2.32.3\"\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("2.32.3")),
            },
            SourceCase {
                name: "poetry lock",
                language: Python,
                package: "requests",
                file: "poetry.lock",
                content: "[[package]]\nname = \"requests\"\nversion = \"2.32.3\"\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("2.32.3")),
            },
            SourceCase {
                name: "requirements manifest",
                language: Python,
                package: "requests",
                file: "requirements.txt",
                content: "requests==2.32.3\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "pyproject manifest",
                language: Python,
                package: "requests",
                file: "pyproject.toml",
                content: "[project]\nname = \"app\"\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "pyproject in a subdirectory",
                language: Python,
                package: "requests",
                file: "pyproject.toml",
                content: "[project]\nname = \"app\"\n",
                nested: true,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "no python source",
                language: Python,
                package: "requests",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
            SourceCase {
                name: "yarn lock",
                language: JavaScript,
                package: "express",
                file: "yarn.lock",
                content: "\"express@^4.18.2\":\n  version \"4.18.2\"\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("4.18.2")),
            },
            SourceCase {
                name: "pnpm lock",
                language: JavaScript,
                package: "express",
                file: "pnpm-lock.yaml",
                content: "  express@4.18.2:\n    version: 4.18.2\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("4.18.2")),
            },
            SourceCase {
                name: "package json",
                language: JavaScript,
                package: "express",
                file: "package.json",
                content: "{\"dependencies\": {\"express\": \"4.18.2\"}}\n",
                nested: false,
                expectation: Expectation::Manifest(Some("4.18.2")),
            },
            SourceCase {
                name: "package json in a subdirectory",
                language: JavaScript,
                package: "express",
                file: "package.json",
                content: "{\"dependencies\": {\"express\": \"4.18.2\"}}\n",
                nested: true,
                expectation: Expectation::Manifest(Some("4.18.2")),
            },
            SourceCase {
                name: "no node source",
                language: JavaScript,
                package: "express",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
            SourceCase {
                name: "cargo lock",
                language: RustLang,
                package: "serde",
                file: "Cargo.lock",
                content: "[[package]]\nname = \"serde\"\nversion = \"1.0.203\"\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("1.0.203")),
            },
            SourceCase {
                name: "cargo manifest",
                language: RustLang,
                package: "serde",
                file: "Cargo.toml",
                content: "[dependencies]\nserde = \"1\"\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "no rust source",
                language: RustLang,
                package: "serde",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
            SourceCase {
                name: "go sum",
                language: Go,
                package: "github.com/pkg/errors",
                file: "go.sum",
                content: "github.com/pkg/errors v0.9.1 h1:abc\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("v0.9.1")),
            },
            SourceCase {
                name: "go mod",
                language: Go,
                package: "github.com/pkg/errors",
                file: "go.mod",
                content: "module app\n\nrequire (\n\tgithub.com/pkg/errors v0.9.1\n)\n",
                nested: false,
                expectation: Expectation::Manifest(Some("0.9.1")),
            },
            SourceCase {
                name: "no go source",
                language: Go,
                package: "github.com/pkg/errors",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
            SourceCase {
                name: "gemfile lock",
                language: Ruby,
                package: "rails",
                file: "Gemfile.lock",
                content: "GEM\n  specs:\n    rails (7.0.8.4)\n",
                nested: false,
                expectation: Expectation::Lockfile(Some("7.0.8.4")),
            },
            SourceCase {
                name: "gemfile",
                language: Ruby,
                package: "rails",
                file: "Gemfile",
                content: "source \"https://rubygems.org\"\ngem \"rails\"\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "no ruby source",
                language: Ruby,
                package: "rails",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
            SourceCase {
                name: "conan manifest",
                language: C,
                package: "zlib/1.3.1",
                file: "conanfile.txt",
                content: "[requires]\nzlib/1.3.1\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "vcpkg manifest",
                language: Cpp,
                package: "zlib",
                file: "vcpkg.json",
                content: "{\"dependencies\": [\"zlib\"]}\n",
                nested: false,
                expectation: Expectation::Manifest(None),
            },
            SourceCase {
                name: "no c source",
                language: C,
                package: "sqlite3",
                file: "",
                content: "",
                nested: false,
                expectation: Expectation::Unresolved,
            },
        ];

        for case in cases {
            let dir = test_dir(case.name);
            let target = if case.nested {
                let nested = dir.join("pkg");
                std::fs::create_dir_all(&nested).unwrap();
                nested
            } else {
                dir.clone()
            };
            if !case.file.is_empty() {
                std::fs::write(target.join(case.file), case.content).unwrap();
            }

            let external = ExternalNode {
                raw_path: case.package.to_string(),
                language: case.language,
                classification: None,
            };
            let result = classify_external(&external, &dir);
            let (source, version) = match &result {
                ExternalClassification::Classified {
                    source, version, ..
                } => (Some(*source), version.clone()),
                ExternalClassification::Unresolved { .. } => (None, None),
            };

            match case.expectation {
                Expectation::Lockfile(expected) => {
                    assert_eq!(
                        source,
                        Some(DependencySource::Lockfile),
                        "{}: expected a lockfile hit, got {result:?}",
                        case.name
                    );
                    assert_eq!(version.as_deref(), expected, "{}: version", case.name);
                }
                Expectation::Manifest(expected) => {
                    assert_eq!(
                        source,
                        Some(DependencySource::Manifest),
                        "{}: expected a manifest hit, got {result:?}",
                        case.name
                    );
                    assert_eq!(version.as_deref(), expected, "{}: version", case.name);
                }
                Expectation::Unresolved => assert_eq!(
                    source, None,
                    "{}: expected no classification, got {result:?}",
                    case.name
                ),
            }

            let _ = std::fs::remove_dir_all(&dir);
        }
    }

    /// The ecosystems are not symmetric, and the descriptor table has to keep
    /// every difference: a lockfile without the entry still names the lockfile
    /// for Rust and Ruby, Python, Node and Go fall through to the manifest, and
    /// only Python and Node look inside an immediate subdirectory.
    #[test]
    fn ecosystem_lockfile_asymmetries_are_preserved() {
        let node = |language: LangId, raw: &str| ExternalNode {
            raw_path: raw.to_string(),
            language,
            classification: None,
        };
        let hit = |result: &ExternalClassification| match result {
            ExternalClassification::Classified {
                source, version, ..
            } => (Some(*source), version.clone()),
            ExternalClassification::Unresolved { .. } => (None, None),
        };
        let scratch = [
            "asymmetry_rust",
            "asymmetry_ruby",
            "asymmetry_python",
            "asymmetry_go",
            "asymmetry_node_subdir",
            "asymmetry_node_nested_yarn",
        ];
        for name in scratch {
            let _ = std::fs::remove_dir_all(test_dir(name));
        }

        // A Cargo.lock that does not carry the crate still names the lockfile.
        let dir = test_dir("asymmetry_rust");
        std::fs::write(
            dir.join("Cargo.lock"),
            "[[package]]\nname = \"other\"\nversion = \"1.0.0\"\n",
        )
        .unwrap();
        assert_eq!(
            hit(&classify_external(&node(LangId::Rust, "serde"), &dir)),
            (Some(DependencySource::Lockfile), None),
            "a Cargo.lock without the crate stays the source for Rust"
        );

        // The same holds for Gemfile.lock.
        let dir = test_dir("asymmetry_ruby");
        std::fs::write(
            dir.join("Gemfile.lock"),
            "GEM\n  specs:\n    other (1.0.0)\n",
        )
        .unwrap();
        assert_eq!(
            hit(&classify_external(&node(LangId::Ruby, "rails"), &dir)),
            (Some(DependencySource::Lockfile), None),
            "a Gemfile.lock without the gem stays the source for Ruby"
        );

        // Python falls through a lockfile that does not carry the entry.
        let dir = test_dir("asymmetry_python");
        std::fs::write(
            dir.join("uv.lock"),
            "[[package]]\nname = \"other\"\nversion = \"1.0.0\"\n",
        )
        .unwrap();
        std::fs::write(dir.join("pyproject.toml"), "[project]\nname = \"app\"\n").unwrap();
        assert_eq!(
            hit(&classify_external(&node(LangId::Python, "requests"), &dir)),
            (Some(DependencySource::Manifest), None),
            "Python falls through a lockfile without the entry"
        );

        // Go falls through go.sum to go.mod, which carries the version.
        let dir = test_dir("asymmetry_go");
        std::fs::write(dir.join("go.sum"), "github.com/other/mod v1.0.0 h1:AAAA=\n").unwrap();
        std::fs::write(
            dir.join("go.mod"),
            "module example.com/app\n\nrequire github.com/foo/bar v1.2.3\n",
        )
        .unwrap();
        assert_eq!(
            hit(&classify_external(
                &node(LangId::Go, "github.com/foo/bar"),
                &dir
            )),
            (Some(DependencySource::Manifest), Some("1.2.3".to_string())),
            "Go falls through go.sum to go.mod"
        );

        // Node answers from an immediate subdirectory, but only for
        // package.json and package-lock.json.
        let dir = test_dir("asymmetry_node_subdir");
        let nested = dir.join("pkg");
        std::fs::create_dir_all(&nested).unwrap();
        std::fs::write(
            nested.join("package.json"),
            "{\"dependencies\":{\"react\":\"18.3.1\"}}",
        )
        .unwrap();
        assert_eq!(
            hit(&classify_external(&node(LangId::JavaScript, "react"), &dir)),
            (Some(DependencySource::Manifest), Some("18.3.1".to_string())),
            "a nested package.json answers for Node"
        );

        let dir = test_dir("asymmetry_node_nested_yarn");
        let nested = dir.join("pkg");
        std::fs::create_dir_all(&nested).unwrap();
        std::fs::write(
            nested.join("yarn.lock"),
            "react@^18:\n  version \"18.3.1\"\n",
        )
        .unwrap();
        assert_eq!(
            hit(&classify_external(&node(LangId::JavaScript, "react"), &dir)),
            (None, None),
            "a nested yarn.lock is not consulted"
        );

        for name in scratch {
            let _ = std::fs::remove_dir_all(test_dir(name));
        }
    }
}