unity-solution-generator 0.1.1

Regenerates Unity .csproj/.sln files from asmdef/asmref layout without launching the Unity editor.
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
//! End-to-end scenario tests covering: idempotency, multi-variant generation,
//! native-plugin filtering, asmdef versionDefines manifest filtering,
//! lock fingerprint short-circuit, and duplicate-asmdef error reporting.

mod common;

use std::collections::BTreeMap;
use std::path::Path;

use common::{make_temp_root, read_compile_set, read_file, write_file};
use unity_solution_generator::{
    BuildConfig, BuildPlatform, DllRef, GenerateOptions, GeneratorError, Lockfile, LockfileIO,
    ProjectScanner, RefCategory, SolutionGenerator,
};

const GR: &str = "tpl";

fn opts(root: &Path, platform: BuildPlatform, cfg: BuildConfig) -> GenerateOptions {
    GenerateOptions::new(root.to_string_lossy().into_owned(), platform)
        .with_generator_root(GR)
        .with_build_config(cfg)
}

fn small_lockfile() -> Lockfile {
    let mut refs: BTreeMap<RefCategory, Vec<DllRef>> = BTreeMap::new();
    refs.insert(
        RefCategory::Engine,
        vec![DllRef::new(
            "UnityEngine",
            "$(UnityPath)/Unity.app/Contents/Managed/UnityEngine/UnityEngine.dll",
        )],
    );
    refs.insert(RefCategory::Editor, vec![]);
    refs.insert(RefCategory::Netstandard, vec![]);
    refs.insert(RefCategory::PlaybackIos, vec![]);
    refs.insert(RefCategory::PlaybackAndroid, vec![]);
    refs.insert(RefCategory::PlaybackStandalone, vec![]);
    refs.insert(RefCategory::Project, vec![]);
    Lockfile {
        unity_version: "6000.2.7f2".into(),
        unity_path: "/test/unity".into(),
        lang_version: "9.0".into(),
        analyzers: vec![],
        refs,
        defines: vec!["UNITY_6000".into()],
        defines_scripting: vec![],
    }
}

#[test]
fn regeneration_is_byte_identical() {
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let csproj1 = read_file(root, "tpl/ios-editor/Lib.csproj");
    let sln1 = read_file(root, &format!("tpl/ios-editor/{}.sln", root.file_name().unwrap().to_string_lossy()));
    let props1 = read_file(root, "tpl/ios-editor/Directory.Build.props");

    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let csproj2 = read_file(root, "tpl/ios-editor/Lib.csproj");
    let sln2 = read_file(root, &format!("tpl/ios-editor/{}.sln", root.file_name().unwrap().to_string_lossy()));
    let props2 = read_file(root, "tpl/ios-editor/Directory.Build.props");

    assert_eq!(csproj1, csproj2);
    assert_eq!(sln1, sln2);
    assert_eq!(props1, props2);
}

#[test]
fn multi_variant_from_same_lockfile_share_scan_cache() {
    // First variant warms the scan-cache; subsequent variants must read it,
    // not re-walk. We can't directly assert "didn't walk" without instrumentation,
    // but we *can* assert the variants produce expected output and don't error.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    let g = SolutionGenerator::new();
    for (p, c) in [
        (BuildPlatform::Ios, BuildConfig::Editor),
        (BuildPlatform::Ios, BuildConfig::Prod),
        (BuildPlatform::Android, BuildConfig::Editor),
        (BuildPlatform::Osx, BuildConfig::Editor),
    ] {
        let r = g
            .generate_from_lockfile(&opts(root, p, c), &lf)
            .unwrap();
        assert!(r.variant_csprojs.iter().any(|s| s.ends_with("Lib.csproj")));
    }
    // Sanity-check the on-disk variants exist
    for v in ["ios-editor", "ios-prod", "android-editor", "osx-editor"] {
        assert!(
            root.join("tpl").join(v).join("Lib.csproj").exists(),
            "variant {} missing",
            v
        );
    }
}

#[test]
fn native_plugin_dirs_skipped() {
    // ProjectScanner skips native-plugin dir names like x86_64, ARM64, .framework, .bundle.
    // (Note: applies to .dll/.asmdef walk inside lockfile_scanner, not the .cs walk —
    // this test confirms the ProjectScanner-side .cs walk *does* still see the leaves
    // because cs files aren't normally in native-plugin dirs anyway.)
    // What we verify here: a `Foo.framework` directory inside Assets does NOT get its
    // .cs files included if scanned by lockfile_scanner. We simulate by inspecting
    // ProjectScanner output, which uses ignore::WalkBuilder without the native-plugin filter
    // — so .cs files in those dirs DO get included. The native-plugin filter only applies
    // to the lockfile-scanner DLL/asmdef walk. We still smoke-test the path.
    //
    // Concrete check: place a .asmdef inside x86_64/ and confirm it isn't picked up
    // when LockfileScanner runs a project walk (we can't run the full LockfileScanner
    // without a real Unity install, so we verify the helper directly via behavior:
    // create a fake non-Unity project and call ProjectScanner — which doesn't apply
    // the native-plugin filter — to confirm the filter is *only* in the lockfile path).
    let tmp = make_temp_root();
    let root = tmp.path();
    write_file(root, "Assets/Plugin/x86_64/Code.cs", "class Native {}\n");
    write_file(root, "Assets/Plugin/Code.cs", "class Plain {}\n");
    // ProjectScanner does NOT apply native-plugin filtering — both files should be picked up.
    let scan = ProjectScanner::scan(root.to_str().unwrap(), GR).unwrap();
    let total_dirs: usize = scan.dirs_by_project.values().map(|v| v.len()).sum::<usize>()
        + scan.unresolved_dirs.len();
    assert!(
        total_dirs >= 2,
        "expected at least 2 cs dirs (x86_64 + parent), got {}",
        total_dirs
    );
}

#[test]
fn asmdef_version_defines_filtered_by_manifest() {
    let tmp = make_temp_root();
    let root = tmp.path();
    // manifest.json declares only physics2d
    write_file(
        root,
        "Packages/manifest.json",
        r#"{"dependencies":{"com.unity.modules.physics2d":"1.0.0"}}"#,
    );
    write_file(
        root,
        "Assets/A/Lib.asmdef",
        r#"{"name":"Lib","versionDefines":[
            {"name":"com.unity.modules.physics2d","expression":"","define":"HAS_PHYSICS"},
            {"name":"com.unity.modules.audio","expression":"","define":"HAS_AUDIO"},
            {"name":"Unity","expression":"","define":"HAS_UNITY"}
        ]}"#,
    );
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    // Use the asmdef-version-defines collector via ProjectScanner + the public API
    // by inspecting the scan; the `defines` collection itself is owned by LockfileScanner
    // which requires a real Unity install. So we directly verify ProjectScanner ingests
    // versionDefines into the AsmDefRecord, and the manifest-filtering happens in the
    // lockfile path. Here we verify the AsmDefRecord captures all three.
    let scan = ProjectScanner::scan(root.to_str().unwrap(), GR).unwrap();
    let asm = scan.asm_def_by_name.get("Lib").unwrap();
    assert_eq!(asm.version_defines.len(), 3);
    let names: std::collections::HashSet<_> =
        asm.version_defines.iter().map(|v| v.define.as_str()).collect();
    assert!(names.contains("HAS_PHYSICS"));
    assert!(names.contains("HAS_AUDIO"));
    assert!(names.contains("HAS_UNITY"));
}

#[test]
fn duplicate_asmdef_name_errors() {
    let tmp = make_temp_root();
    let root = tmp.path();
    write_file(root, "Assets/A/Foo.asmdef", r#"{"name":"Foo"}"#);
    write_file(root, "Assets/B/Foo.asmdef", r#"{"name":"Foo"}"#);
    write_file(root, "Assets/A/X.cs", "class X {}\n");
    write_file(root, "Assets/B/Y.cs", "class Y {}\n");

    let err = ProjectScanner::scan(root.to_str().unwrap(), GR).unwrap_err();
    match err {
        GeneratorError::DuplicateAsmDefName(n) => assert_eq!(n, "Foo"),
        other => panic!("expected DuplicateAsmDefName, got {:?}", other),
    }
}

#[test]
fn lockfile_round_trip_preserves_unicode() {
    let tmp = make_temp_root();
    let root = tmp.path();
    let path = root.join("csproj.lock");
    let mut lf = small_lockfile();
    lf.defines.push("FÜRBALL_测试".to_string());
    LockfileIO::write(&lf, path.to_str().unwrap()).unwrap();
    let r = LockfileIO::read(path.to_str().unwrap()).unwrap();
    assert_eq!(r.defines, lf.defines);
}

#[test]
fn extra_refs_ordering_preserves_lockfile_first() {
    // The Swift impl: lockfile refs come first, extra refs append; dedupe is "lockfile wins".
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Main.asmdef", r#"{"name":"Main"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");
    let extra = vec![
        DllRef::new("UnityEngine", "/SHOULD_NOT_WIN/UnityEngine.dll"),
        DllRef::new("MyExtra", "/path/MyExtra.dll"),
    ];
    SolutionGenerator::new()
        .generate_from_lockfile(
            &opts(root, BuildPlatform::Ios, BuildConfig::Editor).with_extra_refs(extra),
            &lf,
        )
        .unwrap();
    let csproj = read_file(root, "tpl/ios-editor/Main.csproj");
    // Lockfile UnityEngine path should be present, the extra-refs duplicate must not.
    assert!(csproj.contains("Managed/UnityEngine/UnityEngine.dll"));
    assert!(!csproj.contains("/SHOULD_NOT_WIN/UnityEngine.dll"));
    assert!(csproj.contains("MyExtra"));
}

#[test]
fn changing_asmref_target_reroutes_sources() {
    // After scan-cache exists, changing an .asmref reference name should re-route
    // the directory to a different assembly. The cache is keyed by mtime, so
    // updating the file (which bumps mtime) must trigger a fresh scan.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Asm1.asmdef", r#"{"name":"Asm1"}"#);
    write_file(root, "Assets/B/Asm2.asmdef", r#"{"name":"Asm2"}"#);
    // Each assembly owns its own non-empty source dir so both .csprojs are always emitted.
    write_file(root, "Assets/A/A.cs", "class A {}\n");
    write_file(root, "Assets/B/B.cs", "class B {}\n");
    // Game/Code.cs is routed via asmref.
    write_file(root, "Assets/Game/Assembly.asmref", r#"{"reference":"Asm1"}"#);
    write_file(root, "Assets/Game/Code.cs", "class Code {}\n");

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let asm1 = read_compile_set(root, "tpl/ios-editor/Asm1.csproj");
    let asm2 = read_compile_set(root, "tpl/ios-editor/Asm2.csproj");
    assert!(asm1.contains("Assets/Game/Code.cs"));
    assert!(!asm2.contains("Assets/Game/Code.cs"));

    std::thread::sleep(std::time::Duration::from_millis(10));
    write_file(root, "Assets/Game/Assembly.asmref", r#"{"reference":"Asm2"}"#);

    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let asm1 = read_compile_set(root, "tpl/ios-editor/Asm1.csproj");
    let asm2 = read_compile_set(root, "tpl/ios-editor/Asm2.csproj");
    assert!(!asm1.contains("Assets/Game/Code.cs"));
    assert!(asm2.contains("Assets/Game/Code.cs"));
}

#[test]
fn writefile_if_changed_skips_unchanged() {
    // write_file_if_changed returns false when content matches existing bytes.
    // We verify this end-to-end via mtime: after first write, a no-op regenerate
    // must NOT bump the csproj's mtime.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let csproj_path = root.join("tpl/ios-editor/Lib.csproj");
    let mtime_before = std::fs::metadata(&csproj_path).unwrap().modified().unwrap();

    std::thread::sleep(std::time::Duration::from_millis(20));
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let mtime_after = std::fs::metadata(&csproj_path).unwrap().modified().unwrap();
    assert_eq!(
        mtime_before, mtime_after,
        "csproj mtime changed despite no-op regenerate"
    );
}

#[test]
fn output_dir_with_trailing_slash_normalised() {
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");
    let r = SolutionGenerator::new()
        .generate_from_lockfile(
            &opts(root, BuildPlatform::Ios, BuildConfig::Editor)
                .with_output_dir(Some("Library/foo/bar/")),
            &lf,
        )
        .unwrap();
    assert_eq!(r.variant_csprojs, vec!["Library/foo/bar/Lib.csproj"]);
}

#[test]
fn empty_extra_refs_string_parses_empty() {
    let parsed = DllRef::parse_list("");
    assert!(parsed.is_empty());
}

#[test]
fn inplace_asmdef_edit_invalidates_scan_cache() {
    // Editing an asmdef in place changes only that file's mtime, not its
    // parent dir's. Pre-asmdef-cache, the scan-cache only stat'd parent dirs,
    // so in-place edits were silently missed. The cache now stats asmdef
    // files themselves; an in-place rename of the asmdef must reroute sources.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"OldName"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    assert!(root.join("tpl/ios-editor/OldName.csproj").exists());

    std::thread::sleep(std::time::Duration::from_millis(20));
    // In-place edit: same file path, different name. Parent dir mtime won't bump.
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"NewName"}"#);

    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    assert!(
        root.join("tpl/ios-editor/NewName.csproj").exists(),
        "rename should be picked up via per-file mtime tracking"
    );
}

#[test]
fn no_lockfile_no_templates_falls_back_via_cli_path() {
    // CLI-side: when no lockfile and no templates exist, `generate()`
    // and the CLI `generate` both auto-run lock. We can't exercise the Unity-install
    // scan (no Unity in CI), but we *can* assert that calling generate_from_lockfile
    // directly with no asmdefs at all produces the legacy fallback projects.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    // Plain .cs file under Assets/, no asmdef. Should land in Assembly-CSharp via legacy rules.
    write_file(root, "Assets/Foo.cs", "class Foo {}\n");
    let r = SolutionGenerator::new()
        .generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    assert!(
        r.variant_csprojs
            .iter()
            .any(|s| s.ends_with("Assembly-CSharp.csproj")),
        "expected Assembly-CSharp fallback, got {:?}",
        r.variant_csprojs
    );
}

#[test]
fn generate_fingerprint_short_circuits_render() {
    // After a successful generate, the next call with the same options must
    // skip render+write entirely (validated via mtime preservation on the csproj).
    // Touching the lockfile invalidates and forces a regenerate.
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");

    // Provide a real csproj.lock so the fingerprint can record its mtime.
    let lockfile_path = root.join("Library/UnitySolutionGenerator/csproj.lock");
    std::fs::create_dir_all(lockfile_path.parent().unwrap()).unwrap();
    LockfileIO::write(&lf, lockfile_path.to_str().unwrap()).unwrap();

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let csproj_path = root.join("tpl/ios-editor/Lib.csproj");
    let mtime_first = std::fs::metadata(&csproj_path).unwrap().modified().unwrap();

    std::thread::sleep(std::time::Duration::from_millis(20));
    let r2 = g
        .generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    let mtime_second = std::fs::metadata(&csproj_path).unwrap().modified().unwrap();
    assert_eq!(
        mtime_first, mtime_second,
        "fingerprint hit must not rewrite csproj"
    );
    // Cached result still includes correct paths.
    assert!(r2.variant_csprojs.iter().any(|s| s.ends_with("Lib.csproj")));

    // Stronger invalidation check: change the lockfile *content* (a new
    // static define) and confirm the new define ends up in the rendered
    // Directory.Build.props after regenerate. This proves the fingerprint
    // really did invalidate and the second run actually re-rendered, not
    // just that we didn't crash.
    std::thread::sleep(std::time::Duration::from_millis(20));
    let mut lf2 = lf.clone();
    lf2.defines.push("MY_NEW_DEFINE".to_string());
    std::fs::remove_file(&lockfile_path).unwrap();
    LockfileIO::write(&lf2, lockfile_path.to_str().unwrap()).unwrap();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf2)
        .unwrap();
    let props = read_file(root, "tpl/ios-editor/Directory.Build.props");
    assert!(
        props.contains("MY_NEW_DEFINE"),
        "lockfile change must invalidate fingerprint and re-render props"
    );
}

#[test]
fn generate_fingerprint_invalidates_when_csproj_deleted() {
    // If the user nukes the variant dir, the fingerprint must NOT short-circuit
    // (output files no longer exist).
    let tmp = make_temp_root();
    let root = tmp.path();
    let lf = small_lockfile();
    write_file(root, "Assets/A/Lib.asmdef", r#"{"name":"Lib"}"#);
    write_file(root, "Assets/A/Code.cs", "class Code {}\n");
    let lockfile_path = root.join("Library/UnitySolutionGenerator/csproj.lock");
    std::fs::create_dir_all(lockfile_path.parent().unwrap()).unwrap();
    LockfileIO::write(&lf, lockfile_path.to_str().unwrap()).unwrap();

    let g = SolutionGenerator::new();
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();

    // Wipe the variant dir.
    std::fs::remove_dir_all(root.join("tpl/ios-editor")).unwrap();
    assert!(!root.join("tpl/ios-editor/Lib.csproj").exists());

    // Next generate must rebuild the csproj despite the fingerprint existing.
    g.generate_from_lockfile(&opts(root, BuildPlatform::Ios, BuildConfig::Editor), &lf)
        .unwrap();
    assert!(root.join("tpl/ios-editor/Lib.csproj").exists());
}

#[test]
fn lock_fingerprint_short_circuits_rescan() {
    // We can't test scan_and_write end-to-end without a real Unity install. But we
    // *can* test the cache primitives directly.
    let tmp = make_temp_root();
    let root = tmp.path();
    write_file(root, "Assets/A/Code.cs", "x");

    // Build a fingerprint from a single relative path; verify is_valid round-trips.
    let entries = unity_solution_generator::__test_only::build_entries(
        root.to_str().unwrap(),
        root.to_str().unwrap(),
        &["Assets/A/Code.cs".to_string()],
        &[],
    );
    assert!(!entries.is_empty());
    assert!(unity_solution_generator::__test_only::is_valid(&entries));

    // Touching a contributing dir must invalidate.
    std::thread::sleep(std::time::Duration::from_millis(20));
    write_file(root, "Assets/A/Other.cs", "y");
    assert!(!unity_solution_generator::__test_only::is_valid(&entries));
}

/// Regression: lockfile must invalidate when a missing input later appears.
///
/// `wt go orgel-fix` produced a fresh worktree where `Library/PackageCache/`
/// didn't exist at lock time. The original `build_entries` silently dropped
/// missing paths, so Unity later populating `PackageCache` never invalidated
/// the (incomplete) lockfile. The fix records `(p, 0)` for missing entries
/// and treats appearance as invalidation.
#[test]
fn lock_fingerprint_sentinel_invalidates_on_appearance() {
    let tmp = make_temp_root();
    let root = tmp.path();
    let absent = root.join("Library/PackageCache");

    // build_entries should record `Library/PackageCache` as missing — see
    // `lock_cache::build_entries` (it's seeded unconditionally at the abs root).
    let entries = unity_solution_generator::__test_only::build_entries(
        root.to_str().unwrap(),
        root.to_str().unwrap(),
        &[],
        &[],
    );
    let absent_str = absent.to_string_lossy().to_string();
    let recorded = entries.iter().find(|(p, _)| p == &absent_str);
    assert!(
        recorded.is_some_and(|(_, m)| *m == 0),
        "missing path must be recorded with mtime=0 sentinel; got {:?}",
        recorded
    );
    assert!(unity_solution_generator::__test_only::is_valid(&entries));

    // Path appears → fingerprint invalidates.
    std::fs::create_dir_all(&absent).unwrap();
    assert!(!unity_solution_generator::__test_only::is_valid(&entries));
}

/// Regression: typecheck must surface method-body diagnostics (CS1503 etc.).
///
/// `csc /refonly` emits a reference assembly and — empirically, in the .NET 8
/// SDK's Roslyn build (4.10.x) — silently skips body-binding diagnostics when
/// the bodies don't affect the public API surface. That includes CS1503
/// "cannot convert" at call sites, which Unity Editor's full compile catches.
/// Hit on the meow-tower `orgel-fix` branch: USG typecheck reported `ok` while
/// Unity flagged `Argument 4: cannot convert from 'GameDate?' to 'OrgelDate?'`.
///
/// Structural test: `/refonly` must not be passed to csc. `/deterministic`
/// must remain so the cascade-skip mtime-restore trick still works.
#[test]
fn rsp_has_no_refonly() {
    use std::path::PathBuf;
    let rsp = unity_solution_generator::__test_only::build_rsp(
        "9.0",
        &[],
        &[],
        &[],
        &[],
        &[PathBuf::from("/tmp/A.cs")],
        "/tmp/out.dll",
        false,
    );
    assert!(
        !rsp.lines().any(|l| l.trim() == "/refonly"),
        "rsp must not contain /refonly — it suppresses csc body diagnostics. rsp:\n{}",
        rsp
    );
    assert!(
        rsp.lines().any(|l| l.trim() == "/deterministic"),
        "rsp must keep /deterministic for cascade-skip stability. rsp:\n{}",
        rsp
    );
}