spin_doctor/rustlang/
target.rs

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
use anyhow::Result;
use async_trait::async_trait;

use crate::{Diagnosis, Diagnostic, PatientApp, StopDiagnosing, Treatment};

/// VersionDiagnostic detects problems with the app manifest version field.
#[derive(Default)]
pub struct TargetDiagnostic;

#[async_trait]
impl Diagnostic for TargetDiagnostic {
    type Diagnosis = TargetDiagnosis;

    async fn diagnose(&self, patient: &PatientApp) -> Result<Vec<Self::Diagnosis>> {
        // TODO: this, down to the "does the app use Rust" check, probably ought to move up to the Rust level
        // but we can defer this until we have more Rust diagnoses
        let manifest_str = patient.manifest_doc.to_string();
        let manifest = spin_manifest::manifest_from_str(&manifest_str)?;
        let uses_rust = manifest.components.values().any(|c| {
            c.build
                .as_ref()
                .map(|b| b.commands().any(|c| c.starts_with("cargo")))
                .unwrap_or_default()
        });

        if uses_rust {
            diagnose_rust_wasi_target().await
        } else {
            Ok(vec![])
        }
    }
}

async fn diagnose_rust_wasi_target() -> Result<Vec<TargetDiagnosis>> {
    // does any component contain a build command with `cargo` as the program?
    // if so, run rustup target list --installed and:
    // - if rustup is not present, check if cargo is present
    //   - if not, return RustNotInstalled
    //   - if so, warn but return empty list (Rust is installed but not via rustup, so we can't perform a diagnosis - bit of an edge case this one, and the user probably knows what they're doing...?)
    // - if rustup is present but the list does not contain wasm32-wasi, return WasmTargetNotInstalled
    // - if the list does contain wasm32-wasi, return an empty list
    // NOTE: this does not currently check against the Rust SDK MSRV - that could
    // be a future enhancement or separate diagnosis, but at least the Rust compiler
    // should give a clear error for that!

    let diagnosis = match get_rustup_target_status().await? {
        RustupStatus::AllInstalled => vec![],
        RustupStatus::WasiNotInstalled => vec![TargetDiagnosis::WasmTargetNotInstalled],
        RustupStatus::RustupNotInstalled => match get_cargo_status().await? {
            CargoStatus::Installed => {
                terminal::warn!(
                    "Spin Doctor can't determine if the Rust wasm32-wasi target is installed."
                );
                vec![]
            }
            CargoStatus::NotInstalled => vec![TargetDiagnosis::RustNotInstalled],
        },
    };
    Ok(diagnosis)
}

#[allow(clippy::enum_variant_names)]
enum RustupStatus {
    RustupNotInstalled,
    WasiNotInstalled,
    AllInstalled,
}

async fn get_rustup_target_status() -> Result<RustupStatus> {
    let target_list_output = tokio::process::Command::new("rustup")
        .args(["target", "list", "--installed"])
        .output()
        .await;
    let status = match target_list_output {
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                RustupStatus::RustupNotInstalled
            } else {
                anyhow::bail!("Failed to run `rustup target list --installed`: {e:#}")
            }
        }
        Ok(output) => {
            let stdout = String::from_utf8_lossy(&output.stdout);
            if stdout.lines().any(|line| line == "wasm32-wasi") {
                RustupStatus::AllInstalled
            } else {
                RustupStatus::WasiNotInstalled
            }
        }
    };
    Ok(status)
}

enum CargoStatus {
    Installed,
    NotInstalled,
}

async fn get_cargo_status() -> Result<CargoStatus> {
    let cmd_output = tokio::process::Command::new("cargo")
        .arg("--version")
        .output()
        .await;
    let status = match cmd_output {
        Err(e) => {
            if e.kind() == std::io::ErrorKind::NotFound {
                CargoStatus::NotInstalled
            } else {
                anyhow::bail!("Failed to run `cargo --version`: {e:#}")
            }
        }
        Ok(_) => CargoStatus::Installed,
    };
    Ok(status)
}

/// TargetDiagnosis represents a problem with the Rust target.
#[derive(Debug)]
pub enum TargetDiagnosis {
    /// Rust is not installed: neither cargo nor rustup is present
    RustNotInstalled,
    /// The Rust wasm32-wasi target is not installed: rustup is present but the target isn't
    WasmTargetNotInstalled,
}

impl Diagnosis for TargetDiagnosis {
    fn description(&self) -> String {
        match self {
            Self::RustNotInstalled => "The Rust compiler isn't installed".into(),
            Self::WasmTargetNotInstalled => {
                "The required Rust target 'wasm32-wasi' isn't installed".into()
            }
        }
    }

    fn treatment(&self) -> Option<&dyn Treatment> {
        Some(self)
    }
}

#[async_trait]
impl Treatment for TargetDiagnosis {
    fn summary(&self) -> String {
        match self {
            Self::RustNotInstalled => "Install the Rust compiler and the wasm32-wasi target",
            Self::WasmTargetNotInstalled => "Install the Rust wasm32-wasi target",
        }
        .into()
    }

    async fn dry_run(&self, _patient: &PatientApp) -> Result<String> {
        let message = match self {
            Self::RustNotInstalled => "Download and run the Rust installer from https://rustup.rs, with the `--target wasm32-wasi` option",
            Self::WasmTargetNotInstalled => "Run the following command:\n    `rustup target add wasm32-wasi`",
        };
        Ok(message.into())
    }

    async fn treat(&self, _patient: &mut PatientApp) -> Result<()> {
        match self {
            Self::RustNotInstalled => {
                install_rust_with_wasi_target().await?;
            }
            Self::WasmTargetNotInstalled => {
                install_wasi_target()?;
            }
        }
        Ok(())
    }
}

async fn install_rust_with_wasi_target() -> Result<()> {
    let status = run_rust_installer().await?;
    anyhow::ensure!(status.success(), "Rust installation failed: {status:?}");
    let stop = StopDiagnosing::new("Because Rust was just installed, you may need to run a script or restart your command shell to add Rust to your PATH. Please follow the instructions at the end of the installer output above before re-running `spin doctor`.");
    Err(anyhow::anyhow!(stop))
}

#[cfg(not(windows))]
async fn run_rust_installer() -> Result<std::process::ExitStatus> {
    use std::io::Write;

    let resp = reqwest::get("https://sh.rustup.rs").await?;
    let script = resp.bytes().await?;

    let mut cmd = std::process::Command::new("sh");
    cmd.args(["-s", "--", "--target", "wasm32-wasi"]);
    cmd.stdin(std::process::Stdio::piped());
    let mut shell = cmd.spawn()?;
    let mut stdin = shell.stdin.take().unwrap();
    std::thread::spawn(move || {
        stdin.write_all(&script).unwrap();
    });

    let output = shell.wait_with_output()?;
    Ok(output.status)
}

#[cfg(windows)]
async fn run_rust_installer() -> Result<std::process::ExitStatus> {
    // We currently distribute Windows builds only for x64, so hopefully
    // this won't be an issue.
    if std::env::consts::ARCH != "x86_64" {
        anyhow::bail!("Spin Doctor can only install Rust for Windows on x64 processors");
    }

    let temp_dir = tempfile::TempDir::new()?;
    let installer_path = temp_dir.path().join("rustup-init.exe");

    let resp = reqwest::get("https://win.rustup.rs/x86_64").await?;
    let installer_bin = resp.bytes().await?;

    std::fs::write(&installer_path, &installer_bin)?;

    let mut cmd = std::process::Command::new(installer_path);
    cmd.args(["--target", "wasm32-wasi"]);
    let status = cmd.status()?;
    Ok(status)
}

fn install_wasi_target() -> Result<()> {
    let mut cmd = std::process::Command::new("rustup");
    cmd.args(["target", "add", "wasm32-wasi"]);
    let status = cmd.status()?;
    anyhow::ensure!(
        status.success(),
        "Installation command {cmd:?} failed: {status:?}"
    );
    Ok(())
}