changepacks-csharp 0.3.2

C# (.NET) project support for changepacks (NuGet)
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
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
use anyhow::{Context, Result};
use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::{Reader, Writer};
use std::io::Cursor;

struct PropertyGroupContext {
    version_qname: Option<String>,
    close_ws: Option<String>,
    indent: String,
    depth: usize,
    eligible: bool,
}

fn clear_scope_close_ws(
    property_groups: &mut [PropertyGroupContext],
    project_close_ws: &mut Option<String>,
    project_depth: Option<usize>,
    element_depth: usize,
) {
    if let Some(property_group) = property_groups.last_mut() {
        property_group.close_ws = None;
    } else if project_depth == Some(element_depth) {
        *project_close_ws = None;
    }
}

/// Open an element scope and report the whitespace that preceded it.
///
/// When the element sits directly under `<Project>` the pending
/// `project_close_ws` belongs to it rather than to `</Project>`, so it is taken
/// and, the first time round, seeds `fallback_group_indent` with its trailing
/// indentation. Either way the innermost `PropertyGroup`'s `close_ws` is
/// cleared, because a nested element proves that whitespace was not the run
/// immediately before `</PropertyGroup>`.
///
/// `Event::Start` and `Event::Empty` share this preamble verbatim; only the
/// derivation of `is_top_level` differs, because `element_depth` has already
/// been incremented on the Start path.
fn begin_top_level_scope(
    is_top_level: bool,
    property_groups: &mut [PropertyGroupContext],
    project_close_ws: &mut Option<String>,
    fallback_group_indent: &mut Option<String>,
) -> Option<String> {
    let preceding_project_ws = if is_top_level {
        project_close_ws.take()
    } else {
        None
    };
    if is_top_level && fallback_group_indent.is_none() {
        *fallback_group_indent = Some(project_group_indent(preceding_project_ws.as_deref()));
    }
    if let Some(property_group) = property_groups.last_mut() {
        property_group.close_ws = None;
    }
    preceding_project_ws
}

fn has_condition_attribute(element: &BytesStart<'_>) -> Result<bool> {
    for attribute in element.attributes() {
        let attribute = attribute.context("Failed to parse PropertyGroup attribute")?;
        if attribute
            .key
            .local_name()
            .as_ref()
            .eq_ignore_ascii_case(b"Condition")
        {
            return Ok(true);
        }
    }
    Ok(false)
}

pub(crate) fn is_unconditional_project_property_group(
    element: &BytesStart<'_>,
    element_depth: usize,
    project_depth: Option<usize>,
) -> Result<bool> {
    Ok(
        project_depth.is_some_and(|depth| element_depth == depth + 1)
            && !has_condition_attribute(element)?,
    )
}

fn qname_with_local_name(qname: &str, local_name: &str) -> String {
    qname.rsplit_once(':').map_or_else(
        || local_name.to_owned(),
        |(prefix, _)| format!("{prefix}:{local_name}"),
    )
}

/// Emit a complete `<Version>new_version</Version>` element.
///
/// Every synthesis branch in [`rewrite_version`] writes exactly this
/// Start/Text/End triple, so the event order lives here once.
fn write_version_element<W: std::io::Write>(
    writer: &mut Writer<W>,
    version_qname: &str,
    new_version: &str,
) -> Result<()> {
    writer.write_event(Event::Start(BytesStart::new(version_qname)))?;
    writer.write_event(Event::Text(BytesText::new(new_version)))?;
    writer.write_event(Event::End(BytesEnd::new(version_qname)))?;
    Ok(())
}

/// Break the line and re-indent before the next synthesized element.
///
/// A terminator-free document has an empty `line_ending`; it must stay on one
/// line, so nothing is written at all — not even the indent.
fn write_line_break_and_indent<W: std::io::Write>(
    writer: &mut Writer<W>,
    line_ending: &str,
    indent: &str,
) -> Result<()> {
    if line_ending.is_empty() {
        return Ok(());
    }
    writer.write_event(Event::Text(BytesText::new(line_ending)))?;
    writer.write_event(Event::Text(BytesText::new(indent)))?;
    Ok(())
}

/// Break the line and indent one level deeper than `group_indent`.
///
/// The callers that open a `PropertyGroup` all need the same
/// `group_indent + indent` column for the `<Version>` they are about to write.
/// The two indent segments are emitted as two consecutive text events, which is
/// byte-identical to emitting their concatenation. The empty-`line_ending`
/// check is repeated here rather than left to [`write_line_break_and_indent`]
/// so the deeper `indent` is suppressed too for a document that must stay on
/// one line.
fn write_nested_line_break_and_indent<W: std::io::Write>(
    writer: &mut Writer<W>,
    line_ending: &str,
    group_indent: &str,
    indent: &str,
) -> Result<()> {
    if line_ending.is_empty() {
        return Ok(());
    }
    write_line_break_and_indent(writer, line_ending, group_indent)?;
    writer.write_event(Event::Text(BytesText::new(indent)))?;
    Ok(())
}

/// Insert a synthesized `<Version>` immediately before the `</PropertyGroup>`
/// that is closing now.
///
/// `close_ws` is the whitespace run that directly preceded the close tag, when
/// the group ended with one. That run already carries the group's own
/// indentation, so it is replayed verbatim after the new element (keeping
/// `</PropertyGroup>` in its original column) and the new element only needs
/// one further level of `indent` in front of it. A group that had no such run
/// was written inline, so both sides are synthesized from `line_ending` plus
/// `group_indent` instead.
fn write_version_before_property_group_end<W: std::io::Write>(
    writer: &mut Writer<W>,
    version_qname: &str,
    new_version: &str,
    close_ws: Option<&str>,
    group_indent: &str,
    line_ending: &str,
    indent: &str,
) -> Result<()> {
    if let Some(trailing) = close_ws {
        if contains_line_break(trailing) {
            writer.write_event(Event::Text(BytesText::new(indent)))?;
        }
    } else {
        write_nested_line_break_and_indent(writer, line_ending, group_indent, indent)?;
    }
    write_version_element(writer, version_qname, new_version)?;
    if let Some(trailing) = close_ws {
        writer.write_event(Event::Text(BytesText::new(trailing)))?;
    } else {
        write_line_break_and_indent(writer, line_ending, group_indent)?;
    }
    Ok(())
}

/// Synthesize a whole `<PropertyGroup><Version>...</Version></PropertyGroup>`
/// immediately before the `</Project>` that is closing now, for a document that
/// offered no eligible group to insert into.
///
/// Both new qualified names inherit `project_qname`'s prefix. `group_indent` is
/// the column the document already uses for `<Project>`'s children, and
/// `project_close_ws` is the whitespace run that directly preceded
/// `</Project>`: when it exists it is replayed verbatim after the new group, so
/// only the indent ahead of the group has to be re-emitted, and only when that
/// run actually broke the line.
fn write_property_group_before_project_end<W: std::io::Write>(
    writer: &mut Writer<W>,
    project_qname: &str,
    new_version: &str,
    project_close_ws: Option<&str>,
    group_indent: &str,
    line_ending: &str,
    indent: &str,
) -> Result<()> {
    let property_group_qname = qname_with_local_name(project_qname, "PropertyGroup");
    let version_qname = qname_with_local_name(project_qname, "Version");
    if let Some(trailing) = project_close_ws {
        if contains_line_break(trailing) {
            writer.write_event(Event::Text(BytesText::new(group_indent)))?;
        }
    } else {
        write_line_break_and_indent(writer, line_ending, group_indent)?;
    }

    writer.write_event(Event::Start(BytesStart::new(&property_group_qname)))?;
    write_nested_line_break_and_indent(writer, line_ending, group_indent, indent)?;
    write_version_element(writer, &version_qname, new_version)?;
    write_line_break_and_indent(writer, line_ending, group_indent)?;
    writer.write_event(Event::End(BytesEnd::new(&property_group_qname)))?;
    if let Some(trailing) = project_close_ws {
        writer.write_event(Event::Text(BytesText::new(trailing)))?;
    } else if !line_ending.is_empty() {
        writer.write_event(Event::Text(BytesText::new(line_ending)))?;
    }
    Ok(())
}

fn trailing_indentation(whitespace: &str) -> &str {
    let start = whitespace
        .as_bytes()
        .iter()
        .rposition(|byte| matches!(byte, b'\n' | b'\r'))
        .map_or(0, |position| position + 1);
    &whitespace[start..]
}

/// Derive the indent a synthesized top-level `<PropertyGroup>` must be written
/// at, from the whitespace run that preceded the enclosing element.
///
/// This is the single owner of the `.csproj` group-indent derivation: the
/// `Event::Start` and `Event::Empty` `PropertyGroup` branches and the
/// `fallback_group_indent` seed in [`begin_top_level_scope`] all route through
/// here, so a change to how the indent is derived cannot land on only some of
/// them. Absent whitespace (`None`) means "no indent", i.e. an empty string.
fn project_group_indent(preceding_project_ws: Option<&str>) -> String {
    preceding_project_ws
        .map_or("", trailing_indentation)
        .to_owned()
}

fn contains_line_break(whitespace: &str) -> bool {
    whitespace
        .as_bytes()
        .iter()
        .any(|byte| matches!(byte, b'\n' | b'\r'))
}

/// Render `content` with `new_version` applied and report whether any XML node
/// was actually mutated.
///
/// `has_version` claims that the document already carries an eligible
/// `<Version>` element. It gates *only* the four synthesis branches (adding a
/// `<Version>` to a `PropertyGroup`, adding a whole `PropertyGroup` to
/// `<Project>`, and the two self-closing counterparts); the branches that
/// rewrite an existing `<Version>` in place are never gated by it. A
/// `has_version = true` pass therefore flips `version_updated` exactly when an
/// eligible `<Version>` exists, which is what lets [`update_version_in_xml`]
/// use it as the eligibility probe instead of running a separate detection
/// parse over the same document.
fn rewrite_version(content: &str, new_version: &str, has_version: bool) -> Result<(String, bool)> {
    let mut reader = Reader::from_str(content);
    let mut writer = Writer::new(Cursor::new(Vec::with_capacity(content.len())));

    // Preallocate the XML event buffer to skip the first few
    // geometric-doubling reallocations on the every-`changepacks update`
    // hot path for C# projects. Mirrors the `Vec::with_capacity(...)`
    // preallocation policy already applied across `sort_by_dep.rs`,
    // `gen_update_map.rs`, `find_project_dirs.rs`, and the sibling
    // `parse_csproj_metadata` in `crates/csharp/src/finder.rs`.
    // `read_event_into` calls `buf.clear()` between events so capacity
    // persists; 256 bytes comfortably covers the largest single event
    // (attribute-laden `<Project Sdk="Microsoft.NET.Sdk"...>`) without
    // over-reserving on tiny `.csproj` files.
    let mut buf = Vec::with_capacity(256);
    let detected_indent = changepacks_utils::detect_indent_str(content);
    let indent = if detected_indent.is_empty() {
        "    "
    } else {
        detected_indent
    };
    // Probe order encodes a whole-file precedence, not a first-terminator
    // one: CRLF anywhere outranks a lone LF that appears earlier. Each
    // `str::contains` is memchr/SIMD-accelerated, so this ladder beats a
    // hand-rolled single-pass byte scan even in its worst case (a
    // terminator-free file, which pays all three probes); see
    // `test_add_new_version_prefers_crlf_when_an_lf_appears_first` for the
    // precedence guard.
    let line_ending = if content.contains("\r\n") {
        "\r\n"
    } else if content.contains('\n') {
        "\n"
    } else if content.contains('\r') {
        "\r"
    } else {
        ""
    };
    let mut property_groups: Vec<PropertyGroupContext> = Vec::new();
    let mut in_version = false;
    let mut version_updated = false;
    let mut element_depth = 0usize;
    let mut project_depth = None;
    let mut project_close_ws: Option<String> = None;
    let mut project_qname: Option<String> = None;
    let mut fallback_group_indent = None;

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(e)) => {
                element_depth += 1;
                let name = e.local_name();
                if name.as_ref() == b"Project" && project_depth.is_none() {
                    project_depth = Some(element_depth);
                    let element_name = e.name();
                    let qname = std::str::from_utf8(element_name.as_ref())
                        .context("Failed to decode Project qualified name")?;
                    // Only the raw `Project` qualified name is retained here.
                    // The derived `PropertyGroup`/`Version` names are built at
                    // the single synthesize site below, so the dominant path
                    // (a `.csproj` that already carries `<Version>`) pays no
                    // allocation for names it never writes.
                    project_qname = Some(qname.to_owned());
                }
                let is_top_level = project_depth.is_some_and(|depth| element_depth == depth + 1);
                let preceding_project_ws = begin_top_level_scope(
                    is_top_level,
                    &mut property_groups,
                    &mut project_close_ws,
                    &mut fallback_group_indent,
                );
                if name.as_ref() == b"PropertyGroup" {
                    let group_indent = project_group_indent(preceding_project_ws.as_deref());
                    let eligible =
                        is_unconditional_project_property_group(&e, element_depth, project_depth)?;
                    let version_qname = if !has_version && !version_updated && eligible {
                        let element_name = e.name();
                        let qname = std::str::from_utf8(element_name.as_ref())
                            .context("Failed to decode PropertyGroup qualified name")?;
                        Some(qname_with_local_name(qname, "Version"))
                    } else {
                        None
                    };
                    property_groups.push(PropertyGroupContext {
                        version_qname,
                        close_ws: None,
                        indent: group_indent,
                        depth: element_depth,
                        eligible,
                    });
                } else if name.as_ref() == b"Version" {
                    in_version = property_groups.last().is_some_and(|property_group| {
                        property_group.eligible && element_depth == property_group.depth + 1
                    });
                }
                writer.write_event(Event::Start(e))?;
            }
            Ok(Event::End(e)) => {
                let name = e.local_name();
                if name.as_ref() == b"PropertyGroup" {
                    if let Some(property_group) = property_groups.pop()
                        && !version_updated
                        && !has_version
                        && let Some(version_qname) = property_group.version_qname
                    {
                        write_version_before_property_group_end(
                            &mut writer,
                            &version_qname,
                            new_version,
                            property_group.close_ws.as_deref(),
                            &property_group.indent,
                            line_ending,
                            indent,
                        )?;
                        version_updated = true;
                    }
                } else if name.as_ref() == b"Version" {
                    // A content-less `<Version></Version>` produces no
                    // `Event::Text`, so `version_updated` never flips and the
                    // `</PropertyGroup>` "add missing version" branch would
                    // otherwise append a SECOND `<Version>`. Fill the empty
                    // element in place here so exactly one survives. The
                    // happy path (`<Version>X</Version>`) is untouched: its
                    // `Event::Text` already set `version_updated = true`, so
                    // this guard is false.
                    if in_version && !version_updated {
                        writer.write_event(Event::Text(BytesText::new(new_version)))?;
                        version_updated = true;
                    }
                    in_version = false;
                } else if name.as_ref() == b"Project"
                    && project_depth == Some(element_depth)
                    && !version_updated
                    && !has_version
                    && let Some(project_qname) = project_qname.as_deref()
                {
                    write_property_group_before_project_end(
                        &mut writer,
                        project_qname,
                        new_version,
                        project_close_ws.as_deref(),
                        fallback_group_indent.as_deref().unwrap_or(""),
                        line_ending,
                        indent,
                    )?;
                    version_updated = true;
                }
                writer.write_event(Event::End(e))?;
                element_depth = element_depth
                    .checked_sub(1)
                    .context("unexpected XML end tag")?;
            }
            Ok(Event::Text(e)) => {
                let is_whitespace = e
                    .decode()
                    .is_ok_and(|text| text.chars().all(char::is_whitespace));
                if in_version && !version_updated && !is_whitespace {
                    // Replace version text
                    writer.write_event(Event::Text(BytesText::new(new_version)))?;
                    version_updated = true;
                } else {
                    if let Some(property_group) = property_groups.last_mut() {
                        let decoded = e.decode().context("Failed to decode XML text")?;
                        property_group.close_ws = decoded
                            .chars()
                            .all(char::is_whitespace)
                            .then(|| decoded.into_owned());
                    } else if project_depth == Some(element_depth) {
                        let decoded = e.decode().context("Failed to decode XML text")?;
                        project_close_ws = decoded
                            .chars()
                            .all(char::is_whitespace)
                            .then(|| decoded.into_owned());
                    }
                    writer.write_event(Event::Text(e))?;
                }
            }
            Ok(Event::CData(e)) => {
                clear_scope_close_ws(
                    &mut property_groups,
                    &mut project_close_ws,
                    project_depth,
                    element_depth,
                );
                if in_version && !version_updated {
                    writer.write_event(Event::CData(BytesCData::new(new_version)))?;
                    version_updated = true;
                } else {
                    writer.write_event(Event::CData(e))?;
                }
            }
            Ok(Event::Empty(e)) => {
                let name = e.local_name();
                let is_top_level = project_depth == Some(element_depth);
                let preceding_project_ws = begin_top_level_scope(
                    is_top_level,
                    &mut property_groups,
                    &mut project_close_ws,
                    &mut fallback_group_indent,
                );
                if name.as_ref() == b"Project"
                    && project_depth.is_none()
                    && !version_updated
                    && !has_version
                {
                    let start = e.into_owned();
                    let end = BytesEnd::from(start.name()).into_owned();
                    let element_name = start.name();
                    let project_qname = std::str::from_utf8(element_name.as_ref())
                        .context("Failed to decode Project qualified name")?;
                    let property_group_qname =
                        qname_with_local_name(project_qname, "PropertyGroup");
                    let version_qname = qname_with_local_name(project_qname, "Version");
                    let inner_indent = format!("{indent}{indent}");

                    writer.write_event(Event::Start(start))?;
                    write_line_break_and_indent(&mut writer, line_ending, indent)?;
                    writer.write_event(Event::Start(BytesStart::new(&property_group_qname)))?;
                    write_line_break_and_indent(&mut writer, line_ending, &inner_indent)?;
                    write_version_element(&mut writer, &version_qname, new_version)?;
                    write_line_break_and_indent(&mut writer, line_ending, indent)?;
                    writer.write_event(Event::End(BytesEnd::new(&property_group_qname)))?;
                    if !line_ending.is_empty() {
                        writer.write_event(Event::Text(BytesText::new(line_ending)))?;
                    }
                    writer.write_event(Event::End(end))?;
                    version_updated = true;
                } else if name.as_ref() == b"PropertyGroup" && !version_updated && !has_version {
                    let qname = std::str::from_utf8(e.name().as_ref())
                        .context("Failed to decode PropertyGroup qualified name")?
                        .to_owned();
                    let version_qname = qname_with_local_name(&qname, "Version");
                    let group_indent = project_group_indent(preceding_project_ws.as_deref());
                    let is_unconditional_top_level = is_unconditional_project_property_group(
                        &e,
                        element_depth + 1,
                        project_depth,
                    )?;
                    if is_unconditional_top_level {
                        let start = e.into_owned();
                        let end = BytesEnd::from(start.name()).into_owned();
                        writer.write_event(Event::Start(start))?;
                        write_nested_line_break_and_indent(
                            &mut writer,
                            line_ending,
                            &group_indent,
                            indent,
                        )?;
                        write_version_element(&mut writer, &version_qname, new_version)?;
                        write_line_break_and_indent(&mut writer, line_ending, &group_indent)?;
                        writer.write_event(Event::End(end))?;
                        version_updated = true;
                    } else {
                        writer.write_event(Event::Empty(e))?;
                    }
                // A self-closing `<Version/>` carries no `Event::Text`, so
                // `version_updated` never flips and the `</PropertyGroup>`
                // "add missing version" branch would otherwise append a
                // SECOND `<Version>`. Expand the self-closing shape into a
                // filled `<Version>X</Version>` in place here so exactly one
                // survives — the sibling of the `<Version></Version>`
                // (Start/End) fill-in-place above. Every other empty element
                // (including `<Version/>` outside a PropertyGroup, or once the
                // version is already updated) passes through unchanged.
                } else if name.as_ref() == b"Version"
                    && !version_updated
                    && property_groups.last().is_some_and(|property_group| {
                        property_group.eligible && element_depth == property_group.depth
                    })
                {
                    let start = e.into_owned();
                    let end = BytesEnd::from(start.name()).into_owned();
                    writer.write_event(Event::Start(start))?;
                    writer.write_event(Event::Text(BytesText::new(new_version)))?;
                    writer.write_event(Event::End(end))?;
                    version_updated = true;
                } else {
                    writer.write_event(Event::Empty(e))?;
                }
            }
            Ok(Event::Eof) => break,
            Err(e) => return Err(anyhow::anyhow!("XML parsing error: {e}")),
            // Pass-through arms for every event that carries no state and
            // does not need in-place rewriting: Comment, Decl, PI,
            // DocType, GeneralRef. Any future variant with no customization
            // requirement falls into this arm automatically; a variant that
            // DOES need customization must be added above
            // (Start / End / Text / Empty) before this wildcard.
            Ok(event) => {
                clear_scope_close_ws(
                    &mut property_groups,
                    &mut project_close_ws,
                    project_depth,
                    element_depth,
                );
                writer.write_event(event)?;
            }
        }
        buf.clear();
    }

    let result = writer.into_inner().into_inner();
    let rendered = String::from_utf8(result).context("Failed to convert XML to UTF-8")?;
    Ok((rendered, version_updated))
}

/// Update version in csproj XML content using quick-xml
/// Returns the updated XML content or adds Version if it doesn't exist.
/// Errors when no supported XML node can be updated.
pub fn update_version_in_xml(content: &str, new_version: &str) -> Result<String> {
    // First pass doubles as the eligibility probe: with `has_version = true`
    // every synthesis branch is disabled, so the pass mutates a node exactly
    // when the document already holds an eligible `<Version>` — and when it
    // does, its output is already the final answer. Only a document that needs
    // a `<Version>` synthesized pays a second parse.
    let (rendered, version_updated) = rewrite_version(content, new_version, true)?;
    if version_updated {
        return Ok(rendered);
    }

    let (rendered, version_updated) = rewrite_version(content, new_version, false)?;
    if version_updated {
        return Ok(rendered);
    }

    Err(anyhow::anyhow!(
        "C# version update did not mutate any XML node"
    ))
}

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

    #[test]
    fn test_update_version_in_xml() {
        let content = r#"<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>
</Project>"#;

        let result = update_version_in_xml(content, "2.0.0").unwrap();
        assert!(result.contains("<Version>2.0.0</Version>"));
    }

    #[test]
    fn test_update_version_replaces_cdata_payload_in_place() {
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version><![CDATA[1.2.3]]></Version>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version><![CDATA[2.0.0]]></Version>\n  </PropertyGroup>\n</Project>"
        );
        assert_eq!(result.matches("<![CDATA[").count(), 1);
        assert!(!result.contains("1.2.3"));
        assert!(!result.contains("1.2.32.0.0"));
    }

    #[test]
    fn test_update_version_preserves_whitespace_around_cdata() {
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version>\n      <![CDATA[1.2.3]]>\n    </Version>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version>\n      <![CDATA[2.0.0]]>\n    </Version>\n  </PropertyGroup>\n</Project>"
        );
    }

    #[test]
    fn test_update_version_in_xml_without_existing_version() {
        let content = r#"<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
</Project>"#;

        let result = update_version_in_xml(content, "0.0.1").unwrap();
        assert!(result.contains("<Version>0.0.1</Version>"));
    }

    #[test]
    fn test_add_new_version_uses_first_unconditional_top_level_property_group() {
        let content = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <Optimize>false</Optimize>\n  </PropertyGroup>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n  </PropertyGroup>\n</Project>";
        let expected = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <Optimize>false</Optimize>\n  </PropertyGroup>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_uses_self_closing_unconditional_top_level_property_group() {
        let content = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\"/>\n  <PropertyGroup/>\n</Project>";
        let expected = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\"/>\n  <PropertyGroup>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_ignores_nested_property_group() {
        let content = "<Project>\n  <Target Name=\"Build\">\n    <PropertyGroup>\n      <NestedOnly>true</NestedOnly>\n    </PropertyGroup>\n  </Target>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n  </PropertyGroup>\n</Project>";
        let expected = "<Project>\n  <Target Name=\"Build\">\n    <PropertyGroup>\n      <NestedOnly>true</NestedOnly>\n    </PropertyGroup>\n  </Target>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_creates_unconditional_group_when_all_groups_are_conditional() {
        let content = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <Optimize>false</Optimize>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Release'\">\n    <Optimize>true</Optimize>\n  </PropertyGroup>\n</Project>";
        let expected = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <Optimize>false</Optimize>\n  </PropertyGroup>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Release'\">\n    <Optimize>true</Optimize>\n  </PropertyGroup>\n  <PropertyGroup>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_preserves_namespaced_qnames() {
        let content = "<msb:Project xmlns:msb=\"urn:msbuild\">\n  <msb:PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <msb:Optimize>false</msb:Optimize>\n  </msb:PropertyGroup>\n</msb:Project>";
        let expected = "<msb:Project xmlns:msb=\"urn:msbuild\">\n  <msb:PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n    <msb:Optimize>false</msb:Optimize>\n  </msb:PropertyGroup>\n  <msb:PropertyGroup>\n    <msb:Version>2.0.0</msb:Version>\n  </msb:PropertyGroup>\n</msb:Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_preserves_crlf_and_space_indentation() {
        let content = "<Project>\r\n    <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\r\n        <Optimize>false</Optimize>\r\n    </PropertyGroup>\r\n</Project>\r\n";
        let expected = "<Project>\r\n    <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\r\n        <Optimize>false</Optimize>\r\n    </PropertyGroup>\r\n    <PropertyGroup>\r\n        <Version>2.0.0</Version>\r\n    </PropertyGroup>\r\n</Project>\r\n";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_add_new_version_preserves_tab_indentation() {
        let content = "<Project>\n\t<PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n\t\t<Optimize>false</Optimize>\n\t</PropertyGroup>\n</Project>";
        let expected = "<Project>\n\t<PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\">\n\t\t<Optimize>false</Optimize>\n\t</PropertyGroup>\n\t<PropertyGroup>\n\t\t<Version>2.0.0</Version>\n\t</PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_conditional_version_is_unchanged_when_global_version_is_missing() {
        let content = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Release'\">\n    <Version>1.2.3</Version>\n  </PropertyGroup>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n  </PropertyGroup>\n</Project>";
        let expected = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Release'\">\n    <Version>1.2.3</Version>\n  </PropertyGroup>\n  <PropertyGroup>\n    <TargetFramework>net8.0</TargetFramework>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>";

        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(result, expected);
    }

    #[test]
    fn test_update_version_fills_content_less_version_element() {
        // Regression: a content-less `<Version></Version>` element used to
        // yield TWO `<Version>` elements — the empty original plus an
        // appended one from the "add missing version" branch — because no
        // `Event::Text` fired to set `version_updated`. It must instead be
        // filled in place, leaving exactly one `<Version>`.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version></Version>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "0.0.1").unwrap();
        assert_eq!(
            result.matches("<Version>").count(),
            1,
            "expected exactly one <Version> element, got:\n{result}",
        );
        assert!(result.contains("<Version>0.0.1</Version>"));
    }

    #[test]
    fn test_update_version_fills_self_closing_version_element() {
        // Regression: a self-closing `<Version/>` used to yield TWO
        // `<Version>` elements — the empty original passed through the
        // wildcard arm plus an appended one from the "add missing version"
        // branch — because no `Event::Text` fired to set `version_updated`.
        // It must instead be expanded in place, leaving exactly one
        // `<Version>` with the surrounding indentation untouched. Sibling of
        // the `<Version></Version>` case above.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version/>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();
        // Full-string compare: sibling formatting/indentation preserved.
        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>",
            "self-closing <Version/> should be filled in place, formatting preserved:\n{result}",
        );
        assert_eq!(
            result.matches("<Version>2.0.0</Version>").count(),
            1,
            "expected exactly one filled <Version> element, got:\n{result}",
        );
        assert!(
            !result.contains("<Version/>"),
            "self-closing <Version/> should not survive:\n{result}",
        );
        // `<Version` matches only opening/self-closing tags (`</Version>`
        // contains `</Version`, not `<Version`), so this counts Version
        // openings: exactly one.
        assert_eq!(
            result.matches("<Version").count(),
            1,
            "expected exactly one <Version opening, got:\n{result}",
        );
    }

    #[test]
    fn test_update_version_fills_self_closing_version_in_place_after_sibling() {
        // The filled `<Version>` must stay in the self-closing element's
        // original position (right after its sibling property), NOT be
        // appended at the end of the PropertyGroup.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <OutputType>Exe</OutputType>\n    <Version/>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();
        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <OutputType>Exe</OutputType>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>",
            "filled <Version> should replace <Version/> in place, not append:\n{result}",
        );
        assert_eq!(
            result.matches("<Version").count(),
            1,
            "expected exactly one <Version opening, got:\n{result}",
        );
    }

    #[test]
    fn test_update_version_preserves_prefixed_self_closing_version_qname() {
        let content = "<msb:Project xmlns:msb=\"urn:msbuild\">\n  <msb:PropertyGroup>\n    <msb:Version/>\n  </msb:PropertyGroup>\n</msb:Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(
            result,
            "<msb:Project xmlns:msb=\"urn:msbuild\">\n  <msb:PropertyGroup>\n    <msb:Version>2.0.0</msb:Version>\n  </msb:PropertyGroup>\n</msb:Project>"
        );
    }

    #[test]
    fn test_update_version_preserves_attributes_on_filled_version_element() {
        // A `<Version>` written as Start/Text/End while carrying attributes is
        // the shape where a naive rewrite (re-synthesizing the tag from its
        // local name, the way the two "add missing version" branches do)
        // would silently DROP the attributes. The Start arm must instead pass
        // the original element through verbatim and only the Text arm may
        // change, so the `Condition` attribute survives byte-for-byte.
        // Sibling of the self-closing attribute case below.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <OutputType>Exe</OutputType>\n    <Version Condition=\"'$(Configuration)' == 'Release'\">1.0.0</Version>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <OutputType>Exe</OutputType>\n    <Version Condition=\"'$(Configuration)' == 'Release'\">2.0.0</Version>\n  </PropertyGroup>\n</Project>",
            "attribute-bearing <Version> should keep its attributes and only swap its text:\n{result}",
        );
        assert!(
            result.contains("Condition=\"'$(Configuration)' == 'Release'\""),
            "Condition attribute must survive byte-for-byte:\n{result}",
        );
        // `<Version` matches only opening tags (`</Version>` contains
        // `</Version`, not `<Version`), so exactly one opening proves the
        // `</PropertyGroup>` insert branch did not also fire and append a
        // second, attribute-less `<Version>`.
        assert_eq!(
            result.matches("<Version").count(),
            1,
            "expected exactly one <Version opening, got:\n{result}",
        );
        assert!(!result.contains("1.0.0"), "old version survived:\n{result}");
    }

    #[test]
    fn test_update_version_preserves_attributes_and_spacing_on_self_closing_version() {
        let content = "<Project>\n  <PropertyGroup>\n    <Version Condition=\"'$(Configuration)' == 'Release'\" />\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();

        assert_eq!(
            result,
            "<Project>\n  <PropertyGroup>\n    <Version Condition=\"'$(Configuration)' == 'Release'\" >2.0.0</Version>\n  </PropertyGroup>\n</Project>"
        );
    }

    #[test]
    fn test_update_version_passes_through_non_version_self_closing_element() {
        // A self-closing element that is NOT `<Version>` must fall through
        // the Empty arm's `else` and be emitted unchanged, while the real
        // `<Version>` is still updated.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version>1.0.0</Version>\n    <Nullable/>\n  </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "2.0.0").unwrap();
        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n  <PropertyGroup>\n    <Version>2.0.0</Version>\n    <Nullable/>\n  </PropertyGroup>\n</Project>",
            "non-Version self-closing element should pass through unchanged:\n{result}",
        );
        assert!(
            result.contains("<Nullable/>"),
            "self-closing <Nullable/> should survive unchanged:\n{result}",
        );
    }

    // Fixtures for `test_update_version_preserves_feature` — one per XML
    // feature we must not drop when rewriting the version. Named consts keep
    // each rstest `#[case]` line short and self-describing.

    const XML_WITH_EMPTY_ELEMENT: &str = r#"<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
    <IsPackable />
  </PropertyGroup>
</Project>"#;

    const XML_WITH_COMMENT: &str = r#"<Project Sdk="Microsoft.NET.Sdk">
  <!-- This is a comment -->
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>
</Project>"#;

    const XML_WITH_CDATA: &str = r#"<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
    <Description><![CDATA[some data]]></Description>
  </PropertyGroup>
</Project>"#;

    const XML_WITH_XML_DECLARATION: &str = r#"<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>
</Project>"#;

    const XML_WITH_PROCESSING_INSTRUCTION: &str = r#"<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>
</Project>"#;

    const XML_WITH_DOCTYPE: &str = r#"<!DOCTYPE Project>
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <Version>1.0.0</Version>
  </PropertyGroup>
</Project>"#;

    // Each XML feature — empty (self-closing) element, comment, CDATA, XML
    // declaration, processing instruction, DOCTYPE — must survive the
    // version rewrite. `marker` is a substring uniquely tied to the
    // feature under test.
    #[rstest]
    #[case(XML_WITH_EMPTY_ELEMENT, "IsPackable")]
    #[case(XML_WITH_COMMENT, "<!-- This is a comment -->")]
    #[case(XML_WITH_CDATA, "CDATA")]
    #[case(XML_WITH_XML_DECLARATION, "<?xml")]
    #[case(XML_WITH_PROCESSING_INSTRUCTION, "xml-stylesheet")]
    #[case(XML_WITH_DOCTYPE, "DOCTYPE")]
    fn test_update_version_preserves_feature(#[case] content: &str, #[case] marker: &str) {
        let result = update_version_in_xml(content, "2.0.0").unwrap();
        assert!(result.contains("2.0.0"));
        assert!(result.contains(marker));
    }

    #[test]
    fn test_update_version_malformed_xml() {
        let content = r"<Project><PropertyGroup><Version>1.0.0</Version></PropertyGroup";
        let result = update_version_in_xml(content, "2.0.0");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("XML parsing error")
        );
    }

    #[test]
    fn test_property_group_malformed_attribute_returns_contextual_error() {
        // A `PropertyGroup` start tag carrying a valueless attribute makes
        // quick-xml's attribute iterator yield `Err`. That failure must
        // surface as the contextual `Failed to parse PropertyGroup
        // attribute` error rather than a bare `AttrError`, mirroring the
        // `ProjectReference` counterpart pinned in `finder.rs`.
        let content =
            r"<Project><PropertyGroup Broken><Version>1.0.0</Version></PropertyGroup></Project>";

        let error = update_version_in_xml(content, "2.0.0").unwrap_err();

        assert!(
            format!("{error:#}").contains("Failed to parse PropertyGroup attribute"),
            "unexpected error: {error:#}"
        );
    }

    #[test]
    fn test_update_version_preserves_general_ref() {
        // XML with entity references like &custom; triggers Event::GeneralRef in quick-xml,
        // exercising the GeneralRef handler (lines 78-79)
        let content = r"<Project><PropertyGroup><Description>Hello &custom; World</Description><Version>1.0.0</Version></PropertyGroup></Project>";
        let result = update_version_in_xml(content, "2.0.0");
        if let Ok(output) = result {
            assert!(output.contains("2.0.0"));
        }
    }

    #[test]
    fn test_add_new_version_reindent_matches_4_space_indent() {
        // Regression: the "no existing <Version>" branch used to hardcode
        // `"\n  "` for the trailing reindent, so 4-space (and tab) .csproj
        // files ended up with mixed-indent output. The Version line's
        // trailing whitespace MUST match the detected indent, not a
        // hardcoded 2-space value.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n    <PropertyGroup>\n        <OutputType>Exe</OutputType>\n    </PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "0.0.1").unwrap();
        // The trailing reindent (`"\n    "` for 4-space files) must not
        // regress to `"\n  "`.
        assert!(
            !result.contains("</Version>\n  </"),
            "found hardcoded 2-space reindent in 4-space .csproj output:\n{result}",
        );
        // Positive assertion: the 4-space reindent is what we expect.
        assert!(
            result.contains("</Version>\n    </PropertyGroup>"),
            "expected 4-space reindent before </PropertyGroup>:\n{result}",
        );
    }

    #[test]
    fn test_add_new_version_reindent_matches_tab_indent() {
        // Same regression but for tab-indented .csproj files.
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n\t<PropertyGroup>\n\t\t<OutputType>Exe</OutputType>\n\t</PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "0.0.1").unwrap();
        assert!(
            result.contains("</Version>\n\t</PropertyGroup>"),
            "expected tab reindent before </PropertyGroup>:\n{result}",
        );
    }

    #[test]
    fn test_add_new_version_preserves_zero_indent_property_group_close() {
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n<PropertyGroup>\n    <OutputType>Exe</OutputType>\n</PropertyGroup>\n</Project>";
        let result = update_version_in_xml(content, "0.0.1").unwrap();
        assert!(
            result.contains("</Version>\n</PropertyGroup>"),
            "expected zero-indent close tag to stay at column 0:\n{result}",
        );
    }

    #[test]
    fn test_update_version_without_property_group_creates_unconditional_group() {
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\">\n</Project>\n";
        let result = update_version_in_xml(content, "0.0.1").unwrap();

        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\">\n<PropertyGroup>\n    <Version>0.0.1</Version>\n</PropertyGroup>\n</Project>\n"
        );
    }

    #[test]
    fn test_self_closing_project_expands_with_crlf_and_trailing_bytes() -> Result<()> {
        let content = "<Project Sdk=\"Microsoft.NET.Sdk\" />\r\n<!-- trailing -->\r\n";

        let result = update_version_in_xml(content, "1.2.3")?;

        assert_eq!(
            result,
            "<Project Sdk=\"Microsoft.NET.Sdk\" >\r\n    <PropertyGroup>\r\n        <Version>1.2.3</Version>\r\n    </PropertyGroup>\r\n</Project>\r\n<!-- trailing -->\r\n"
        );
        Ok(())
    }

    #[test]
    fn test_namespaced_self_closing_project_preserves_qualified_names() -> Result<()> {
        let content = "<msb:Project xmlns:msb=\"urn:msbuild\"/>\n";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<msb:Project xmlns:msb=\"urn:msbuild\">\n    <msb:PropertyGroup>\n        <msb:Version>2.0.0</msb:Version>\n    </msb:PropertyGroup>\n</msb:Project>\n"
        );
        Ok(())
    }

    #[test]
    fn test_add_new_version_uses_bare_carriage_return_line_ending() -> Result<()> {
        // Classic-Mac `.csproj`: the only terminator is a bare `\r`, so the
        // `line_ending` ladder must resolve to `"\r"` and NEVER synthesize an
        // LF. `<PropertyGroup>` closes with no preceding whitespace text, so
        // `close_ws` stays `None` and the insertion genuinely goes through the
        // `line_ending` branch instead of reusing captured trailing
        // whitespace. `detect_indent_str` splits on CR as well as LF, so this
        // CR-only file reports its real 2-space indent and the `"    "`
        // default does NOT apply: the synthesized inner indent is the
        // 2-space `<PropertyGroup>` indent plus the 2-space unit.
        let content =
            "<Project>\r  <PropertyGroup><OutputType>Exe</OutputType></PropertyGroup>\r</Project>";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<Project>\r  <PropertyGroup><OutputType>Exe</OutputType>\r    <Version>2.0.0</Version>\r  </PropertyGroup>\r</Project>"
        );
        assert!(
            !result.contains('\n'),
            "no line feed may be introduced into a CR-only file:\n{result:?}",
        );
        Ok(())
    }

    #[test]
    fn test_add_new_version_prefers_crlf_when_an_lf_appears_first() -> Result<()> {
        // Mixed-terminator `.csproj`: a lone LF precedes the first CRLF. The
        // contract is "CRLF anywhere wins", NOT "first terminator wins", so
        // the synthesized `<Version>` must be joined with `"\r\n"`. This pins
        // the whole-file precedence that a first-terminator scan would break,
        // silently rewriting the bytes of users' project files.
        let content = "<Project>\n  <PropertyGroup><OutputType>Exe</OutputType></PropertyGroup>\r\n</Project>\r\n";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<Project>\n  <PropertyGroup><OutputType>Exe</OutputType>\r\n    <Version>2.0.0</Version>\r\n  </PropertyGroup>\r\n</Project>\r\n"
        );
        Ok(())
    }

    #[test]
    fn test_add_new_version_omits_line_ending_for_terminator_free_content() -> Result<()> {
        // A single-line `.csproj` with no terminator at all resolves the
        // `line_ending` ladder to `""`; every `!line_ending.is_empty()` guard
        // must then stay silent so the output remains a single line rather
        // than gaining an invented terminator.
        let content = "<Project/>";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<Project><PropertyGroup><Version>2.0.0</Version></PropertyGroup></Project>"
        );
        Ok(())
    }

    #[test]
    fn test_add_new_version_into_property_group_keeps_terminator_free_content_on_one_line()
    -> Result<()> {
        // Sibling of the `<Project/>` case above, but through the OTHER
        // synthesis branch: an existing `<PropertyGroup>` gains a `<Version>`
        // before its close tag. The group closes with no preceding whitespace
        // run, so `close_ws` is `None` and both re-indent helpers
        // (`write_nested_line_break_and_indent` for the element and
        // `write_line_break_and_indent` for the close tag) are asked to break
        // the line — with an empty `line_ending` both must stay silent, or a
        // single-line `.csproj` would gain an invented terminator AND a stray
        // indent run.
        let content =
            "<Project><PropertyGroup><OutputType>Exe</OutputType></PropertyGroup></Project>";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<Project><PropertyGroup><OutputType>Exe</OutputType><Version>2.0.0</Version></PropertyGroup></Project>"
        );
        assert!(
            !result.contains('\n') && !result.contains('\r'),
            "no terminator may be introduced into a single-line file:\n{result:?}",
        );
        Ok(())
    }

    #[test]
    fn test_add_new_version_terminates_synthesized_group_when_project_close_has_no_whitespace()
    -> Result<()> {
        // The only top-level group is conditional, so a whole
        // `<PropertyGroup>` is synthesized before `</Project>`. `</Project>`
        // follows `</PropertyGroup>` immediately, so no whitespace run was
        // captured for it and the synthesized block must supply BOTH sides
        // itself: the leading break/indent from `line_ending`, and — the
        // branch pinned here — a trailing `line_ending` so `</Project>` still
        // starts on its own line instead of being glued onto
        // `</PropertyGroup>`.
        let content = "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\"><Optimize>false</Optimize></PropertyGroup></Project>\n";

        let result = update_version_in_xml(content, "2.0.0")?;

        assert_eq!(
            result,
            "<Project>\n  <PropertyGroup Condition=\"'$(Configuration)' == 'Debug'\"><Optimize>false</Optimize></PropertyGroup>\n  <PropertyGroup>\n    <Version>2.0.0</Version>\n  </PropertyGroup>\n</Project>\n"
        );
        assert!(
            !result.contains("</PropertyGroup></Project>"),
            "the synthesized group must not be glued onto </Project>:\n{result}",
        );
        Ok(())
    }

    #[test]
    fn test_begin_top_level_scope_seeds_fallback_group_indent_for_the_first_top_level_element() {
        // Opening the FIRST element directly under `<Project>` both consumes
        // the pending `</Project>` whitespace and seeds `fallback_group_indent`
        // from its trailing indentation — the column a synthesized top-level
        // `<PropertyGroup>` is later written at. Seeding here (rather than only
        // in the `PropertyGroup` branches) is what makes the fallback available
        // even for a document whose first top-level element is something else.
        let mut project_close_ws = Some("\n    ".to_string());
        let mut fallback_group_indent = None;

        let preceding = begin_top_level_scope(
            true,
            &mut [],
            &mut project_close_ws,
            &mut fallback_group_indent,
        );

        assert_eq!(preceding.as_deref(), Some("\n    "));
        assert!(
            project_close_ws.is_none(),
            "the whitespace belongs to the opened element, not to </Project>"
        );
        assert_eq!(fallback_group_indent.as_deref(), Some("    "));
    }

    #[test]
    fn test_update_version_without_project_element_reports_no_mutated_node() {
        // Neither pass can touch a document that carries no `<Project>`: the
        // `<PropertyGroup>` is not project-scoped, so it is never eligible,
        // its `<Version>` is never rewritten in place, and no synthesis branch
        // has a `</Project>` to insert before. That must surface as an error
        // rather than silently returning the input unchanged, which would make
        // `changepacks update` report a successful bump it never performed.
        let content = "<Solution>\n  <PropertyGroup>\n    <Version>1.0.0</Version>\n  </PropertyGroup>\n</Solution>\n";

        let error = update_version_in_xml(content, "2.0.0").unwrap_err();

        assert!(
            format!("{error:#}").contains("C# version update did not mutate any XML node"),
            "unexpected error: {error:#}"
        );
    }
}