Repository: dtolnay/cargo-expand (language: rust)

Issue:
Maybe: work around rustup#3036
<!--  ❤️  -->

context: https://github.com/rust-lang/rustup/issues/3036 https://github.com/rust-lang/rustup/issues/3035

As of rustup 1.25, cargo-expand doesn't work when run from stable:

```nu
〉main:pegcel〉$env.CARGO
Error: nu::shell::name_not_found (link)

  × Name not found
   ╭─[entry #27:1:1]
 1 │ $env.CARGO
   ·      ──┬──
   ·        ╰── did you mean 'Path'?
   ╰────

〉main:pegcel〉rustup -vV
rustup 1.25.1 (bb60b1e89 2022-07-12)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.62.1 (e092d0b6b 2022-07-16)`
〉main:pegcel〉cargo +nightly expand --version
cargo-expand 1.0.27
〉main:pegcel〉cargo expand --example item
error: no such subcommand: `+nightly`
〉main:pegcel〉
```

AIUI 1.25.1 reverted (perhaps temporarily?) the change to set `$RUSTC` so that `cargo +toolchain` could function when run from a cargo invocation. However, there's a separate change that also impacts Windows that leads to the toolchain cargo being selected instead of the rustup shim.

I'm honestly not exactly certain what the correct behavior w.r.t. https://github.com/rust-lang/rustup/issues/3036 is. Perhaps the correct option is to use `rustup run nightly cargo` instead of `cargo +nightly`?


Discussion / hints:

Fix patch:
diff --git a/src/main.rs b/src/main.rs
index 1a2e7ad..fe44e47 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -57,12 +57,14 @@ fn main() {
 fn cargo_expand_or_run_nightly() -> Result<i32> {
     const NO_RUN_NIGHTLY: &str = "CARGO_EXPAND_NO_RUN_NIGHTLY";
 
-    if env::var_os(NO_RUN_NIGHTLY).is_some() || maybe_nightly() || !can_run_plus_nightly() {
+    if env::var_os(NO_RUN_NIGHTLY).is_some() || maybe_nightly() || !can_rustup_run_nightly() {
         return cargo_expand();
     }
 
-    let mut nightly = Command::new("cargo");
-    nightly.arg("+nightly");
+    let mut nightly = Command::new("rustup");
+    nightly.arg("run");
+    nightly.arg("nightly");
+    nightly.arg("cargo");
     nightly.arg("expand");
 
     let mut args = env::args_os().peekable();
@@ -110,9 +112,11 @@ fn definitely_not_nightly() -> bool {
     version.starts_with("cargo 1") && !version.contains("nightly")
 }
 
-fn can_run_plus_nightly() -> bool {
-    Command::new("cargo")
-        .arg("+nightly")
+fn can_rustup_run_nightly() -> bool {
+    Command::new("rustup")
+        .arg("run")
+        .arg("nightly")
+        .arg("cargo")
         .arg("--version")
         .output()
         .map_or(false, |output| output.status.success())


Test patch: