mbx 1.24.1

A build cache for Rust projects
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
use super::cargo::{CARGO_TARGET_DIR_ENV, cargo_roots, exit_code};
use crate::config::Config;
use eyre::{Context, Result};
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::process::{Command, ExitCode};
use std::sync::OnceLock;

const CARGO_SHIM_STEM: &str = "cargo";
const CARGO_SHIM_MODE_ENV: &str = "MBX_CARGO_SHIM_MODE";
const CARGO_SHIM_PATH_ENV: &str = "MBX_CARGO_SHIM_PATH";
static PATH_BEFORE_SHIM_EXCLUSION: OnceLock<OsString> = OnceLock::new();

/// Whether this process was installed under Cargo's name by `mbx setup`.
pub fn is_cargo_shim() -> bool {
    match std::env::var_os(CARGO_SHIM_MODE_ENV).as_deref() {
        // Internal commands must remain mbx commands even when current_exe()
        // points at an installation hardlinked or copied under Cargo's name.
        Some(value) if value == "0" => false,
        Some(value) if value == "1" => true,
        _ => invoked_as_cargo(),
    }
}

fn invoked_as_cargo() -> bool {
    std::env::args_os()
        .next()
        .as_deref()
        .is_some_and(is_cargo_shim_path)
}

fn is_cargo_shim_path(path: &OsStr) -> bool {
    Path::new(path).file_stem().is_some_and(|stem| {
        if cfg!(windows) {
            stem.to_string_lossy().eq_ignore_ascii_case(CARGO_SHIM_STEM)
        } else {
            stem == OsStr::new(CARGO_SHIM_STEM)
        }
    })
}

/// Run an invocation received through the persistent Cargo shim.
pub fn run_cargo_shim() -> Result<ExitCode> {
    let reported_shim = std::env::var_os(CARGO_SHIM_PATH_ENV)
        .map(PathBuf::from)
        .or_else(|| {
            invoked_as_cargo()
                .then(|| std::env::current_exe().ok())
                .flatten()
        });
    // mise removes its wrapper directory before launching mbx, so there is no
    // shim path to exclude. Treating the first remaining Cargo as a shim would
    // instead remove the real tool from PATH.
    let invoked_via_mise_wrapper = !invoked_as_cargo() && reported_shim.is_none();
    // The Windows launcher uses this only to select dispatch in the target mbx.
    // Do not leak it into Cargo, build scripts, or nested mbx commands.
    unsafe { std::env::remove_var(CARGO_SHIM_MODE_ENV) };
    unsafe { std::env::remove_var(CARGO_SHIM_PATH_ENV) };
    #[cfg(windows)]
    if invoked_as_cargo()
        && let Some(code) = forward_to_current_mbx(reported_shim.as_deref())?
    {
        return Ok(code);
    }
    let shim_dir = super::setup_install_dir()
        .ok_or_else(|| eyre::eyre!("the platform data directory could not be located"))?;
    let configured_shim = shim_dir.join(if cfg!(windows) { "cargo.exe" } else { "cargo" });
    let real_cargo = if invoked_via_mise_wrapper {
        resolve_real_cargo(&configured_shim)?
    } else {
        let shim = resolve_active_cargo_shim(
            &configured_shim,
            reported_shim.as_deref().map(Path::as_os_str),
            std::env::var_os("PATH").as_deref(),
        );
        exclude_shim_from_path(&shim)?;
        resolve_real_cargo(&shim)?
    };
    let arguments = std::env::args_os().skip(1).collect::<Vec<_>>();
    if mbx_disabled() {
        return run_real_cargo(&real_cargo, &arguments);
    }
    if enclosing_session(std::env::var_os(crate::session::SOCKET_ENV).as_deref())
        || cargo_proxy_passthrough(&arguments)
    {
        return run_real_cargo(&real_cargo, &arguments);
    }
    let Ok(string_arguments) = super::strings(&arguments) else {
        return run_real_cargo(&real_cargo, &arguments);
    };
    // The roots probe can persist metadata under the cache root. Reject its
    // placement before probing, but only after passthrough decisions above.
    if let Some(root) = mbx_cache_cargo::cache_root() {
        crate::storage::require_local(&root, "mbx cache directory", "MBX_CACHE_DIR")?;
        crate::storage::require_local(
            &root.join("cargo-roots"),
            "mbx cache directory",
            "MBX_CACHE_DIR",
        )?;
    }
    let target_dir_env = std::env::var_os(CARGO_TARGET_DIR_ENV);
    let roots = match cargo_roots(&real_cargo, &string_arguments, target_dir_env.as_deref()) {
        Some(roots) => roots,
        None => match failed_probe(&real_cargo, &arguments, target_dir_env.as_deref()) {
            FailedProbe::Roots(roots) => *roots,
            FailedProbe::Passthrough => return run_real_cargo(&real_cargo, &arguments),
            FailedProbe::Reject => eyre::bail!(
                "could not verify Cargo build storage: metadata probing failed; run cargo metadata --no-deps --format-version 1 with the same manifest and configuration options to diagnose it"
            ),
        },
    };
    // Every probe and the final child must name Cargo directly.
    unsafe { std::env::set_var("CARGO", &real_cargo) };
    let (config, settings) = Config::load_for_cli()?;
    super::cargo::run_with_roots(&config, &settings, &string_arguments, roots)
}

fn resolve_active_cargo_shim(
    configured: &Path,
    reported: Option<&OsStr>,
    path: Option<&OsStr>,
) -> PathBuf {
    reported
        .map(Path::new)
        .filter(|shim| is_cargo_shim_path(shim.as_os_str()) && shim.is_file())
        .map(Path::to_path_buf)
        .or_else(|| {
            let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
            std::env::split_paths(path?)
                .map(|dir| dir.join(name))
                .find(|candidate| candidate.is_file())
        })
        .unwrap_or_else(|| configured.to_path_buf())
}

#[cfg(windows)]
fn forward_to_current_mbx(reported_shim: Option<&Path>) -> Result<Option<ExitCode>> {
    let Some(install_dir) = super::setup_install_dir() else {
        return Ok(None);
    };
    let Some(target) = super::setup::cargo_shim_target(&install_dir) else {
        return Ok(None);
    };
    let mut command = Command::new(&target);
    command
        .args(std::env::args_os().skip(1))
        .env(CARGO_SHIM_MODE_ENV, "1");
    if let Some(reported_shim) = reported_shim {
        command.env(CARGO_SHIM_PATH_ENV, reported_shim);
    }
    let status = command
        .status()
        .wrap_err_with(|| format!("failed to run the current mbx at {}", target.display()))?;
    Ok(Some(exit_code(status)))
}

fn mbx_disabled() -> bool {
    mbx_disabled_value(std::env::var_os("MBX_DISABLE").as_deref())
}

fn mbx_disabled_value(value: Option<&OsStr>) -> bool {
    value.is_some_and(|value| {
        let value = value.to_string_lossy();
        value != "0" && !value.eq_ignore_ascii_case("false")
    })
}

fn enclosing_session(session_socket: Option<&OsStr>) -> bool {
    // Cargo subcommands such as watch and nextest can invoke Cargo again. The
    // outer session's compiler wrapper and agent socket are already inherited,
    // so starting another session here would pay the setup cost for every
    // nested invocation and replace the long-lived watch session's context.
    session_socket.is_some_and(|socket| !socket.is_empty())
}

pub(super) fn cargo_proxy_passthrough(arguments: &[OsString]) -> bool {
    let cargo_arguments = arguments
        .iter()
        .take_while(|argument| argument.as_os_str() != OsStr::new("--"))
        .collect::<Vec<_>>();
    let command = super::launch::cargo_subcommand(arguments);
    if cargo_arguments.iter().any(|argument| {
        matches!(argument.to_str(), Some("-V" | "--help" | "-h"))
            // For install, --version selects a package version.
            || (argument.as_os_str() == "--version" && command != Some("install"))
    }) {
        return true;
    }
    if command == Some("install") {
        // Registry/git sources need their own root discovery. Path installs
        // compile the local workspace and must share its wrapper environment.
        return cargo_arguments
            .iter()
            .any(|argument| argument.as_os_str() == "--list")
            || !cargo_arguments.iter().any(|argument| {
                argument.as_os_str() == "--path"
                    || argument
                        .to_str()
                        .is_some_and(|value| value.starts_with("--path="))
            });
    }
    command.is_none()
        || matches!(
            command,
            Some(
                "help"
                    | "new"
                    | "init"
                    | "add"
                    | "remove"
                    | "update"
                    | "fetch"
                    | "clean"
                    | "config"
                    | "fmt"
                    | "tree"
                    | "search"
                    | "info"
                    | "login"
                    | "logout"
                    | "owner"
                    | "package"
                    | "publish"
                    | "uninstall"
                    | "yank"
                    | "locate-project"
                    | "metadata"
                    | "generate-lockfile"
                    | "pkgid"
                    | "read-manifest"
                    | "report"
                    | "vendor"
                    | "verify-project"
                    | "version"
                    | "git-checkout"
            )
        )
}

/// What to do with an invocation whose roots probe failed.
pub(super) enum FailedProbe {
    /// Roots recovered from the expanded invocation: manage the build.
    Roots(Box<super::cargo::Roots>),
    /// Hand the invocation to Cargo as it was typed.
    Passthrough,
    /// Refuse it: the build storage cannot be verified.
    Reject,
}

/// Decide what a failed roots probe leaves the caller able to do.
///
/// Resolve once, then use the same arguments for root recovery and the
/// decision to allow a manifest-less invocation.
pub(super) fn failed_probe(
    cargo: &OsStr,
    arguments: &[OsString],
    target_dir_env: Option<&OsStr>,
) -> FailedProbe {
    // An alias can name a manifest that the command line does not, so expand
    // it before reading the invocation: `i = "install --path /project"`
    // compiles a local package from a directory that has none of its own.
    let invocation = super::cargo_invocation::resolve(cargo, arguments);
    // The first probe asked about the alias's own name and got nothing back.
    // Ask again as Cargo will read it: the roots belong to the package the
    // expansion names, though the child still receives the command line as it
    // was typed, since Cargo expands the alias itself.
    if let Some(resolved) = &invocation
        && resolved.arguments != arguments
        && let Ok(expanded) = super::strings(&resolved.arguments)
        && let Some(roots) = super::cargo::cargo_roots(cargo, &expanded, target_dir_env)
    {
        return FailedProbe::Roots(Box::new(roots));
    }
    if metadata_failure_passthrough(invocation.as_ref()) {
        note_uncached_passthrough(invocation.as_ref());
        return FailedProbe::Passthrough;
    }
    FailedProbe::Reject
}

/// Say that a command is running without a cache session. An external
/// subcommand may create the workspace itself, as `cargo chef cook` does in an
/// empty directory, and the builds it starts through `$CARGO` then never reach
/// mbx; without a message that looks like a slow build. Other commands outside
/// a project are routine, so they only log at debug level.
fn note_uncached_passthrough(invocation: Option<&super::cargo_invocation::Invocation>) {
    let Some(invocation) = invocation else {
        return;
    };
    let Some((position, command)) = super::launch::cargo_subcommand_at(&invocation.arguments)
    else {
        return;
    };
    // Cargo's own quiet flag precedes the subcommand; anything after it belongs
    // to the subcommand.
    let quiet = invocation.arguments[..position]
        .iter()
        .any(|argument| argument == "-q" || argument == "--quiet");
    if matches!(invocation.kind, super::cargo_invocation::Kind::External) && !quiet {
        log::info!(
            "no Cargo manifest in scope; running `cargo {command}` without the cache. If it builds a workspace it creates, write the manifest first so the build runs inside an mbx session"
        );
    } else {
        log::debug!("no Cargo manifest in scope; running `cargo {command}` without the cache");
    }
}

/// Preserve invocations outside a project, non-build aliases, and
/// unknown-command diagnostics without allowing failed metadata to launch a
/// build alias or an external Cargo command inside one.
fn metadata_failure_passthrough(invocation: Option<&super::cargo_invocation::Invocation>) -> bool {
    let Some(invocation) = invocation else {
        return false;
    };
    // Only losslessly resolved invocations reach this point. No manifest
    // means Cargo has no local package whose storage needs verification.
    if let Ok(strings) = super::strings(&invocation.arguments)
        && let Ok(working_dir) = std::env::current_dir()
        && !mbx_cache_cargo::manifest_in_scope(
            // External commands own their flags. A missing --manifest-path
            // must not disguise the project the invocation runs inside.
            if matches!(invocation.kind, super::cargo_invocation::Kind::External) {
                &[]
            } else {
                &strings
            },
            &working_dir,
        )
    {
        return true;
    }
    matches!(
        invocation.kind,
        super::cargo_invocation::Kind::Unknown | super::cargo_invocation::Kind::Proxy
    )
}

fn same_path(left: &Path, right: &Path) -> bool {
    let (left, right) = match (left.canonicalize(), right.canonicalize()) {
        (Ok(left), Ok(right)) => (left, right),
        _ => (left.to_path_buf(), right.to_path_buf()),
    };
    #[cfg(windows)]
    {
        left.to_string_lossy()
            .eq_ignore_ascii_case(&right.to_string_lossy())
    }
    #[cfg(not(windows))]
    {
        left == right
    }
}

/// Keep explicit `mbx build`-style commands from rediscovering a persistent
/// Cargo shim or mise command wrapper on PATH.
pub(super) fn prepare_explicit_cargo() -> Result<()> {
    let standalone_shim = super::setup_install_dir()
        .map(|directory| directory.join(if cfg!(windows) { "cargo.exe" } else { "cargo" }))
        .filter(|shim| shim.is_file());
    let Some(path) = std::env::var_os("PATH") else {
        return Ok(());
    };
    let (path, proxy) = path_without_cargo_proxies(&path, standalone_shim.as_deref())?;
    let Some(proxy) = proxy else {
        return Ok(());
    };
    let cargo = resolve_explicit_cargo(
        &proxy,
        standalone_shim.as_deref(),
        std::env::var_os("CARGO").as_deref(),
        &path,
    )?;
    PATH_BEFORE_SHIM_EXCLUSION.get_or_init(|| std::env::var_os("PATH").unwrap_or_default());
    // SAFETY: CLI dispatch is single-threaded here, before the cache session
    // or any child process has started.
    unsafe {
        std::env::set_var("PATH", path);
        std::env::set_var("CARGO", cargo);
    }
    Ok(())
}

fn path_without_cargo_proxies(
    path: &OsStr,
    standalone_shim: Option<&Path>,
) -> Result<(OsString, Option<PathBuf>)> {
    let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
    let mut first_proxy = None;
    let directories = std::env::split_paths(path).filter(|directory| {
        let candidate = directory.join(name);
        let is_standalone = standalone_shim.is_some_and(|shim| same_path(&candidate, shim));
        let is_proxy =
            candidate.is_file() && (is_standalone || is_mise_command_wrapper_path(&candidate));
        if is_proxy {
            first_proxy.get_or_insert(candidate);
        }
        !is_proxy
    });
    Ok((std::env::join_paths(directories)?, first_proxy))
}

/// Resolve Cargo for an explicit command without accepting its standalone shim.
fn resolve_explicit_cargo(
    proxy: &Path,
    standalone_shim: Option<&Path>,
    configured: Option<&OsStr>,
    path: &OsStr,
) -> Result<OsString> {
    if let Some(configured) = configured.map(Path::new).filter(|candidate| {
        candidate.is_absolute()
            && candidate.is_file()
            && !is_mise_command_wrapper_path(candidate)
            && standalone_shim.is_none_or(|shim| !same_path(candidate, shim))
    }) {
        return Ok(configured.as_os_str().to_owned());
    }
    let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
    for directory in std::env::split_paths(path) {
        let candidate = directory.join(name);
        if candidate.is_file() {
            return Ok(candidate.into_os_string());
        }
    }
    eyre::bail!(
        "could not find the real Cargo after excluding {}",
        proxy.display()
    )
}

fn exclude_shim_from_path(shim: &Path) -> Result<()> {
    let Some(path) = std::env::var_os("PATH") else {
        return Ok(());
    };
    PATH_BEFORE_SHIM_EXCLUSION.get_or_init(|| path.clone());
    let path = path_without_shim(shim, &path)?;
    // SAFETY: CLI dispatch is single-threaded here, before any child process
    // or cache session has started.
    unsafe { std::env::set_var("PATH", path) };
    Ok(())
}

pub(super) fn activation_path() -> Option<OsString> {
    PATH_BEFORE_SHIM_EXCLUSION
        .get()
        .cloned()
        .or_else(|| std::env::var_os("PATH"))
}

fn path_without_shim(shim: &Path, path: &OsStr) -> Result<OsString> {
    let Some(shim_dir) = shim.parent() else {
        return Ok(path.to_owned());
    };
    std::env::join_paths(
        std::env::split_paths(path).filter(|directory| !same_path(directory, shim_dir)),
    )
    .map_err(Into::into)
}

fn resolve_real_cargo(current: &Path) -> Result<OsString> {
    let configured = std::env::var_os("CARGO");
    if let Some(cargo) = configured_real_cargo(current, configured.as_deref()) {
        return Ok(cargo);
    }
    let path = std::env::var_os("PATH").ok_or_else(|| eyre::eyre!("PATH is not set"))?;
    resolve_real_cargo_from(current, None, &path)
}

fn configured_real_cargo(current: &Path, configured: Option<&OsStr>) -> Option<OsString> {
    let configured = Path::new(configured?);
    let current_dir = current.parent();
    (configured.is_absolute()
        && configured.is_file()
        && !is_mise_cargo_proxy_path(configured)
        && current_dir.is_none_or(|current_dir| {
            configured
                .parent()
                .is_some_and(|parent| !same_path(parent, current_dir))
        }))
    .then(|| configured.as_os_str().to_owned())
}

pub(crate) fn is_mise_command_wrapper_path(path: &Path) -> bool {
    let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
    path.ends_with(Path::new("command-wrappers").join("bin").join(name))
}

fn is_mise_cargo_proxy_path(path: &Path) -> bool {
    if is_mise_command_wrapper_path(path) {
        return true;
    }
    let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
    let Some(shims) = path.parent().filter(|parent| {
        parent.file_name() == Some(OsStr::new("shims"))
            && path.file_name() == Some(OsStr::new(name))
    }) else {
        return false;
    };
    let Some(data_dir) = shims.parent() else {
        return false;
    };
    data_dir.file_name() == Some(OsStr::new("mise"))
        || std::env::var_os("MISE_DATA_DIR")
            .is_some_and(|configured| same_path(data_dir, Path::new(&configured)))
}

fn resolve_real_cargo_from(
    current: &Path,
    configured: Option<&OsStr>,
    path: &OsStr,
) -> Result<OsString> {
    let current_dir = current.parent();
    if let Some(cargo) = configured_real_cargo(current, configured) {
        return Ok(cargo);
    }
    let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
    for directory in std::env::split_paths(path) {
        if current_dir.is_some_and(|current_dir| same_path(&directory, current_dir)) {
            continue;
        }
        let candidate = directory.join(name);
        if candidate.is_file() && !is_mise_cargo_proxy_path(&candidate) {
            return Ok(candidate.into_os_string());
        }
    }
    eyre::bail!(
        "could not find the real Cargo after excluding {}",
        current.display()
    )
}

fn run_real_cargo(cargo: &OsStr, arguments: &[OsString]) -> Result<ExitCode> {
    let status = Command::new(cargo)
        .args(arguments)
        .status()
        .wrap_err_with(|| format!("failed to run {}", cargo.to_string_lossy()))?;
    Ok(exit_code(status))
}

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

    #[test]
    fn cargo_shim_passthrough_preserves_cargo_namespace_and_toolchains() {
        assert!(cargo_proxy_passthrough(&[]));
        assert!(cargo_proxy_passthrough(&["--verbose".into()]));
        assert!(cargo_proxy_passthrough(&[
            "--color".into(),
            "always".into()
        ]));
        assert!(!cargo_proxy_passthrough(&[
            "--color".into(),
            "always".into(),
            "build".into()
        ]));
        assert!(cargo_proxy_passthrough(&[
            "-C".into(),
            "project".into(),
            "clean".into()
        ]));
        assert!(!cargo_proxy_passthrough(&["cache".into(), "stats".into()]));
        assert!(cargo_proxy_passthrough(&[
            "install".into(),
            "ripgrep".into()
        ]));
        assert!(cargo_proxy_passthrough(&[
            "+nightly".into(),
            "--version".into()
        ]));
        assert!(!cargo_proxy_passthrough(&[
            "+nightly".into(),
            "build".into()
        ]));
        assert!(!cargo_proxy_passthrough(&[
            "test".into(),
            "--workspace".into()
        ]));
        assert!(!cargo_proxy_passthrough(&[
            "run".into(),
            "--".into(),
            "--help".into()
        ]));
        for command in [
            "clean",
            "config",
            "fmt",
            "pkgid",
            "read-manifest",
            "report",
            "vendor",
            "verify-project",
        ] {
            assert!(
                cargo_proxy_passthrough(&[command.into()]),
                "{command} should not start an mbx session"
            );
        }
        for command in [
            "build", "check", "test", "bench", "run", "rustc", "rustdoc", "doc", "clippy", "fix",
        ] {
            assert!(
                !cargo_proxy_passthrough(&[command.into()]),
                "{command} should start an mbx session"
            );
        }
    }

    #[test]
    fn cargo_shim_wraps_path_installs_but_not_queries_or_remote_sources() {
        for args in [
            vec!["install", "--path", "."],
            vec!["install", "--path", ".", "--version", "0.1.0"],
            vec![
                "install",
                "--locked",
                "--path=foo",
                "--target-dir",
                "target",
            ],
            vec!["+stable", "--color", "always", "install", "--path", "foo"],
        ] {
            let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
            assert!(!cargo_proxy_passthrough(&args), "{args:?}");
        }
        for args in [
            vec!["install", "ripgrep"],
            vec!["install", "--git", "https://example.com/repo"],
            vec!["install", "--list"],
            vec!["install", "--path", ".", "--help"],
            vec!["install", "--list", "--path", "."],
            vec!["install", "--", "--path", "."],
        ] {
            let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
            assert!(cargo_proxy_passthrough(&args), "{args:?}");
        }
    }

    #[test]
    fn cargo_shim_reuses_an_enclosing_session() {
        assert!(enclosing_session(Some(OsStr::new("session-socket"))));
        assert!(!enclosing_session(Some(OsStr::new(""))));
        assert!(!enclosing_session(None));
    }

    #[test]
    fn cargo_shim_identification_and_disable_values_are_explicit() {
        assert!(is_cargo_shim_path(OsStr::new("/tmp/cargo")));
        assert!(is_cargo_shim_path(OsStr::new("cargo.exe")));
        assert!(!is_cargo_shim_path(OsStr::new("/tmp/mbx")));
        assert!(mbx_disabled_value(Some(OsStr::new("1"))));
        assert!(!mbx_disabled_value(Some(OsStr::new("0"))));
        assert!(!mbx_disabled_value(Some(OsStr::new("false"))));
        assert!(!mbx_disabled_value(None));
    }

    #[cfg(windows)]
    #[test]
    fn cargo_shim_paths_are_case_insensitive_on_windows() {
        assert!(is_cargo_shim_path(OsStr::new(r"C:\Tools\CARGO.EXE")));
        assert!(same_path(
            Path::new(r"C:\Users\Example\mbx\bin"),
            Path::new(r"c:\users\example\MBX\BIN")
        ));
    }

    #[test]
    fn cargo_resolution_excludes_the_shim_directory_and_prevents_recursion() {
        let directory = tempfile::tempdir().unwrap();
        let shim_dir = directory.path().join("shim");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let shim = shim_dir.join(name);
        let real = real_dir.join(name);
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([&shim_dir, &real_dir]).unwrap();

        assert_eq!(
            resolve_real_cargo_from(&shim, Some(shim.as_os_str()), &path).unwrap(),
            real.into_os_string()
        );
    }

    #[test]
    fn cargo_resolution_uses_the_active_shim_when_the_configured_home_changed() {
        let directory = tempfile::tempdir().unwrap();
        let configured = directory.path().join("new-home/bin/cargo");
        let shim_dir = directory.path().join("original-home/bin");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let shim = shim_dir.join(name);
        let real = real_dir.join(name);
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([&shim_dir, &real_dir]).unwrap();

        let active = resolve_active_cargo_shim(&configured, None, Some(&path));
        assert_eq!(active, shim);
        let path = path_without_shim(&active, &path).unwrap();
        assert_eq!(
            resolve_real_cargo_from(&active, None, &path).unwrap(),
            real.into_os_string()
        );
    }

    #[cfg(windows)]
    #[test]
    fn reported_windows_shim_wins_when_real_cargo_precedes_it_on_path() {
        let directory = tempfile::tempdir().unwrap();
        let configured = directory.path().join("new-home/bin/cargo.exe");
        let shim_dir = directory.path().join("shim");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let shim = shim_dir.join("cargo.exe");
        let real = real_dir.join("cargo.exe");
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([&real_dir, &shim_dir]).unwrap();

        let active = resolve_active_cargo_shim(&configured, Some(shim.as_os_str()), Some(&path));
        assert_eq!(active, shim);
        let path = path_without_shim(&active, &path).unwrap();
        assert_eq!(
            resolve_real_cargo_from(&active, None, &path).unwrap(),
            real.into_os_string()
        );
    }

    #[test]
    fn configured_real_cargo_does_not_need_a_path_search() {
        let directory = tempfile::tempdir().unwrap();
        let shim_dir = directory.path().join("shim");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let shim = shim_dir.join(name);
        let real = real_dir.join(name);
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&real, b"real").unwrap();

        assert_eq!(
            configured_real_cargo(&shim, Some(real.as_os_str())),
            Some(real.into_os_string())
        );
    }

    #[test]
    fn cargo_resolution_rejects_mise_proxies_as_the_real_cargo() {
        let directory = tempfile::tempdir().unwrap();
        let shim_dir = directory.path().join("shim");
        let wrapper_dir = directory.path().join("command-wrappers/bin");
        let mise_shims_dir = directory.path().join("mise/shims");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&wrapper_dir).unwrap();
        std::fs::create_dir_all(&mise_shims_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let shim = shim_dir.join(name);
        let wrapper = wrapper_dir.join(name);
        let mise_shim = mise_shims_dir.join(name);
        let real = real_dir.join(name);
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&wrapper, b"wrapper").unwrap();
        std::fs::write(&mise_shim, b"mise shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([&wrapper_dir, &mise_shims_dir, &real_dir]).unwrap();

        assert_eq!(
            configured_real_cargo(&shim, Some(wrapper.as_os_str())),
            None
        );
        assert_eq!(
            configured_real_cargo(&shim, Some(mise_shim.as_os_str())),
            None
        );
        assert_eq!(
            resolve_real_cargo_from(&shim, Some(mise_shim.as_os_str()), &path).unwrap(),
            real.into_os_string()
        );
    }

    #[test]
    fn cargo_resolution_removes_the_shim_directory_from_child_path() {
        let directory = tempfile::tempdir().unwrap();
        let shim_dir = directory.path().join("shim");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let shim = shim_dir.join(name);
        let path = std::env::join_paths([&shim_dir, &real_dir]).unwrap();

        assert_eq!(
            std::env::split_paths(&path_without_shim(&shim, &path).unwrap()).collect::<Vec<_>>(),
            vec![real_dir]
        );
    }

    #[test]
    fn explicit_cargo_excludes_mise_wrapper_from_child_path() {
        let directory = tempfile::tempdir().unwrap();
        let wrapper_dir = directory.path().join("command-wrappers/bin");
        let shim_dir = directory.path().join("mbx/bin");
        let mise_shims_dir = directory.path().join("mise/shims");
        let tool_dir = directory.path().join("tool/bin");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&wrapper_dir).unwrap();
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&mise_shims_dir).unwrap();
        std::fs::create_dir_all(&tool_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        let name = if cfg!(windows) { "cargo.exe" } else { "cargo" };
        let wrapper = wrapper_dir.join(name);
        let shim = shim_dir.join(name);
        let mise_shim = mise_shims_dir.join(name);
        let real = real_dir.join(name);
        std::fs::write(&wrapper, b"mise wrapper").unwrap();
        std::fs::write(&shim, b"mbx shim").unwrap();
        std::fs::write(&mise_shim, b"mise shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([
            &wrapper_dir,
            &shim_dir,
            &mise_shims_dir,
            &tool_dir,
            &real_dir,
        ])
        .unwrap();

        let (path, proxy) = path_without_cargo_proxies(&path, Some(&shim)).unwrap();

        assert_eq!(proxy, Some(wrapper.clone()));
        assert_eq!(
            std::env::split_paths(&path).collect::<Vec<_>>(),
            vec![mise_shims_dir, tool_dir, real_dir]
        );
        assert_eq!(
            resolve_explicit_cargo(&wrapper, Some(&shim), Some(shim.as_os_str()), &path).unwrap(),
            mise_shim.into_os_string()
        );
    }

    #[cfg(unix)]
    #[test]
    fn cargo_resolution_excludes_a_symlink_to_the_shim_directory() {
        use std::os::unix::fs::symlink;

        let directory = tempfile::tempdir().unwrap();
        let shim_dir = directory.path().join("shim");
        let shim_link = directory.path().join("linked-shim");
        let real_dir = directory.path().join("real");
        std::fs::create_dir_all(&shim_dir).unwrap();
        std::fs::create_dir_all(&real_dir).unwrap();
        symlink(&shim_dir, &shim_link).unwrap();
        let shim = shim_dir.join("cargo");
        let real = real_dir.join("cargo");
        std::fs::write(&shim, b"shim").unwrap();
        std::fs::write(&real, b"real").unwrap();
        let path = std::env::join_paths([&shim_link, &real_dir]).unwrap();

        assert_eq!(
            resolve_real_cargo_from(&shim, None, &path).unwrap(),
            real.into_os_string()
        );
    }
}