devscout-rs 0.7.0

Fast code index for C# and TypeScript codebases -- map, find, refs, impact from the CLI.
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
// A lightweight, hand-scanned model of the repo's `.csproj` projects: which
// directory each project owns, which other projects it references, and
// whether it is a test project. No MSBuild is invoked and no XML crate is
// used -- `scan_csproj`/`scan_props` below hand-scan just enough of the file
// to answer those three questions.
//
// ## Discovery
//
// `discover` walks each scope directory recursively (via
// `walk::list_files_with_ext`, so it inherits that walk's `SKIP_DIRS` and
// deterministic ordering) looking for `.csproj` and `.props` files, then
// additionally checks each scope directory's ancestors -- one directory at a
// time, NOT recursively -- up to and including `root`. That ancestor check
// exists because a repo-root `Directory.Build.props` commonly sits above
// every scope directory and would otherwise never be seen: the recursive
// walk only covers subtrees rooted AT a scope directory. `.props` files are
// filtered to the exact name `Directory.Build.props`; `.sln` files are never
// looked for (no extension in the scan list) and so are always ignored.
//
// ## Units and directory ownership
//
// Every discovered `.csproj` becomes a `Unit` keyed by its repo-relative path
// (`id`) and owning its containing directory (`dir`). `ProjectModel` maps a
// directory to its unit only when that directory holds EXACTLY one `.csproj`
// -- a directory holding two or more is deliberately left unmapped, so
// `unit_of_file` walks past it to the nearest single-project ancestor
// instead of guessing.
//
// ## Test detection
//
// `Unit.test` is decided by a fixed precedence, first source to answer wins:
// 1. an explicit `<IsTestProject>` element in the csproj itself;
// 2. an explicit `<IsTestProject>` in the nearest `Directory.Build.props`
//    (searched from the csproj's own directory upward to `root`; a props
//    file with no such element does not count as an answer and defers to the
//    next source, but a further-out props file is never consulted once a
//    nearer one has been found);
// 3. a `Microsoft.NET.Test.Sdk` `PackageReference`;
// 4. a project name ending in `.Tests` or `.Test`.
//
// ## Reference closure
//
// `Unit.refs` holds only DIRECT `ProjectReference` targets, normalized to
// repo-relative ids (backslashes to forward slashes, `..`/`.` resolved
// against the csproj's own directory). A reference that resolves above
// `root` is kept as a literal string starting with `..` rather than clamped
// -- clamping could accidentally alias it onto an unrelated real unit id, and
// a leading `..` can never match one (ids are always repo-relative paths
// with no `..` component), so it safely resolves to nothing everywhere a ref
// id is looked up. A reference naming an id no `Unit` actually has is simply
// ignored when the closure is built.
//
// `ProjectModel::from_units` computes each unit's full transitive closure by
// BFS over `refs` once, up front, so `reachable` is an O(1) set lookup.

use std::collections::{HashMap, HashSet, VecDeque};
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use crate::graph;
use crate::repo;
use crate::walk;

/// One `.csproj` project discovered under the repo.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unit {
    /// Repo-relative path to the `.csproj` file itself (`/`-joined), e.g.
    /// `"src/App/App.csproj"`. Doubles as this unit's identity: `refs`
    /// entries and cross-unit lookups are matched against this string.
    pub id: String,
    /// The project name -- the csproj file name with its `.csproj`
    /// extension stripped (e.g. `"App.Tests"` for `App.Tests.csproj`). Used
    /// only for the name-suffix fallback in test detection; not otherwise
    /// assumed to be unique.
    pub name: String,
    /// Repo-relative path to the directory containing the `.csproj` file
    /// (`/`-joined, no trailing slash; `""` for a `.csproj` directly at
    /// `root`).
    pub dir: String,
    /// The `id`s of this unit's DIRECT `ProjectReference` targets only (not
    /// transitively closed -- see `ProjectModel::reachable` for that). An
    /// entry that does not match any discovered unit's `id` (an out-of-root
    /// target, or a project this scan never found) is kept verbatim; it
    /// simply never resolves to anything.
    pub refs: Vec<String>,
    /// Whether this project is a test project, per the precedence documented
    /// on the module.
    pub test: bool,
}

/// The repo's discovered `.csproj` projects, their direct references, and
/// the transitive closure over those references.
#[derive(Debug, Clone)]
pub struct ProjectModel {
    /// All discovered units, sorted by `id`. A unit's position in this
    /// vector is its index everywhere else in this type (`dir_to_unit`
    /// values, `closure` entries, `reachable`'s arguments).
    pub units: Vec<Unit>,
    // Directory -> unit index, present only for a directory that owns
    // EXACTLY one unit. A directory with zero units has no entry (nothing to
    // map); a directory with two or more also has no entry, on purpose --
    // see `unit_of_file`.
    dir_to_unit: HashMap<String, usize>,
    // closure[i] is the set of unit indices transitively reachable from unit
    // i by following `refs`, NOT including i itself (even if a reference
    // cycle loops back to it -- `reachable` handles the `from == to` case
    // separately).
    closure: Vec<HashSet<usize>>,
}

/// Discovers the repo's `.csproj` project model under `scope`.
///
/// `scope` holds directory names relative to `root`, `"."` meaning `root`
/// itself -- same convention as `walk::list_source_files`. `Ok(None)` when
/// zero `.csproj` files are found anywhere in scope or above it; discovery
/// never fails just because nothing was there to discover.
///
/// See the module docs for the full discovery, ownership, test-detection and
/// closure rules.
pub fn discover(root: &Path, scope: &[String]) -> io::Result<Option<ProjectModel>> {
    let mut csproj_files: HashSet<PathBuf> = HashSet::new();
    let mut props_by_dir: HashMap<PathBuf, String> = HashMap::new();

    // Recursive walk of each scope directory.
    let found = walk::list_files_with_ext(root, scope, &[".csproj", ".props"])?;
    for rel in found {
        let abs = root.join(&rel);
        if rel.ends_with(".csproj") {
            csproj_files.insert(abs);
        } else if is_directory_build_props(&abs) {
            if let (Some(dir), Some(text)) = (abs.parent(), read_lossy(&abs)) {
                props_by_dir.insert(dir.to_path_buf(), text);
            }
        }
    }

    // Each scope dir's ancestors up to root, checked non-recursively (one
    // `read_dir` per ancestor, no descent into siblings).
    for d in scope {
        let scope_abs = if d == "." {
            root.to_path_buf()
        } else {
            root.join(d)
        };
        for ancestor in ancestors_up_to(root, &scope_abs) {
            scan_ancestor_dir(&ancestor, &mut csproj_files, &mut props_by_dir);
        }
    }

    if csproj_files.is_empty() {
        return Ok(None);
    }

    let mut units = Vec::with_capacity(csproj_files.len());
    for csproj_path in &csproj_files {
        units.push(build_unit(root, csproj_path, &props_by_dir)?);
    }

    Ok(Some(ProjectModel::from_units(units)))
}

// The ancestor directories of `dir`, starting at its parent and continuing
// up to and including `root`. Empty when `dir == root` (no ancestors left to
// check within the repo) or when `dir` is not under `root` at all.
fn ancestors_up_to(root: &Path, dir: &Path) -> Vec<PathBuf> {
    let mut out = Vec::new();
    let mut cur = dir.to_path_buf();
    while cur != root {
        let Some(parent) = cur.parent() else {
            break;
        };
        out.push(parent.to_path_buf());
        if parent == root {
            break;
        }
        cur = parent.to_path_buf();
    }
    out
}

// Non-recursive `.csproj`/`Directory.Build.props` scan of a single
// directory's direct entries. A missing or unreadable directory (e.g. an
// ancestor computed lexically from a scope element that does not exist on
// disk) is silently skipped, matching `walk`'s "nonexistent scope element"
// behavior rather than failing the whole discovery.
fn scan_ancestor_dir(
    dir: &Path,
    csproj_files: &mut HashSet<PathBuf>,
    props_by_dir: &mut HashMap<PathBuf, String>,
) {
    let Ok(entries) = fs::read_dir(dir) else {
        return;
    };
    for entry in entries.flatten() {
        let Ok(file_type) = entry.file_type() else {
            continue;
        };
        if !file_type.is_file() {
            continue;
        }
        let path = entry.path();
        if path
            .file_name()
            .and_then(|n| n.to_str())
            .is_some_and(|n| n.ends_with(".csproj"))
        {
            csproj_files.insert(path);
        } else if is_directory_build_props(&path) {
            if let Some(text) = read_lossy(&path) {
                props_by_dir.insert(dir.to_path_buf(), text);
            }
        }
    }
}

fn is_directory_build_props(path: &Path) -> bool {
    path.file_name().and_then(|n| n.to_str()) == Some("Directory.Build.props")
}

fn read_lossy(path: &Path) -> Option<String> {
    fs::read(path)
        .ok()
        .map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
}

// Reads and scans one discovered `.csproj` into its `Unit`. `props_by_dir`
// is the full set of `Directory.Build.props` files discovery already found
// (recursive-walk plus ancestor scan combined), keyed by their own
// directory, so the nearest-ancestor search below never touches the disk
// again.
fn build_unit(
    root: &Path,
    csproj_path: &Path,
    props_by_dir: &HashMap<PathBuf, String>,
) -> io::Result<Unit> {
    let id = repo::rel_path(root, csproj_path);
    let dir_abs = csproj_path.parent().unwrap_or(root).to_path_buf();
    let dir = repo::rel_path(root, &dir_abs);
    let name = csproj_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("")
        .to_string();

    let bytes = fs::read(csproj_path)?;
    let text = String::from_utf8_lossy(&bytes).into_owned();
    let facts = scan_csproj(&text);

    let refs = facts
        .project_refs
        .iter()
        .map(|raw| resolve_ref(&dir, raw))
        .collect();

    // Precedence: explicit csproj value, then the nearest Directory.Build.props
    // that has one, then the test-SDK package, then the name suffix.
    let test = facts
        .is_test_project
        .or_else(|| nearest_props_is_test_project(root, &dir_abs, props_by_dir))
        .unwrap_or_else(|| {
            facts.has_test_sdk || name.ends_with(".Tests") || name.ends_with(".Test")
        });

    Ok(Unit {
        id,
        name,
        dir,
        refs,
        test,
    })
}

// Walks from `start_dir` (the unit's own directory) upward to and including
// `root`, returning the `<IsTestProject>` value from the FIRST
// `Directory.Build.props` found -- `None` if that nearest file has no such
// element (a further-out props file is not consulted) or if none exists on
// the path at all.
fn nearest_props_is_test_project(
    root: &Path,
    start_dir: &Path,
    props_by_dir: &HashMap<PathBuf, String>,
) -> Option<bool> {
    let mut cur = start_dir.to_path_buf();
    loop {
        if let Some(text) = props_by_dir.get(&cur) {
            return scan_props(text);
        }
        if cur == root {
            return None;
        }
        cur = cur.parent()?.to_path_buf();
    }
}

impl ProjectModel {
    /// Builds a `ProjectModel` from an already-scanned unit list: sorts
    /// `units` by `id`, maps each directory that owns exactly one unit to
    /// it, and computes each unit's transitive reference closure by BFS.
    /// This is the constructor `discover` itself uses once scanning is done,
    /// and the one a persistence layer would use to rebuild a `ProjectModel`
    /// from a stored unit list without re-scanning any files.
    pub fn from_units(mut units: Vec<Unit>) -> Self {
        units.sort_by(|a, b| a.id.cmp(&b.id));

        let id_to_index: HashMap<&str, usize> = units
            .iter()
            .enumerate()
            .map(|(i, u)| (u.id.as_str(), i))
            .collect();

        let mut dir_counts: HashMap<&str, usize> = HashMap::new();
        for u in &units {
            *dir_counts.entry(u.dir.as_str()).or_insert(0) += 1;
        }
        let mut dir_to_unit = HashMap::new();
        for (i, u) in units.iter().enumerate() {
            if dir_counts.get(u.dir.as_str()) == Some(&1) {
                dir_to_unit.insert(u.dir.clone(), i);
            }
        }

        let closure = build_closure(&units, &id_to_index);

        ProjectModel {
            units,
            dir_to_unit,
            closure,
        }
    }

    /// The unit owning `rel` (a repo-relative file path, `/`-joined): the
    /// nearest ancestor directory of `rel` that owns EXACTLY one unit.
    /// A directory owning zero units is walked past because nothing maps
    /// it; a directory owning two or more is ALSO walked past, on purpose
    /// (`ProjectModel::from_units` never maps an ambiguous directory) --
    /// climbing to the next single-project ancestor instead of guessing
    /// which of the colliding projects owns the file. `None` when no
    /// ancestor, up to and including the repo root, owns exactly one unit.
    pub fn unit_of_file(&self, rel: &str) -> Option<usize> {
        let mut dir = match rel.rfind('/') {
            Some(i) => &rel[..i],
            None => "",
        };
        loop {
            if let Some(&idx) = self.dir_to_unit.get(dir) {
                return Some(idx);
            }
            if dir.is_empty() {
                return None;
            }
            dir = match dir.rfind('/') {
                Some(i) => &dir[..i],
                None => "",
            };
        }
    }

    /// Whether `to` is reachable from `from`: trivially true when they are
    /// the same unit, otherwise true iff `to` is in `from`'s precomputed
    /// transitive `ProjectReference` closure.
    pub fn reachable(&self, from: usize, to: usize) -> bool {
        from == to || self.closure.get(from).is_some_and(|c| c.contains(&to))
    }
}

// ---------------------------------------------------------------------------
// Persistence bridge -- `graph.json`'s `units` array and the staleness
// sidecar beside it. The on-disk row type (`graph::GraphUnit`) lives in
// graph.rs with every other serde struct; the two conversions and the
// comparison live here, next to the model they describe.
// ---------------------------------------------------------------------------

/// The model's units as graph.json persists them, in `ProjectModel` order
/// (sorted by `id`). `dir` is dropped: it is always `id`'s parent directory
/// and `units_from_graph` recomputes it.
pub fn graph_units(model: &ProjectModel) -> Vec<graph::GraphUnit> {
    model
        .units
        .iter()
        .map(|u| graph::GraphUnit {
            id: u.id.clone(),
            name: u.name.clone(),
            refs: u.refs.clone(),
            test: u.test,
        })
        .collect()
}

/// The inverse of `graph_units`: persisted rows back into `Unit`s, ready for
/// `ProjectModel::from_units`.
///
/// `dir` is recomputed as `id`'s parent (`""` for
/// a `.csproj` sitting directly at the repo root), which is exactly what
/// `discover` derived it from in the first place.
pub fn units_from_graph(rows: &[graph::GraphUnit]) -> Vec<Unit> {
    rows.iter()
        .map(|r| Unit {
            id: r.id.clone(),
            name: r.name.clone(),
            dir: match r.id.rfind('/') {
                Some(i) => r.id[..i].to_string(),
                None => String::new(),
            },
            refs: r.refs.clone(),
            test: r.test,
        })
        .collect()
}

/// Whether `model` disagrees with the project-units sidecar last written for
/// `root` -- `map`'s "did a `.csproj` change?" signal.
///
/// It has to exist because a `.csproj` is not a `SOURCE_EXT`: editing one
/// moves no indexed file, so `graph::index_is_stale` stays false and the
/// graph would keep a stale reference closure forever. The comparison is a
/// raw BYTE compare against the sidecar rather than a parse-and-diff -- the
/// sidecar holds exactly what `graph_units` serializes, so any difference at
/// all (a new project, a dropped `ProjectReference`, a flipped `test`) shows
/// up as different bytes, and an unreadable or truncated sidecar reads as
/// differing, which costs one rebuild and is the safe direction.
///
/// A missing sidecar with a model is a difference (nothing was ever written,
/// or the last run had no projects); a missing sidecar with NO model is not
/// -- that is the steady state of every repo that has no `.csproj`, and it
/// must never force a rebuild.
pub fn sidecar_differs(root: &Path, model: Option<&ProjectModel>) -> bool {
    let existing = fs::read(graph::project_units_path(root)).ok();
    match (existing, model) {
        (None, None) => false,
        (None, Some(_)) => true,
        // A sidecar left over from when the repo still had projects: the
        // model went away, so the graph's `units` must go away with it.
        (Some(_), None) => true,
        (Some(bytes), Some(m)) => match serde_json::to_vec(&graph_units(m)) {
            Ok(fresh) => bytes != fresh,
            Err(_) => true,
        },
    }
}

// BFS per unit over `refs` (resolved through `id_to_index`; an unresolved
// ref id is simply skipped) to produce each unit's full transitive closure
// once, up front.
fn build_closure(units: &[Unit], id_to_index: &HashMap<&str, usize>) -> Vec<HashSet<usize>> {
    units
        .iter()
        .enumerate()
        .map(|(i, _)| {
            let mut seen = HashSet::new();
            seen.insert(i);
            let mut queue: VecDeque<usize> = VecDeque::new();
            queue.push_back(i);
            let mut result = HashSet::new();
            while let Some(cur) = queue.pop_front() {
                for r in &units[cur].refs {
                    if let Some(&idx) = id_to_index.get(r.as_str()) {
                        if seen.insert(idx) {
                            result.insert(idx);
                            queue.push_back(idx);
                        }
                    }
                }
            }
            result
        })
        .collect()
}

// ---------------------------------------------------------------------------
// Hand-written XML scanning -- no XML crate. Just enough tag/attribute
// recognition to answer `scan_csproj`/`scan_props`'s three questions; nothing
// here builds a DOM or validates well-formedness.
// ---------------------------------------------------------------------------

// The facts `scan_csproj` extracts from a `.csproj` file's raw text.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
struct CsprojFacts {
    // Raw `ProjectReference` `Include` values, backslashes already turned to
    // forward slashes, but NOT yet resolved against any directory (the caller
    // does that with `resolve_ref`, since this function has no directory
    // context).
    project_refs: Vec<String>,
    // Whether any `PackageReference Include="Microsoft.NET.Test.Sdk"` tag
    // was seen.
    has_test_sdk: bool,
    // The parsed content of an `<IsTestProject>` element, if one was seen
    // with a recognizable `true`/`false` value (case-insensitive).
    is_test_project: Option<bool>,
}

// Scans `text` (a `.csproj` file's contents) for `ProjectReference`/
// `PackageReference` `Include` attributes and an `<IsTestProject>` element.
// XML comments (`<!-- ... -->`) are stripped first, so a fake reference
// living inside a comment is never seen.
fn scan_csproj(text: &str) -> CsprojFacts {
    let clean = strip_xml_comments(text);
    let mut facts = CsprojFacts::default();
    let mut pos = 0usize;
    let mut awaiting_close: Option<&str> = None;

    while let Some((gap, tag)) = next_tag(&clean, &mut pos) {
        if let Some(open_name) = awaiting_close {
            if tag.is_closing && tag.name == open_name {
                let value = gap.trim();
                if value.eq_ignore_ascii_case("true") {
                    facts.is_test_project = Some(true);
                } else if value.eq_ignore_ascii_case("false") {
                    facts.is_test_project = Some(false);
                }
            }
            awaiting_close = None;
        }

        if tag.is_closing {
            continue;
        }

        match tag.name {
            "ProjectReference" => {
                if let Some(include) = find_attr(tag.attrs, "Include") {
                    facts.project_refs.push(include.replace('\\', "/"));
                }
            }
            "PackageReference" => {
                if let Some(include) = find_attr(tag.attrs, "Include") {
                    if include == "Microsoft.NET.Test.Sdk" {
                        facts.has_test_sdk = true;
                    }
                }
            }
            "IsTestProject" if !tag.self_closing => {
                awaiting_close = Some("IsTestProject");
            }
            _ => {}
        }
    }

    facts
}

// Scans a `Directory.Build.props` file's contents for an `<IsTestProject>`
// element. Reuses `scan_csproj`'s scan (`Directory.Build.props` uses the same
// MSBuild element syntax); the `ProjectReference`/`PackageReference` facts it
// also computes are simply unused here.
fn scan_props(text: &str) -> Option<bool> {
    scan_csproj(text).is_test_project
}

// Resolves a raw (already backslash-normalized) `Include` path against
// `csproj_dir` (the referencing csproj's own repo-relative directory,
// `/`-joined, `""` for root) to a repo-relative id: `.` segments drop out,
// `..` pops the last real segment. A `..` with nothing left to pop (the
// reference climbs above `root`) is kept as a literal leading `..` segment
// instead of being clamped -- see the module docs for why that is safe.
fn resolve_ref(csproj_dir: &str, raw: &str) -> String {
    let mut stack: Vec<&str> = if csproj_dir.is_empty() {
        Vec::new()
    } else {
        csproj_dir.split('/').collect()
    };
    for seg in raw.split('/') {
        match seg {
            "" | "." => {}
            ".." => {
                if matches!(stack.last(), None | Some(&"..")) {
                    stack.push("..");
                } else {
                    stack.pop();
                }
            }
            other => stack.push(other),
        }
    }
    stack.join("/")
}

// Strips `<!-- ... -->` regions from `text`. An unterminated comment (no
// closing `-->`) drops everything from the `<!--` onward.
fn strip_xml_comments(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    loop {
        match rest.find("<!--") {
            Some(start) => {
                out.push_str(&rest[..start]);
                match rest[start..].find("-->") {
                    Some(end_rel) => {
                        rest = &rest[start + end_rel + 3..];
                    }
                    None => return out,
                }
            }
            None => {
                out.push_str(rest);
                return out;
            }
        }
    }
}

// One `<...>` tag, already split into its name and raw attribute text.
struct TagInfo<'a> {
    name: &'a str,
    is_closing: bool,
    self_closing: bool,
    // Everything after the name, unparsed -- `find_attr` reads out of this.
    // Empty for a closing tag.
    attrs: &'a str,
}

// Advances `*pos` past the next `<...>` tag in `text` found at or after
// `*pos`, returning it along with the plain text that preceded it (the gap
// between the previous tag's `>` and this tag's `<`) -- callers that need an
// element's text content (`IsTestProject`) read it out of the NEXT call's
// `gap`, since that is exactly the text between this tag and whatever tag
// follows it. `None` once no further tag is found.
//
// The search for the tag's closing `>` tracks single/double-quoted spans so
// a multi-line attribute value (or one that happens to be empty) does not
// end the tag early; nothing here assumes a tag fits on one line.
fn next_tag<'a>(text: &'a str, pos: &mut usize) -> Option<(&'a str, TagInfo<'a>)> {
    let search_from = *pos;
    let lt = text[search_from..].find('<')? + search_from;
    let gap = &text[search_from..lt];

    let bytes = text.as_bytes();
    let mut i = lt + 1;
    let mut quote: Option<u8> = None;
    let mut gt = None;
    while i < bytes.len() {
        let c = bytes[i];
        match quote {
            Some(q) => {
                if c == q {
                    quote = None;
                }
            }
            None => {
                if c == b'"' || c == b'\'' {
                    quote = Some(c);
                } else if c == b'>' {
                    gt = Some(i);
                    break;
                }
            }
        }
        i += 1;
    }
    let gt = gt?;
    *pos = gt + 1;

    let content = text[lt + 1..gt].trim();
    if let Some(name) = content.strip_prefix('/') {
        return Some((
            gap,
            TagInfo {
                name: name.trim(),
                is_closing: true,
                self_closing: false,
                attrs: "",
            },
        ));
    }

    let self_closing = content.ends_with('/');
    let body = if self_closing {
        content[..content.len() - 1].trim_end()
    } else {
        content
    };
    let (name, attrs) = match body.find(|c: char| c.is_whitespace()) {
        Some(i2) => (&body[..i2], body[i2..].trim()),
        None => (body, ""),
    };

    Some((
        gap,
        TagInfo {
            name,
            is_closing: false,
            self_closing,
            attrs,
        },
    ))
}

// Finds `name="value"` (or `name='value'`) inside a tag's raw attribute
// text, requiring `name` to be a whole attribute name -- preceded by the
// start of `attrs` or whitespace, and followed (after optional whitespace)
// directly by `=`, not by more identifier characters. Values may span
// multiple lines (the search does not treat `\n` specially).
fn find_attr(attrs: &str, name: &str) -> Option<String> {
    let mut i = 0usize;
    while i < attrs.len() {
        let found = attrs[i..].find(name)?;
        let abs = i + found;
        let boundary_before = abs == 0 || attrs.as_bytes()[abs - 1].is_ascii_whitespace();
        if boundary_before {
            let after = &attrs[abs + name.len()..];
            let after_trimmed = after.trim_start();
            if let Some(value_part) = after_trimmed.strip_prefix('=') {
                let value_part = value_part.trim_start();
                if let Some(quote) = value_part.chars().next() {
                    if quote == '"' || quote == '\'' {
                        if let Some(end) = value_part[quote.len_utf8()..].find(quote) {
                            return Some(
                                value_part[quote.len_utf8()..quote.len_utf8() + end].to_string(),
                            );
                        }
                    }
                }
            }
        }
        i = abs + name.len();
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    static COUNTER: AtomicU64 = AtomicU64::new(0);

    fn scratch_dir(label: &str) -> PathBuf {
        let n = COUNTER.fetch_add(1, Ordering::SeqCst);
        let dir = std::env::temp_dir().join(format!(
            "scout-project-rs-{label}-{}-{n}",
            std::process::id()
        ));
        fs::create_dir_all(&dir).expect("create scratch dir");
        dir
    }

    fn write_file(path: &Path, contents: &str) {
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).expect("create parent dirs");
        }
        fs::write(path, contents).expect("write file");
    }

    // -- scan_csproj: ProjectReference / PackageReference -------------------

    #[test]
    fn scan_csproj_reads_include_from_a_self_closing_project_reference() {
        let facts = scan_csproj(
            r#"<Project><ItemGroup><ProjectReference Include="../B/B.csproj" /></ItemGroup></Project>"#,
        );
        assert_eq!(facts.project_refs, vec!["../B/B.csproj".to_string()]);
    }

    #[test]
    fn scan_csproj_reads_include_from_an_open_close_project_reference() {
        let facts = scan_csproj(
            r#"<ItemGroup><ProjectReference Include="../B/B.csproj"><Private>false</Private></ProjectReference></ItemGroup>"#,
        );
        assert_eq!(facts.project_refs, vec!["../B/B.csproj".to_string()]);
    }

    #[test]
    fn scan_csproj_reads_a_multi_line_attribute() {
        let xml =
            "<ItemGroup>\n  <ProjectReference\n    Include=\"../B/B.csproj\"\n  />\n</ItemGroup>";
        let facts = scan_csproj(xml);
        assert_eq!(facts.project_refs, vec!["../B/B.csproj".to_string()]);
    }

    #[test]
    fn scan_csproj_reads_single_quoted_attributes() {
        let facts = scan_csproj(
            r#"<ItemGroup><PackageReference Include='Microsoft.NET.Test.Sdk' Version='17.0.0' /></ItemGroup>"#,
        );
        assert!(facts.has_test_sdk);
    }

    #[test]
    fn scan_csproj_skips_a_project_reference_inside_a_comment() {
        let xml = r#"
            <ItemGroup>
              <!-- <ProjectReference Include="../Fake/Fake.csproj" /> -->
              <ProjectReference Include="../Real/Real.csproj" />
            </ItemGroup>
        "#;
        let facts = scan_csproj(xml);
        assert_eq!(facts.project_refs, vec!["../Real/Real.csproj".to_string()]);
    }

    #[test]
    fn scan_csproj_detects_the_test_sdk_package_reference() {
        let facts = scan_csproj(
            r#"<ItemGroup><PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /></ItemGroup>"#,
        );
        assert!(facts.has_test_sdk);
    }

    #[test]
    fn scan_csproj_reads_explicit_is_test_project_true() {
        let facts =
            scan_csproj("<PropertyGroup><IsTestProject>true</IsTestProject></PropertyGroup>");
        assert_eq!(facts.is_test_project, Some(true));
    }

    #[test]
    fn scan_csproj_reads_explicit_is_test_project_false() {
        let facts =
            scan_csproj("<PropertyGroup><IsTestProject>False</IsTestProject></PropertyGroup>");
        assert_eq!(facts.is_test_project, Some(false));
    }

    #[test]
    fn scan_props_reads_is_test_project() {
        assert_eq!(
            scan_props("<Project><PropertyGroup><IsTestProject>true</IsTestProject></PropertyGroup></Project>"),
            Some(true)
        );
        assert_eq!(scan_props("<Project><PropertyGroup /></Project>"), None);
    }

    // -- resolve_ref ----------------------------------------------------------

    #[test]
    fn resolve_ref_resolves_dotdot_and_backslashes_against_the_csproj_directory() {
        let facts = scan_csproj(r#"<ProjectReference Include="..\..\Shared\Shared.csproj" />"#);
        assert_eq!(
            facts.project_refs,
            vec!["../../Shared/Shared.csproj".to_string()]
        );
        assert_eq!(
            resolve_ref("tests/T", "../../Shared/Shared.csproj"),
            "Shared/Shared.csproj"
        );
    }

    #[test]
    fn resolve_ref_keeps_an_out_of_root_target_as_a_normalised_string_that_matches_nothing() {
        let resolved = resolve_ref("src/A", "../../../Outside/Outside.csproj");
        assert_eq!(resolved, "../Outside/Outside.csproj");
        assert!(resolved.starts_with(".."));
    }

    // -- from_units: sorting, closure, reachability --------------------------

    fn unit(id: &str, dir: &str, refs: &[&str], test: bool) -> Unit {
        Unit {
            id: id.to_string(),
            name: id.trim_end_matches(".csproj").to_string(),
            dir: dir.to_string(),
            refs: refs.iter().map(|r| r.to_string()).collect(),
            test,
        }
    }

    #[test]
    fn from_units_sorts_by_id_and_reachable_is_reflexive_and_directional() {
        let model = ProjectModel::from_units(vec![
            unit("src/C/C.csproj", "src/C", &[], false),
            unit("src/A/A.csproj", "src/A", &["src/B/B.csproj"], false),
            unit("src/B/B.csproj", "src/B", &["src/C/C.csproj"], false),
        ]);

        let ids: Vec<&str> = model.units.iter().map(|u| u.id.as_str()).collect();
        assert_eq!(
            ids,
            vec!["src/A/A.csproj", "src/B/B.csproj", "src/C/C.csproj"]
        );

        let a = model
            .units
            .iter()
            .position(|u| u.id == "src/A/A.csproj")
            .unwrap();
        let b = model
            .units
            .iter()
            .position(|u| u.id == "src/B/B.csproj")
            .unwrap();
        let c = model
            .units
            .iter()
            .position(|u| u.id == "src/C/C.csproj")
            .unwrap();

        assert!(model.reachable(a, a), "a unit reaches itself");
        assert!(model.reachable(a, c), "A -> B -> C is transitive");
        assert!(!model.reachable(b, a), "B does not reference A");
        assert!(!model.reachable(c, b), "C references nothing");
        assert!(!model.reachable(c, a), "C cannot reach A");
    }

    #[test]
    fn from_units_ignores_an_unresolved_reference_in_the_closure() {
        let model = ProjectModel::from_units(vec![unit(
            "src/A/A.csproj",
            "src/A",
            &["src/Missing/Missing.csproj"],
            false,
        )]);
        let a = 0;
        assert!(model.closure[a].is_empty());
        assert!(!model.reachable(a, 99));
    }

    #[test]
    fn from_units_closes_a_reference_cycle_without_revisiting_a_unit() {
        // A -> B -> C -> A: the walk must terminate, every unit must reach the
        // other two, and a unit's own index is never listed in its closure.
        let model = ProjectModel::from_units(vec![
            unit("src/A/A.csproj", "src/A", &["src/B/B.csproj"], false),
            unit("src/B/B.csproj", "src/B", &["src/C/C.csproj"], false),
            unit("src/C/C.csproj", "src/C", &["src/A/A.csproj"], false),
        ]);
        for from in 0..3 {
            assert_eq!(
                model.closure[from].len(),
                2,
                "unit {from} reaches the other two"
            );
            assert!(!model.closure[from].contains(&from));
            for to in 0..3 {
                assert!(model.reachable(from, to), "{from} -> {to} inside the cycle");
            }
        }
    }

    #[test]
    fn unit_of_file_climbs_to_a_root_level_unit_from_any_nested_file() {
        let model = ProjectModel::from_units(vec![unit("A.csproj", "", &[], false)]);
        assert_eq!(model.unit_of_file("Elsewhere/Deep/x.cs"), Some(0));
    }

    #[test]
    fn unit_of_file_returns_none_when_no_ancestor_directory_has_exactly_one_unit() {
        // The only unit owns "src/A"; a file outside that whole directory
        // chain (root itself owns no unit here) never resolves.
        let model = ProjectModel::from_units(vec![unit("src/A/A.csproj", "src/A", &[], false)]);
        assert_eq!(model.unit_of_file("other/y.cs"), None);
    }

    // -- discover: end to end over real temp directories ---------------------

    #[test]
    fn discover_returns_none_when_no_csproj_is_under_scope_or_above_it() {
        let root = scratch_dir("no-csproj");
        write_file(&root.join("src/a.cs"), "class A {}\n");

        let result = discover(&root, &["src".to_string()]).unwrap();
        assert!(result.is_none());

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_builds_units_closure_and_test_flags_for_a_referenced_project_chain() {
        let root = scratch_dir("chain");
        write_file(
            &root.join("src/A/A.csproj"),
            r#"<Project><ItemGroup><ProjectReference Include="../B/B.csproj" /></ItemGroup></Project>"#,
        );
        write_file(
            &root.join("src/B/B.csproj"),
            r#"<Project><ItemGroup><ProjectReference Include="../C/C.csproj" /></ItemGroup></Project>"#,
        );
        write_file(&root.join("src/C/C.csproj"), "<Project></Project>");
        write_file(
            &root.join("tests/T/T.csproj"),
            r#"<Project>
                <ItemGroup>
                  <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
                  <ProjectReference Include="../../src/A/A.csproj" />
                </ItemGroup>
               </Project>"#,
        );
        write_file(&root.join("src/A/Sub/x.cs"), "class X {}\n");

        let model = discover(&root, &["src".to_string(), "tests".to_string()])
            .unwrap()
            .expect("csproj files were written under scope");

        let idx = |id: &str| model.units.iter().position(|u| u.id == id).unwrap();
        let a = idx("src/A/A.csproj");
        let b = idx("src/B/B.csproj");
        let c = idx("src/C/C.csproj");
        let t = idx("tests/T/T.csproj");

        assert!(model.reachable(a, c), "A -> B -> C must be transitive");
        assert!(!model.reachable(b, a), "B does not reference A");
        assert_eq!(model.unit_of_file("src/A/Sub/x.cs"), Some(a));
        assert!(model.units[t].test, "T carries the test SDK package");
        assert!(model.reachable(t, a), "T directly references A");

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_directory_with_two_csproj_files_is_skipped_when_resolving_a_file_beneath_it() {
        let root = scratch_dir("two-csproj-dir");
        write_file(&root.join("src/Root.csproj"), "<Project></Project>");
        write_file(&root.join("src/shared/X.csproj"), "<Project></Project>");
        write_file(&root.join("src/shared/Y.csproj"), "<Project></Project>");
        write_file(&root.join("src/shared/deep/z.cs"), "class Z {}\n");

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();

        let root_unit = model
            .units
            .iter()
            .position(|u| u.id == "src/Root.csproj")
            .unwrap();
        // "src/shared" owns two csproj files, so it is never mapped; a file
        // under it (directly or nested) resolves to the nearest single-csproj
        // ancestor, "src" (owning Root.csproj).
        assert_eq!(model.unit_of_file("src/shared/deep/z.cs"), Some(root_unit));
        assert_eq!(model.unit_of_file("src/shared/w.cs"), Some(root_unit));

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_props_precedence_beats_the_test_sdk_package() {
        let root = scratch_dir("props-precedence");
        write_file(
            &root.join("src/App/App.csproj"),
            r#"<Project><ItemGroup><PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /></ItemGroup></Project>"#,
        );
        write_file(
            &root.join("src/App/Directory.Build.props"),
            "<Project><PropertyGroup><IsTestProject>false</IsTestProject></PropertyGroup></Project>",
        );

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();
        let app = model
            .units
            .iter()
            .position(|u| u.id == "src/App/App.csproj")
            .unwrap();
        assert!(
            !model.units[app].test,
            "an explicit Directory.Build.props IsTestProject must beat the test-SDK package"
        );

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_name_suffix_fallback_when_nothing_else_says_test() {
        let root = scratch_dir("name-suffix");
        write_file(
            &root.join("src/App.Tests/App.Tests.csproj"),
            "<Project></Project>",
        );
        write_file(&root.join("src/App/App.csproj"), "<Project></Project>");

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();
        let tests_unit = model
            .units
            .iter()
            .position(|u| u.id == "src/App.Tests/App.Tests.csproj")
            .unwrap();
        let app_unit = model
            .units
            .iter()
            .position(|u| u.id == "src/App/App.csproj")
            .unwrap();

        assert!(model.units[tests_unit].test, "name ends with .Tests");
        assert!(!model.units[app_unit].test);

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_finds_a_directory_build_props_above_the_scope_root_via_ancestor_scan() {
        let root = scratch_dir("ancestor-props");
        write_file(
            &root.join("Directory.Build.props"),
            "<Project><PropertyGroup><IsTestProject>true</IsTestProject></PropertyGroup></Project>",
        );
        write_file(&root.join("src/A/A.csproj"), "<Project></Project>");

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();
        let a = model
            .units
            .iter()
            .position(|u| u.id == "src/A/A.csproj")
            .unwrap();
        assert!(
            model.units[a].test,
            "the root Directory.Build.props sits above the scope dir and must still be found"
        );

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_ancestor_scan_does_not_recurse_into_sibling_directories() {
        let root = scratch_dir("ancestor-no-recurse");
        write_file(&root.join("src/A/A.csproj"), "<Project></Project>");
        // Sibling of "src", one level below the ancestor "root" -- must NOT
        // be found: the ancestor check on "root" is non-recursive.
        write_file(&root.join("other/Other.csproj"), "<Project></Project>");

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();
        assert_eq!(model.units.len(), 1);
        assert_eq!(model.units[0].id, "src/A/A.csproj");

        fs::remove_dir_all(&root).ok();
    }

    #[test]
    fn discover_ignores_sln_files() {
        let root = scratch_dir("sln-ignored");
        write_file(
            &root.join("Repo.sln"),
            "Microsoft Visual Studio Solution File",
        );
        write_file(&root.join("src/A/A.csproj"), "<Project></Project>");

        let model = discover(&root, &["src".to_string()]).unwrap().unwrap();
        assert_eq!(model.units.len(), 1);
        assert_eq!(model.units[0].id, "src/A/A.csproj");

        fs::remove_dir_all(&root).ok();
    }
}