uv 0.11.28

A Python package and project manager
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
use anyhow::Result;
use assert_cmd::prelude::*;
use assert_fs::prelude::*;
use insta::assert_snapshot;
use serde_json::json;
use std::process::Command;

use uv_fs::Simplified;
use uv_static::EnvVars;
#[cfg(unix)]
use uv_test::ReadOnlyDirectoryGuard;
use uv_test::{TestContext, uv_snapshot};

fn write_project(
    context: &TestContext,
    requires_python: &str,
    dependencies: &[&str],
) -> Result<()> {
    let pyproject_toml = toml::to_string(&json!({
        "project": {
            "name": "project",
            "version": "0.1.0",
            "requires-python": requires_python,
            "dependencies": dependencies,
        }
    }))?;
    context
        .temp_dir
        .child("pyproject.toml")
        .write_str(&pyproject_toml)?;
    Ok(())
}

#[test]
fn sync_centralized_env() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"])
        .with_filtered_centralized_environment_hashes();
    write_project(&context, ">=3.12", &["iniconfig"])?;

    // Creates the environment in the centralized store.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Creating virtual environment `project-cp3.12.[X]-[HASH]`
    Resolved 2 packages in [TIME]
    Prepared 1 package in [TIME]
    Installed 1 package in [TIME]
     + iniconfig==2.0.0
    "#);

    let link = context.temp_dir.child(".venv");
    let target = fs_err::read_link(link.path())?;
    // The project link points into the cache.
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
    });

    fs_err::remove_dir_all(link.path())?;

    // Reuses the cache entry without recreating `.venv`.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--dry-run")
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Would use project environment at: [CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]
    Resolved 2 packages in [TIME]
    Found up-to-date lockfile at: uv.lock
    Checked 1 package in [TIME]
    Would make no changes
    "#);
    // Only the cached environment remains.
    assert!(!link.exists());
    assert!(target.is_dir());
    Ok(())
}

#[test]
fn sync_centralized_env_switch_python() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.11", "3.12"]);
    write_project(&context, ">=3.11", &[])?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12")
        .assert()
        .success();
    let link_312 = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(link_312.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
    });

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.11")
        .assert()
        .success();
    let link_311 = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(link_311.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.11.[X]-[HASH]");
    });

    // The original environment is reused, not recreated.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);
    Ok(())
}

#[test]
#[cfg(feature = "test-python-managed")]
fn sync_centralized_env_distinguishes_python_patch() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&[])
        .with_managed_python_dirs()
        .with_python_download_cache();
    context
        .python_install()
        .arg("3.12.9")
        .arg("3.12.11")
        .assert()
        .success();
    write_project(&context, ">=3.12", &[])?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12.9")
        .assert()
        .success();
    let first = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(first.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.9-[HASH]");
    });

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12.11")
        .assert()
        .success();
    let second = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(second.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.11-[HASH]");
    });
    Ok(())
}

#[test]
#[cfg(feature = "test-python-managed")]
fn sync_centralized_env_survives_python_patch_upgrade() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&[])
        .with_managed_python_dirs()
        .with_python_download_cache();
    context.python_install().arg("3.12.9").assert().success();
    write_project(&context, ">=3.12", &[])?;

    // Create an upgradeable environment.
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12")
        .assert()
        .success();
    let first = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(first.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12-[HASH]");
    });

    // The transparent upgrade retargets the environment to a different interpreter.
    let python = if cfg!(windows) {
        first.join("Scripts/python.exe")
    } else {
        first.join("bin/python")
    };
    context.python_install().arg("3.12.11").assert().success();
    uv_snapshot!(context.filters(), Command::new(&python).arg("--version"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Python 3.12.11

    ----- stderr -----
    "#);

    // Should reuse the same environment without re-creating after the transparent upgrade.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    let second = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    assert_eq!(first, second);
    Ok(())
}

#[test]
fn sync_centralized_env_avoids_project_name_collisions() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    let project_a = context.temp_dir.child("project-a");
    let project_b = context.temp_dir.child("project-b");
    for project in [&project_a, &project_b] {
        project.create_dir_all()?;
        project.child("pyproject.toml").write_str(
            r#"
            [project]
            name = "project"
            version = "0.1.0"
            requires-python = ">=3.12"
            dependencies = []
            "#,
        )?;
    }

    for project in [&project_a, &project_b] {
        context
            .sync()
            .arg("--preview-features")
            .arg("centralized-project-envs")
            .arg("--project")
            .arg(project.path())
            .assert()
            .success();
    }

    let target_a = fs_err::read_link(project_a.child(".venv").path())?;
    let target_b = fs_err::read_link(project_b.child(".venv").path())?;
    // Projects with the same name use different environments.
    assert_ne!(target_a, target_b);
    Ok(())
}

#[test]
fn sync_centralized_env_respects_explicit_environments() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .env(EnvVars::UV_PROJECT_ENVIRONMENT, "override")
        .assert()
        .success();
    // `UV_PROJECT_ENVIRONMENT` bypasses centralized environments.
    assert!(context.temp_dir.child("override").is_dir());
    assert!(!context.temp_dir.child(".venv").exists());

    let active = context.temp_dir.child("active");
    context.venv().arg(active.path()).assert().success();
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--active")
        .env_remove(EnvVars::UV_PROJECT_ENVIRONMENT)
        .env(EnvVars::VIRTUAL_ENV, active.path())
        .assert()
        .success();
    // `--active` uses `VIRTUAL_ENV` directly.
    assert!(active.is_dir());
    assert!(!context.temp_dir.child(".venv").exists());
    Ok(())
}

#[test]
fn sync_centralized_env_respects_active_default_environment() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    let environment = context.temp_dir.child(".venv");
    context.venv().arg(environment.path()).assert().success();

    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--active")
        .env(EnvVars::VIRTUAL_ENV, environment.path()), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    assert!(!context.cache_dir.child("environments-v2").exists());
    Ok(())
}

#[test]
fn sync_centralized_env_virtual_workspace() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    context.temp_dir.child("pyproject.toml").write_str(
        r#"
        [tool.uv.workspace]
        members = ["member"]
        "#,
    )?;
    let member = context.temp_dir.child("member");
    member.create_dir_all()?;
    member.child("pyproject.toml").write_str(
        r#"
        [project]
        name = "member"
        version = "0.1.0"
        requires-python = ">=3.12"
        dependencies = []
        "#,
    )?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    let target = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    // The workspace root owns the centralized environment.
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/temp-cp3.12.[X]-[HASH]");
    });
    // The workspace member does not get its own environment.
    assert!(!member.child(".venv").exists());

    context
        .sync()
        .arg("--package")
        .arg("member")
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    // Selecting a workspace member still uses the workspace environment.
    assert_eq!(
        fs_err::read_link(context.temp_dir.child(".venv").path())?,
        target
    );
    assert!(!member.child(".venv").exists());
    Ok(())
}

#[test]
fn sync_centralized_env_dry_run() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &["iniconfig"])?;

    // Reports the persistent centralized environment path.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--dry-run")
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Would create project environment at: [CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]
    Resolved 2 packages in [TIME]
    Would create lockfile at: uv.lock
    Would download 1 package
    Would install 1 package
     + iniconfig==2.0.0
    "#);
    // Dry-run creates neither the persistent environment nor its project link.
    assert!(!context.temp_dir.child(".venv").exists());
    assert!(!context.cache_dir.child("environments-v2").exists());
    Ok(())
}

#[test]
fn cache_prune_removes_and_recreates_centralized_environment() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    let cache_dir = "cache";

    context
        .sync()
        .env(EnvVars::UV_CACHE_DIR, cache_dir)
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();
    let link = context.temp_dir.child(".venv");
    let target = fs_err::read_link(link.path())?;

    context
        .prune()
        .env(EnvVars::UV_CACHE_DIR, cache_dir)
        .assert()
        .success();
    assert!(!target.exists());
    assert_eq!(target, fs_err::read_link(link.path())?);

    // Without the preview, uv replaces the dangling cache link with a local environment.
    context
        .sync()
        .env(EnvVars::UV_CACHE_DIR, cache_dir)
        .assert()
        .success();
    assert!(link.is_dir());
    assert!(fs_err::read_link(link.path()).is_err());

    context
        .sync()
        .env(EnvVars::UV_CACHE_DIR, cache_dir)
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();
    // The recreated environment uses the same cache entry.
    assert_eq!(target, fs_err::read_link(link.path())?);
    // The dangling target is recreated.
    assert!(target.is_dir());
    Ok(())
}

#[test]
fn sync_recovers_incomplete_centralized_environment() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    let link = context.temp_dir.child(".venv");
    let target = fs_err::read_link(link.path())?;

    // Simulate a mangled environment (e.g., due to interruption).
    uv_fs::remove_virtualenv(link.path())?;
    uv_fs::remove_virtualenv(&target)?;
    fs_err::create_dir(&target)?;
    uv_fs::cachedir::ensure_tag(&target)?;
    fs_err::write(target.join(".gitignore"), "*")?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    assert_eq!(target, fs_err::read_link(link.path())?);
    assert!(target.join("pyvenv.cfg").is_file());
    Ok(())
}

#[test]
fn sync_centralized_env_no_cache_uses_dot_venv() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"])
        .with_filtered_centralized_environment_hashes();
    write_project(&context, ">=3.12", &[])?;

    uv_snapshot!(context.filters(), context.sync()
        .arg("--no-cache")
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    warning: The `centralized-project-envs` feature has no effect when `--no-cache` is enabled
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Creating virtual environment at: .venv
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    let environment = context.temp_dir.child(".venv");
    assert!(environment.is_dir());
    assert!(fs_err::read_link(environment.path()).is_err());

    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Creating virtual environment `project-cp3.12.[X]-[HASH]`
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    let target = fs_err::read_link(environment.path())?;
    // A later cached invocation replaces the local environment with a centralized one.
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
    });

    // With `--no-cache`, uv follows the existing `.venv` link without recreating the environment.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--no-cache")
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    warning: The `centralized-project-envs` feature has no effect when `--no-cache` is enabled
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);
    assert_eq!(fs_err::read_link(environment.path())?, target);
    Ok(())
}

#[test]
fn sync_centralized_env_replaces_existing_directory_link() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    let environment = context.temp_dir.child(".venv");
    let previous_target = context.temp_dir.child("previous-target");
    previous_target.create_dir_all()?;
    let marker = previous_target.child("marker");
    marker.touch()?;
    uv_fs::create_symlink(previous_target.path(), environment.path())?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    let target = fs_err::read_link(environment.path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
    });
    assert!(marker.is_file());
    Ok(())
}

#[test]
fn sync_centralized_env_with_existing_file() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    let environment = context.temp_dir.child(".venv");
    environment.write_str("user-data")?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    #[cfg(unix)]
    {
        let target = fs_err::read_link(environment.path())?;
        insta::with_settings!({ filters => context.filters() }, {
            assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
        });
    }

    #[cfg(windows)]
    {
        // TODO(tk): This changes once `.venv` can store an environment path.
        assert_eq!(fs_err::read_to_string(environment.path())?, "user-data");
        assert!(context.cache_dir.child("environments-v2").is_dir());
    }
    Ok(())
}

#[cfg(windows)]
#[test]
fn sync_centralized_env_replaces_existing_empty_directory() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"]);
    write_project(&context, ">=3.12", &[])?;
    context.temp_dir.child(".venv").create_dir_all()?;

    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    let target = fs_err::read_link(context.temp_dir.child(".venv").path())?;
    insta::with_settings!({ filters => context.filters() }, {
        assert_snapshot!(target.portable_display(), @"[CACHE_DIR]/environments-v2/project-cp3.12.[X]-[HASH]");
    });
    Ok(())
}

#[test]
fn run_and_sync_link_failure_reporting() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"])
        .with_filtered_centralized_environment_hashes()
        .with_filter((
            r"(?m)^(warning: Failed to create link to project environment at `[^`]+`): .*$",
            "$1: [ERR]",
        ));
    write_project(&context, ">=3.12", &["iniconfig"])?;
    let environment = context.temp_dir.child(".venv");
    environment.create_dir_all()?;
    environment.child("keep").touch()?;

    // `uv run` uses the centralized environment without reporting a link update failure.
    uv_snapshot!(context.filters(), context.run()
        .current_dir(&context.home_dir)
        .arg("--project")
        .arg(context.temp_dir.path())
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("python")
        .arg("-c")
        .arg("import iniconfig"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Creating virtual environment `project-cp3.12.[X]-[HASH]`
    Resolved 2 packages in [TIME]
    Prepared 1 package in [TIME]
    Installed 1 package in [TIME]
     + iniconfig==2.0.0
    "#);

    assert!(environment.child("keep").is_file());

    // `uv sync` reports the same link update failure to the user.
    uv_snapshot!(context.filters(), context.sync()
        .current_dir(&context.home_dir)
        .arg("--project")
        .arg(context.temp_dir.path())
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    warning: Failed to create link to project environment at `[VENV]/`: [ERR]
    Resolved 2 packages in [TIME]
    Checked 1 package in [TIME]
    "#);

    assert!(environment.child("keep").is_file());
    Ok(())
}

#[cfg(unix)]
#[test]
fn sync_centralized_env_local_environment_removal_failure_is_not_fatal() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"])
        .with_filtered_centralized_environment_hashes();
    write_project(&context, ">=3.12", &[])?;
    let environment = context.temp_dir.child(".venv");
    environment.create_dir_all()?;
    let pyvenv_cfg = environment.child("pyvenv.cfg");
    pyvenv_cfg.touch()?;

    // Prevent uv from removing `pyvenv.cfg` while replacing the local environment.
    let _guard = ReadOnlyDirectoryGuard::new(environment.path())?;
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Creating virtual environment `project-cp3.12.[X]-[HASH]`
    warning: Failed to remove existing local virtual environment at `.venv`: failed to remove file `[VENV]/pyvenv.cfg`: Permission denied (os error 13)
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    assert!(pyvenv_cfg.is_file());
    assert!(context.cache_dir.child("environments-v2").is_dir());
    Ok(())
}

#[cfg(unix)]
#[test]
fn sync_centralized_env_link_creation_failure_preserves_cached_target() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.12"])
        .with_filter((r"\.tmp[a-zA-Z0-9]+", "[TMP]"));
    write_project(&context, ">=3.12", &[])?;
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .assert()
        .success();

    let environment = context.temp_dir.child(".venv");
    // Record the cache target to verify the failed link update leaves it selected.
    let target = fs_err::read_link(environment.path())?;

    let _guard = ReadOnlyDirectoryGuard::new(context.temp_dir.path())?;
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    warning: Failed to create link to project environment at `.venv`: Permission denied (os error 13) at path "[TEMP_DIR]/[TMP]"
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    assert_eq!(target, fs_err::read_link(environment.path())?);
    assert!(target.join("pyvenv.cfg").is_file());
    Ok(())
}

#[test]
fn sync_replaces_environment_links_without_removing_cached_targets() -> Result<()> {
    let context = uv_test::test_context_with_versions!(&["3.11", "3.12"]);
    write_project(&context, ">=3.11", &[])?;
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12")
        .assert()
        .success();
    // Record the cache target so the final sync can verify it remains reusable.
    let cache_target = fs_err::read_link(context.temp_dir.child(".venv").path())?;

    let override_environment = context.temp_dir.child("override");
    uv_fs::create_symlink(&cache_target, override_environment.path())?;
    context
        .sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.11")
        .env(EnvVars::UV_PROJECT_ENVIRONMENT, "override")
        .assert()
        .success();

    // An explicit environment path is local, but replacing it does not remove the cached target.
    assert!(override_environment.is_dir());
    assert!(fs_err::read_link(override_environment.path()).is_err());

    let environment = context.temp_dir.child(".venv");
    let intermediate = context.temp_dir.child("intermediate");
    uv_fs::create_symlink(&cache_target, intermediate.path())?;
    uv_fs::replace_symlink(intermediate.path(), environment.path())?;

    uv_snapshot!(context.filters(), context
        .sync()
        .arg("--python")
        .arg("3.11"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.11.[X] interpreter at: [PYTHON-3.11]
    Removed link to project environment at: .venv
    Creating virtual environment at: .venv
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    // Without the preview, uv replaces the indirect cache link with a local environment.
    assert!(environment.is_dir());
    assert!(fs_err::read_link(environment.path()).is_err());

    // uv rebuilds the linked environment without replacing the link.
    let target = context.temp_dir.child("environment");
    fs_err::rename(environment.path(), target.path())?;
    uv_fs::create_symlink(target.path(), environment.path())?;
    uv_snapshot!(context.filters(), context.sync()
        .arg("--python")
        .arg("3.12"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
    Removed virtual environment at: .venv
    Creating virtual environment at: .venv
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);

    assert_eq!(fs_err::read_link(environment.path())?, target.path());
    // The link still points to the rebuilt Python 3.12 environment.
    let python = if cfg!(windows) {
        target.join("Scripts/python.exe")
    } else {
        target.join("bin/python")
    };
    uv_snapshot!(context.filters(), Command::new(python).arg("--version"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----
    Python 3.12.[X]

    ----- stderr -----
    "#);

    // uv reuses the cached environment without recreating it.
    uv_snapshot!(context.filters(), context.sync()
        .arg("--preview-features")
        .arg("centralized-project-envs")
        .arg("--python")
        .arg("3.12"), @r#"
    success: true
    exit_code: 0
    ----- stdout -----

    ----- stderr -----
    Resolved 1 package in [TIME]
    Checked in [TIME]
    "#);
    assert_eq!(fs_err::read_link(environment.path())?, cache_target);
    Ok(())
}