relay-knowledge 1.1.16

Graph-database-based knowledge graph project.
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
use crate::domain::{CodeIndexSnapshot, CodeParseStatus, CodeRepositoryRegistration};

use super::*;

#[test]
fn configuration_review_regressions_skip_structural_noise() {
    let starlark = parse_source_snapshot(
        "BUILD.bazel",
        br#"load(
    # )
    "//tools:defs.bzl",
    "rule",
)
cc_binary(name = "app", deps = ["//libs"])
"#,
    );
    assert!(
        starlark
            .imports
            .iter()
            .any(|import| import.module == "//tools:defs.bzl"),
        "Starlark load comments must not close multiline calls: {:?}",
        starlark.imports
    );
    assert!(
        starlark
            .references
            .iter()
            .any(|reference| reference.name == "//libs:libs"),
        "Bazel package-only labels should normalize to default target labels: {:?}",
        starlark.references
    );
    assert!(
        !starlark
            .references
            .iter()
            .any(|reference| reference.name == "//libs"),
        "Bazel package-only labels should not keep the unresolvable package literal: {:?}",
        starlark.references
    );
    let starlark_string_parens = parse_source_snapshot(
        "BUILD.bazel",
        br#"load(
    "//tools:defs.bzl",
    "macro(",
)
load("//more:defs.bzl", "other")
"#,
    );
    assert!(
        starlark_string_parens
            .imports
            .iter()
            .any(|import| import.module == "//tools:defs.bzl")
            && starlark_string_parens
                .imports
                .iter()
                .any(|import| import.module == "//more:defs.bzl"),
        "Starlark string parentheses must not swallow later calls: {:?}",
        starlark_string_parens.imports
    );
    let starlark_scope = parse_sources_snapshot(&[
        (
            "app/BUILD",
            br#"cc_binary(name = "app", deps = [":core"])"# as &[u8],
        ),
        ("libs/BUILD", br#"cc_library(name = "core")"# as &[u8]),
    ]);
    assert!(
        starlark_scope.references.iter().any(|reference| {
            reference.path == "app/BUILD"
                && reference.name == "//app:core"
                && reference.target_symbol_snapshot_id.is_none()
        }),
        "local Bazel labels should stay scoped to their BUILD package: {:?}",
        starlark_scope.references
    );

    let go_mod = parse_source_snapshot(
        "go.mod",
        b"module example.com/app\nrequire (\n\texample.com/lib v1.2.3\n) // end require\nreplace example.com/old => example.com/new\n",
    );
    assert!(
        !go_mod
            .references
            .iter()
            .any(|reference| reference.name == "replace"),
        "go.mod trailing block comments must close require blocks before replace lines: {:?}",
        go_mod.references
    );
    assert!(
        go_mod
            .references
            .iter()
            .any(|reference| reference.name == "example.com/new"),
        "go.mod replace lines after commented block closes should still be indexed: {:?}",
        go_mod.references
    );

    let yaml = parse_source_snapshot(
        "config.yaml",
        b"script: |\n  echo: hello\nmirrors:\n  - http://example.com\nserver:\n  port: 8080\n",
    );
    assert!(
        !yaml.symbols.iter().any(|symbol| symbol.name == "echo"),
        "YAML block scalar content must not become config keys: {:?}",
        yaml.symbols
    );
    assert!(
        !yaml.symbols.iter().any(|symbol| symbol.name == "http"),
        "YAML scalar list URLs must not become config keys: {:?}",
        yaml.symbols
    );
    assert!(
        yaml.chunks.iter().any(|chunk| {
            chunk.symbol_snapshot_id.is_none() && chunk.content.contains("http://example.com")
        }),
        "structured config files with symbols should keep file chunks for scalar values: {:?}",
        yaml.chunks
    );
    assert!(
        yaml.symbols.iter().any(|symbol| symbol.name == "port"),
        "YAML mappings after a block scalar should still be indexed: {:?}",
        yaml.symbols
    );
    let yaml_sequence_scalar =
        parse_source_snapshot("config.yaml", b"scripts:\n  - |\n    echo: hi\nname: app\n");
    assert!(
        !yaml_sequence_scalar
            .symbols
            .iter()
            .any(|symbol| symbol.name == "echo"),
        "YAML sequence block scalar content must not become config keys: {:?}",
        yaml_sequence_scalar.symbols
    );
    assert!(
        yaml_sequence_scalar
            .symbols
            .iter()
            .any(|symbol| symbol.name == "name"),
        "YAML mappings after sequence block scalars should still be indexed: {:?}",
        yaml_sequence_scalar.symbols
    );
    let yaml_quoted_scalar =
        parse_source_snapshot("config.yaml", b"commands:\n  - \"echo: hi\"\nname: app\n");
    assert!(
        !yaml_quoted_scalar
            .symbols
            .iter()
            .any(|symbol| symbol.name == "echo"),
        "YAML quoted scalar colons must not become config keys: {:?}",
        yaml_quoted_scalar.symbols
    );
    assert!(
        yaml_quoted_scalar
            .symbols
            .iter()
            .any(|symbol| symbol.name == "name"),
        "YAML mappings after quoted scalar lists should still be indexed: {:?}",
        yaml_quoted_scalar.symbols
    );

    let cmake = parse_source_snapshot(
        "CMakeLists.txt",
        b"add_executable(${PROJECT_NAME} main.cc)\nadd_library(real src.cc)\nadd_custom_target(generated_headers)\nadd_dependencies(real generated_headers)\ntarget_link_libraries(real LINK_INTERFACE_LIBRARIES core)\n",
    );
    assert!(
        !cmake
            .symbols
            .iter()
            .any(|symbol| symbol.name.contains("PROJECT_NAME")),
        "CMake variable-expanded target names must not become symbols: {:?}",
        cmake.symbols
    );
    assert!(
        cmake.symbols.iter().any(|symbol| symbol.name == "real"),
        "literal CMake targets should still be indexed: {:?}",
        cmake.symbols
    );
    assert!(
        cmake
            .references
            .iter()
            .any(|reference| reference.name == "generated_headers" && reference.kind == "target"),
        "CMake add_dependencies target arguments should be indexed as target references: {:?}",
        cmake.references
    );
    assert!(
        !cmake
            .references
            .iter()
            .any(|reference| reference.name == "LINK_INTERFACE_LIBRARIES"),
        "legacy CMake link-interface keywords must not become target references: {:?}",
        cmake.references
    );
    let cmake_include = parse_sources_snapshot(&[
        (
            "cmake/Config.cmake",
            br#"include("${CMAKE_CURRENT_LIST_DIR}/Targets.cmake")"# as &[u8],
        ),
        (
            "cmake/Targets.cmake",
            b"add_library(pkg INTERFACE)\n" as &[u8],
        ),
        (
            "cmake/cmake/Targets.cmake",
            b"add_library(wrong INTERFACE)\n" as &[u8],
        ),
    ]);
    let cmake_include_import = cmake_include
        .imports
        .iter()
        .find(|import| import.module == "cmake/Targets.cmake")
        .expect("CMAKE_CURRENT_LIST_DIR include should be indexed");
    assert_eq!(
        cmake_include_import.resolution_state, "resolved",
        "CMAKE_CURRENT_LIST_DIR includes should resolve to sibling files: {:?}",
        cmake_include.imports
    );
    assert_eq!(
        cmake_include_import.target_hint.as_deref(),
        Some("cmake/Targets.cmake"),
        "already-resolved CMAKE_CURRENT_LIST_DIR includes must not prepend the parent twice: {:?}",
        cmake_include.imports
    );
    let cmake_nested_include = parse_sources_snapshot(&[
        (
            "src/CMakeLists.txt",
            b"include(cmake/Flags.cmake)\n" as &[u8],
        ),
        ("cmake/Flags.cmake", b"set(FLAGS enabled)\n" as &[u8]),
    ]);
    let nested_cmake_import = cmake_nested_include
        .imports
        .iter()
        .find(|import| import.path == "src/CMakeLists.txt" && import.module == "cmake/Flags.cmake")
        .expect("nested CMake include should be indexed");
    assert_eq!(
        nested_cmake_import.resolution_state, "unresolved",
        "relative CMake includes must not fall back to the repository root: {:?}",
        cmake_nested_include.imports
    );
    let cmake_quoted_parens = parse_source_snapshot(
        "CMakeLists.txt",
        b"set(PATTERN \"(\")\nset(BRACKET [=[(]=])\nadd_library(after src.cc)\n",
    );
    assert!(
        cmake_quoted_parens
            .symbols
            .iter()
            .any(|symbol| symbol.name == "after" && symbol.kind == "target"),
        "CMake quoted/bracket argument parentheses must not swallow later calls: {:?}",
        cmake_quoted_parens.symbols
    );
    let cmake_variable_scope = parse_sources_snapshot(&[
        ("app/CMakeLists.txt", b"set(SRC_DIR src)\n" as &[u8]),
        (
            "tools/CMakeLists.txt",
            b"set(OUT ${SRC_DIR}/tool)\n" as &[u8],
        ),
    ]);
    let cross_file_cmake_var = cmake_variable_scope
        .references
        .iter()
        .find(|reference| {
            reference.path == "tools/CMakeLists.txt"
                && reference.name == "SRC_DIR"
                && reference.kind == "variable"
        })
        .expect("tools CMake file should record SRC_DIR reference");
    assert_eq!(
        cross_file_cmake_var.target_symbol_snapshot_id, None,
        "CMake variable references should not resolve to unrelated CMake files: {:?}",
        cmake_variable_scope.references
    );
    assert_eq!(cross_file_cmake_var.resolution_state, "unresolved");
    let cmake_subdir_scope = parse_sources_snapshot(&[
        ("src/CMakeLists.txt", b"add_subdirectory(lib)\n" as &[u8]),
        (
            "lib/CMakeLists.txt",
            b"add_library(root_level src.cc)\n" as &[u8],
        ),
    ]);
    let src_subdir_import = cmake_subdir_scope
        .imports
        .iter()
        .find(|import| {
            import.path == "src/CMakeLists.txt" && import.module == "src/lib/CMakeLists.txt"
        })
        .expect("relative CMake subdirectory should be indexed under caller directory");
    assert_eq!(
        src_subdir_import.resolution_state, "unresolved",
        "CMake add_subdirectory should not fall back to a root-level sibling: {:?}",
        cmake_subdir_scope.imports
    );
    let cmake_alias = parse_source_snapshot(
        "CMakeLists.txt",
        b"add_library(real src.cc)\nadd_library(alias ALIAS real)\nadd_subdirectory(${THIRD_PARTY_DIR}/foo)\n",
    );
    assert!(
        cmake_alias
            .references
            .iter()
            .any(|reference| reference.name == "real" && reference.kind == "target"),
        "CMake ALIAS operands should be indexed as target references: {:?}",
        cmake_alias.references
    );
    assert!(
        !cmake_alias
            .imports
            .iter()
            .any(|import| import.module.contains("THIRD_PARTY_DIR")),
        "CMake variable-expanded subdirectories should not become file imports: {:?}",
        cmake_alias.imports
    );
    let cmake_absolute_subdir = parse_source_snapshot(
        "src/CMakeLists.txt",
        b"add_subdirectory(/opt/vendor/lib build)\nadd_library(app src.cc)\n",
    );
    assert!(
        !cmake_absolute_subdir
            .imports
            .iter()
            .any(|import| import.module.contains("/opt/vendor/lib")),
        "CMake absolute add_subdirectory paths should not become repo-relative imports: {:?}",
        cmake_absolute_subdir.imports
    );
    assert!(
        cmake_absolute_subdir
            .symbols
            .iter()
            .any(|symbol| symbol.name == "app"),
        "CMake calls after absolute subdirectories should still be indexed: {:?}",
        cmake_absolute_subdir.symbols
    );
    let cmake_bracket_comment = parse_source_snapshot(
        "CMakeLists.txt",
        b"#[[\nadd_library(disabled disabled.cc)\ninclude(disabled.cmake)\n]]\nadd_library(enabled src.cc)\n",
    );
    assert!(
        !cmake_bracket_comment
            .symbols
            .iter()
            .any(|symbol| symbol.name == "disabled")
            && !cmake_bracket_comment
                .imports
                .iter()
                .any(|import| import.module == "disabled.cmake"),
        "CMake bracket comments must not emit disabled calls: {:?} {:?}",
        cmake_bracket_comment.symbols,
        cmake_bracket_comment.imports
    );
    assert!(
        cmake_bracket_comment
            .symbols
            .iter()
            .any(|symbol| symbol.name == "enabled"),
        "CMake calls after bracket comments should still be indexed: {:?}",
        cmake_bracket_comment.symbols
    );

    let markdown = parse_source_snapshot(
        "README.md",
        b"# Runtime\n    # install\n```sh\n# install dependencies\n```\n## Usage\n",
    );
    assert!(
        !markdown
            .symbols
            .iter()
            .any(|symbol| symbol.name == "install dependencies"),
        "Markdown fenced-code comments must not become headings: {:?}",
        markdown.symbols
    );
    assert!(
        !markdown
            .symbols
            .iter()
            .any(|symbol| symbol.name == "install"),
        "Markdown indented-code comments must not become headings: {:?}",
        markdown.symbols
    );
    assert!(
        markdown.symbols.iter().any(|symbol| symbol.name == "Usage"),
        "Markdown headings after fenced code should still be indexed: {:?}",
        markdown.symbols
    );
    assert!(
        markdown
            .chunks
            .iter()
            .any(|chunk| chunk.symbol_snapshot_id.is_none() && chunk.content.contains("## Usage")),
        "Markdown files with heading symbols should keep a file chunk for body text: {:?}",
        markdown.chunks
    );
    let properties = parse_source_snapshot(
        "app.properties",
        b"message = hello \\\n  world\nnext = yes\n",
    );
    assert!(
        !properties
            .symbols
            .iter()
            .any(|symbol| symbol.name == "world"),
        "continued Java properties value lines must not become keys: {:?}",
        properties.symbols
    );
    assert!(
        properties
            .symbols
            .iter()
            .any(|symbol| symbol.name == "next"),
        "Java properties keys after continuations should still be indexed: {:?}",
        properties.symbols
    );

    let jinja = parse_source_snapshot(
        "templates/page.html.j2",
        br#"{% include ["primary.html", "fallback.html"] %}"#,
    );
    assert!(
        jinja
            .imports
            .iter()
            .any(|import| import.module == "primary.html"),
        "first Jinja include-list template should be indexed: {:?}",
        jinja.imports
    );
    assert!(
        jinja
            .imports
            .iter()
            .any(|import| import.module == "fallback.html"),
        "fallback Jinja include-list template should be indexed: {:?}",
        jinja.imports
    );
    let jinja_template_root = parse_sources_snapshot(&[
        (
            "templates/pages/index.html.j2",
            br#"{% extends "layouts/base.html.j2" %}"# as &[u8],
        ),
        (
            "templates/layouts/base.html.j2",
            b"{% block body %}{% endblock %}" as &[u8],
        ),
    ]);
    let template_root_import = jinja_template_root
        .imports
        .iter()
        .find(|import| {
            import.path == "templates/pages/index.html.j2"
                && import.module == "layouts/base.html.j2"
        })
        .expect("Jinja template-root import should be indexed");
    assert_eq!(
        template_root_import.resolution_state, "resolved",
        "Jinja nested templates should resolve imports from the nearest template root: {:?}",
        jinja_template_root.imports
    );
    assert_eq!(
        template_root_import.target_hint.as_deref(),
        Some("templates/layouts/base.html.j2"),
        "Jinja template-root imports should target the sibling root directory: {:?}",
        jinja_template_root.imports
    );

    let xml = parse_source_snapshot(
        "pom.xml",
        br#"<root data-href = "bad.xml" relocation = "move.xml"><description>use href="prose.xml" in docs</description><![CDATA[<old href="cdata.xml"></old>]]><real href = "ok.xml" location = "extra.xml"/></root>"#,
    );
    assert!(
        !xml.symbols.iter().any(|symbol| symbol.name == "old"),
        "XML CDATA elements must not become symbols: {:?}",
        xml.symbols
    );
    assert!(
        !xml.imports.iter().any(|import| matches!(
            import.module.as_str(),
            "bad.xml" | "move.xml" | "cdata.xml" | "prose.xml"
        )),
        "XML prose, CDATA, and non-import attributes must not become imports: {:?}",
        xml.imports
    );
    assert!(
        xml.imports.iter().any(|import| import.module == "ok.xml")
            && xml
                .imports
                .iter()
                .any(|import| import.module == "extra.xml"),
        "XML imports outside CDATA and exact import attributes should still be indexed: {:?}",
        xml.imports
    );
    let xml_multiline = parse_source_snapshot(
        "pom.xml",
        br#"<project
  xsi:schemaLocation="urn:example schema/project.xsd"
  href="project.xml">
</project>"#,
    );
    assert!(
        xml_multiline
            .imports
            .iter()
            .any(|import| import.module == "schema/project.xsd")
            && xml_multiline
                .imports
                .iter()
                .any(|import| import.module == "project.xml"),
        "XML multiline start-tag imports should be indexed: {:?}",
        xml_multiline.imports
    );
    assert!(
        xml_multiline
            .symbols
            .iter()
            .any(|symbol| symbol.name == "project"),
        "XML multiline start tags should still emit element symbols: {:?}",
        xml_multiline.symbols
    );

    let malformed_starlark = parse_source_snapshot(
        "BUILD.bazel",
        br#"cc_library(name = "app")
load(
"#,
    );
    assert_eq!(
        malformed_starlark.files[0].parse_status,
        CodeParseStatus::Partial,
        "malformed configuration files should stay degraded even with partial facts"
    );
    let malformed_cmake = parse_source_snapshot("CMakeLists.txt", b"add_library(app src.cc)\n)\n");
    assert_eq!(
        malformed_cmake.files[0].parse_status,
        CodeParseStatus::Partial,
        "unbalanced CMake files should stay degraded even with target facts"
    );
    let balanced_invalid_cmake =
        parse_source_snapshot("CMakeLists.txt", b"add_library(app src.cc)\nnot_a_call\n");
    assert_eq!(
        balanced_invalid_cmake.files[0].parse_status,
        CodeParseStatus::Partial,
        "balanced CMake files with invalid top-level syntax should stay degraded"
    );
    let malformed_ninja = parse_source_snapshot("build.ninja", b"build app.o cc app.c\n");
    assert_eq!(
        malformed_ninja.files[0].parse_status,
        CodeParseStatus::Partial,
        "malformed Ninja manifests should stay degraded even when manual facts exist"
    );
    let malformed_go_template =
        parse_source_snapshot("templates/deployment.yaml", br#"{{ include "app.labels" ."#);
    assert_eq!(
        malformed_go_template.files[0].parse_status,
        CodeParseStatus::Partial,
        "malformed Go-template actions should stay degraded even when manual facts exist"
    );
    let make_scope = parse_sources_snapshot(&[
        ("app/Makefile", b"app: build\n" as &[u8]),
        ("tools/Makefile", b"build:\n" as &[u8]),
    ]);
    let cross_file_make_ref = make_scope
        .references
        .iter()
        .find(|reference| {
            reference.path == "app/Makefile"
                && reference.name == "build"
                && reference.kind == "target"
        })
        .expect("app Makefile should record build prerequisite");
    assert_eq!(
        cross_file_make_ref.target_symbol_snapshot_id, None,
        "Make target prerequisites should not resolve to unrelated Makefiles: {:?}",
        make_scope.references
    );
    assert_eq!(cross_file_make_ref.resolution_state, "unresolved");

    let make_variable_scope = parse_sources_snapshot(&[
        ("app/Makefile", b"all: $(OBJECTS)\n" as &[u8]),
        ("tools/Makefile", b"OBJECTS = tool.o\n" as &[u8]),
    ]);
    let cross_file_make_var = make_variable_scope
        .references
        .iter()
        .find(|reference| {
            reference.path == "app/Makefile"
                && reference.name == "OBJECTS"
                && reference.kind == "variable"
        })
        .expect("app Makefile should record OBJECTS prerequisite variable");
    assert_eq!(
        cross_file_make_var.target_symbol_snapshot_id, None,
        "Make variable references should not resolve to unrelated Makefiles: {:?}",
        make_variable_scope.references
    );
    assert_eq!(cross_file_make_var.resolution_state, "unresolved");

    let make_patterns = parse_source_snapshot(
        "Makefile",
        b"OBJECTS = app.o # $(OLD_OBJECTS)\nall: ${OBJECTS}\nobjects: %.o: %.c\n",
    );
    let brace_make_var = make_patterns
        .references
        .iter()
        .find(|reference| reference.name == "OBJECTS" && reference.kind == "variable")
        .expect("brace-style Make variable should be indexed");
    assert_eq!(
        brace_make_var.resolution_state, "resolved",
        "brace-style Make variables should resolve inside the same Makefile: {:?}",
        make_patterns.references
    );
    assert!(
        !make_patterns
            .references
            .iter()
            .any(|reference| matches!(reference.name.as_str(), "%.o:" | "%.c")),
        "Make static pattern rules should not index target patterns as prerequisites: {:?}",
        make_patterns.references
    );
    assert!(
        !make_patterns
            .references
            .iter()
            .any(|reference| reference.name == "OLD_OBJECTS"),
        "Make assignment comments must not become variable references: {:?}",
        make_patterns.references
    );
    let make_modifiers = parse_source_snapshot(
        "Makefile",
        b"SOURCES = app.c\nexport OBJECTS = $(SOURCES:.c=.o)\noverride CFLAGS += -O2\n",
    );
    assert!(
        make_modifiers
            .symbols
            .iter()
            .any(|symbol| symbol.name == "OBJECTS")
            && make_modifiers
                .symbols
                .iter()
                .any(|symbol| symbol.name == "CFLAGS"),
        "Make assignment modifiers should not hide variable symbols: {:?}",
        make_modifiers.symbols
    );
    let make_modifier_ref = make_modifiers
        .references
        .iter()
        .find(|reference| reference.name == "SOURCES" && reference.kind == "variable")
        .expect("Make substitution assignment should reference SOURCES");
    assert_eq!(
        make_modifier_ref.resolution_state, "resolved",
        "Make variable references inside modifier assignments should resolve: {:?}",
        make_modifiers.references
    );

    let make = parse_source_snapshot(
        "Makefile",
        b"define SCRIPT\necho: hi\nendef\ninclude $(GENERATED_MKS)\ninclude static.mk\nreal: dep\n",
    );
    assert!(
        !make.symbols.iter().any(|symbol| symbol.name == "echo"),
        "Make define body lines must not become rule targets: {:?}",
        make.symbols
    );
    assert!(
        !make
            .imports
            .iter()
            .any(|import| import.module.contains("GENERATED_MKS"))
            && make
                .imports
                .iter()
                .any(|import| import.module == "static.mk"),
        "Make variable-expanded includes should be skipped while static includes remain: {:?}",
        make.imports
    );

    let ninja = parse_source_snapshot("build.ninja", b"command = echo $$HOME ${real} $builddir\n");
    assert!(
        !ninja
            .references
            .iter()
            .any(|reference| reference.name == "HOME"),
        "Ninja escaped dollar literals must not become variable refs: {:?}",
        ninja.references
    );
    assert!(
        ninja
            .references
            .iter()
            .any(|reference| reference.name == "real")
            && ninja
                .references
                .iter()
                .any(|reference| reference.name == "builddir"),
        "Ninja real variable references should still be indexed: {:?}",
        ninja.references
    );
    let ninja_manifest = parse_source_snapshot(
        "build.ninja",
        b"rule cc\n  command = cc $in -o $out\nbuild app.o: cc app.c\n",
    );
    assert_eq!(
        ninja_manifest.files[0].parse_status,
        CodeParseStatus::Parsed,
        "valid Ninja manifests should not be degraded by the Make grammar fallback"
    );
    let ninja_dynamic_include = parse_source_snapshot(
        "build.ninja",
        b"builddir = out\nsubninja $builddir/rules.ninja\ninclude static.ninja\n",
    );
    assert!(
        !ninja_dynamic_include
            .imports
            .iter()
            .any(|import| import.module == "$builddir/rules.ninja")
            && ninja_dynamic_include
                .imports
                .iter()
                .any(|import| import.module == "static.ninja"),
        "Ninja variable-expanded includes should be skipped while static includes remain: {:?}",
        ninja_dynamic_include.imports
    );

    let dockerfile = parse_source_snapshot(
        "Dockerfile",
        b"ARG BASE_IMAGE=alpine\nFROM ${BASE_IMAGE} AS builder\nFROM alpine AS final\n",
    );
    assert!(
        !dockerfile
            .imports
            .iter()
            .any(|import| import.module.contains("BASE_IMAGE"))
            && dockerfile
                .imports
                .iter()
                .any(|import| import.module == "alpine"),
        "Dockerfile variable-expanded base images should not become imports: {:?}",
        dockerfile.imports
    );
    let docker_variants = parse_sources_snapshot(&[
        ("Dockerfile.dev", b"FROM alpine AS builder\n" as &[u8]),
        ("Dockerfile.prod", b"FROM alpine AS builder\n" as &[u8]),
    ]);
    let builder_ids = docker_variants
        .symbols
        .iter()
        .filter(|symbol| symbol.name == "builder")
        .map(|symbol| symbol.canonical_symbol_id.as_str())
        .collect::<Vec<_>>();
    assert_eq!(
        builder_ids.len(),
        2,
        "Dockerfile variants should both expose their stage symbols: {:?}",
        docker_variants.symbols
    );
    assert!(
        builder_ids
            .iter()
            .any(|canonical_id| canonical_id.contains("Dockerfile.dev"))
            && builder_ids
                .iter()
                .any(|canonical_id| canonical_id.contains("Dockerfile.prod"))
            && builder_ids[0] != builder_ids[1],
        "Dockerfile variant suffixes should remain in canonical symbol ids: {:?}",
        builder_ids
    );
    let docker_copy_from = parse_source_snapshot(
        "Dockerfile",
        b"FROM alpine AS builder\nFROM scratch AS final\nCOPY --from=builder /out /app\nCOPY --from=nginx:alpine /etc/nginx /nginx\n",
    );
    let copy_stage_ref = docker_copy_from
        .references
        .iter()
        .find(|reference| reference.name == "builder" && reference.kind == "stage")
        .expect("Docker COPY --from should reference prior stages");
    assert_eq!(
        copy_stage_ref.resolution_state, "resolved",
        "Docker COPY --from stage references should resolve in the same file: {:?}",
        docker_copy_from.references
    );
    assert!(
        docker_copy_from
            .imports
            .iter()
            .any(|import| import.module == "nginx:alpine")
            && !docker_copy_from
                .imports
                .iter()
                .any(|import| import.module == "builder"),
        "Docker COPY --from should import external images but not prior stages: {:?}",
        docker_copy_from.imports
    );

    let go_template = parse_source_snapshot(
        "templates/deployment.yaml",
        br#"{{- /* {{ include "old.tpl" . }} */ -}}{{ include "current.tpl" . }}"#,
    );
    assert!(
        !go_template
            .imports
            .iter()
            .any(|import| import.module == "old.tpl")
            && go_template
                .imports
                .iter()
                .any(|import| import.module == "current.tpl"),
        "trimmed Go-template comments should not emit disabled imports: {:?}",
        go_template.imports
    );
    let dynamic_go_template = parse_source_snapshot(
        "templates/deployment.yaml",
        br#"{{ include .Template.Name . }} docs: "old.tpl"
{{ include "current.tpl" . }}"#,
    );
    assert!(
        !dynamic_go_template
            .imports
            .iter()
            .any(|import| import.module == "old.tpl")
            && dynamic_go_template
                .imports
                .iter()
                .any(|import| import.module == "current.tpl"),
        "Go-template imports must stop scanning at the closing action delimiter: {:?}",
        dynamic_go_template.imports
    );
    let control_only_go_template = parse_source_snapshot(
        "templates/NOTES.txt",
        br#"{{- if .Values.enabled -}}ready{{- end -}}"#,
    );
    assert_eq!(
        control_only_go_template.files[0].parse_status,
        CodeParseStatus::Parsed,
        "balanced Go-template control actions should not require extracted facts"
    );
    let go_template_block = parse_source_snapshot(
        "templates/layout.tpl",
        br#"{{ block "content" . }}fallback{{ end }}"#,
    );
    assert!(
        go_template_block
            .symbols
            .iter()
            .any(|symbol| symbol.name == "content" && symbol.kind == "template")
            && go_template_block
                .references
                .iter()
                .any(|reference| reference.name == "content" && reference.kind == "template")
            && go_template_block
                .imports
                .iter()
                .any(|import| import.module == "content"),
        "Go-template block actions should define and execute the named template: {:?} {:?} {:?}",
        go_template_block.symbols,
        go_template_block.references,
        go_template_block.imports
    );
    let templated_yaml = parse_source_snapshot(
        "templates/deployment.yaml",
        br#"apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Values.name }}
{{- if .Values.enabled }}
spec:
  replicas: 1
{{- end }}
{{ include "app.labels" . }}
"#,
    );
    assert_eq!(
        templated_yaml.files[0].parse_status,
        CodeParseStatus::Parsed,
        "valid Go-template manifests should not be degraded by the Jinja grammar fallback"
    );
    assert!(
        ["apiVersion", "kind", "metadata", "name", "spec", "replicas"]
            .iter()
            .all(|key| templated_yaml
                .symbols
                .iter()
                .any(|symbol| { symbol.name == *key && symbol.kind == "config_key" })),
        "templated YAML manifests should still expose static config keys: {:?}",
        templated_yaml.symbols
    );
    assert!(
        templated_yaml
            .references
            .iter()
            .any(|reference| reference.name == "app.labels" && reference.kind == "template"),
        "templated YAML manifests should still expose helper references: {:?}",
        templated_yaml.references
    );

    let helm_notes = parse_source_snapshot(
        "charts/app/templates/NOTES.txt",
        br#"{{ include "app.labels" . }}"#,
    );
    assert_eq!(helm_notes.files[0].language_id, "gotemplate");
    assert!(
        helm_notes
            .imports
            .iter()
            .any(|import| import.module == "app.labels"),
        "Helm NOTES.txt should be indexed as a Go template: {:?}",
        helm_notes.imports
    );

    let helm_template_scope = parse_sources_snapshot(&[
        (
            "charts/app/templates/deployment.yaml",
            br#"{{ include "app.labels" . }}"# as &[u8],
        ),
        (
            "charts/app/templates/_helpers.tpl",
            br#"{{ define "app.labels" }}{{ end }}"# as &[u8],
        ),
        (
            "charts/other/templates/_helpers.tpl",
            br#"{{ define "app.labels" }}{{ end }}"# as &[u8],
        ),
    ]);
    let app_helper = helm_template_scope
        .symbols
        .iter()
        .find(|symbol| {
            symbol.path == "charts/app/templates/_helpers.tpl" && symbol.name == "app.labels"
        })
        .expect("same-chart helper should be indexed");
    let app_template_reference = helm_template_scope
        .references
        .iter()
        .find(|reference| {
            reference.path == "charts/app/templates/deployment.yaml"
                && reference.name == "app.labels"
                && reference.kind == "template"
        })
        .expect("same-chart helper reference should be indexed");
    assert_eq!(
        app_template_reference.target_symbol_snapshot_id.as_deref(),
        Some(app_helper.symbol_snapshot_id.as_str()),
        "Helm template references should resolve inside the current chart before other charts: {:?}",
        helm_template_scope.references
    );

    let missing_local_template = parse_sources_snapshot(&[
        (
            "charts/app/templates/deployment.yaml",
            br#"{{ include "app.labels" . }}"# as &[u8],
        ),
        (
            "charts/other/templates/_helpers.tpl",
            br#"{{ define "app.labels" }}{{ end }}"# as &[u8],
        ),
    ]);
    let missing_local_reference = missing_local_template
        .references
        .iter()
        .find(|reference| {
            reference.path == "charts/app/templates/deployment.yaml"
                && reference.name == "app.labels"
                && reference.kind == "template"
        })
        .expect("cross-chart helper reference should be indexed");
    assert_eq!(
        missing_local_reference.target_symbol_snapshot_id, None,
        "Helm template references must not resolve across chart template roots: {:?}",
        missing_local_template.references
    );
    assert_eq!(missing_local_reference.resolution_state, "unresolved");
}

fn parse_source_snapshot(path: &str, source: &[u8]) -> CodeIndexSnapshot {
    parse_sources_snapshot(&[(path, source)])
}

fn parse_sources_snapshot(files: &[(&str, &[u8])]) -> CodeIndexSnapshot {
    let registration =
        CodeRepositoryRegistration::new("repo", "alias", "/tmp/repo", Vec::new(), Vec::new())
            .expect("registration should validate");
    let mut build = SnapshotBuild::new(
        &registration,
        "commit".to_owned(),
        "tree".to_owned(),
        true,
        1,
        0,
    );

    for (path, source) in files {
        parse_indexed_file(&mut build, path, source).expect("file should parse");
    }

    build.finish()
}