瀏覽代碼

Warn about broken platform-tools installation (#5415)

* Warn about broken installation

* Use try_exists
Lucas Ste 8 月之前
父節點
當前提交
7527bbb418
共有 2 個文件被更改,包括 85 次插入0 次删除
  1. 25 0
      platform-tools-sdk/cargo-build-sbf/src/main.rs
  2. 60 0
      platform-tools-sdk/cargo-build-sbf/tests/crates.rs

+ 25 - 0
platform-tools-sdk/cargo-build-sbf/src/main.rs

@@ -517,6 +517,23 @@ fn check_undefined_symbols(config: &Config, program: &Path) {
     }
 }
 
+// Check if we have all binaries in place to execute the build command.
+// If the download failed or the binaries were somehow deleted, inform the user how to fix it.
+fn corrupted_toolchain(config: &Config) -> bool {
+    let toolchain_path = config
+        .sbf_sdk
+        .join("dependencies")
+        .join("platform-tools")
+        .join("rust");
+
+    let binaries = toolchain_path.join("bin");
+
+    !toolchain_path.try_exists().unwrap_or(false)
+        || !binaries.try_exists().unwrap_or(false)
+        || !binaries.join("rustc").try_exists().unwrap_or(false)
+        || !binaries.join("cargo").try_exists().unwrap_or(false)
+}
+
 // check whether custom solana toolchain is linked, and link it if it is not.
 fn link_solana_toolchain(config: &Config) {
     let toolchain_path = config
@@ -728,6 +745,14 @@ fn build_solana_package(
         }
     }
 
+    if corrupted_toolchain(config) {
+        error!(
+            "The Solana toolchain is corrupted. Please, run cargo-build-sbf with the \
+        --force-tools-install argument to fix it."
+        );
+        exit(1);
+    }
+
     let llvm_bin = config
         .sbf_sdk
         .join("dependencies")

+ 60 - 0
platform-tools-sdk/cargo-build-sbf/tests/crates.rs

@@ -231,3 +231,63 @@ fn test_workspace_metadata_tools_version() {
     run_cargo_build("workspace-metadata", &[], false);
     clean_target("workspace-metadata");
 }
+
+#[test]
+#[serial]
+fn test_corrupted_toolchain() {
+    run_cargo_build("noop", &[], false);
+
+    fn assert_failed_command() {
+        let cwd = env::current_dir().expect("Unable to get current working directory");
+        let toml = cwd
+            .join("tests")
+            .join("crates")
+            .join("noop")
+            .join("Cargo.toml");
+        let toml = format!("{}", toml.display());
+        let args = vec!["--sbf-sdk", "../sbf", "--manifest-path", &toml];
+
+        let mut cmd = assert_cmd::Command::cargo_bin("cargo-build-sbf").unwrap();
+        let assert = cmd.env("RUST_LOG", "debug").args(&args).assert();
+        let output = assert.get_output();
+
+        assert!(
+            String::from_utf8_lossy(&output.stderr).contains("The Solana toolchain is corrupted.")
+        );
+    }
+
+    let cwd = env::current_dir().expect("Unable to get current working directory");
+    let sdk_path = cwd.parent().unwrap().join("sbf");
+
+    let bin_folder = sdk_path
+        .join("dependencies")
+        .join("platform-tools")
+        .join("rust")
+        .join("bin");
+    fs::rename(bin_folder.join("cargo"), bin_folder.join("cargo_2"))
+        .expect("Failed to rename file");
+
+    assert_failed_command();
+
+    fs::rename(bin_folder.join("cargo_2"), bin_folder.join("cargo"))
+        .expect("Failed to rename file");
+    fs::rename(bin_folder.join("rustc"), bin_folder.join("rustc_2"))
+        .expect("Failed to rename file");
+
+    assert_failed_command();
+
+    fs::rename(bin_folder.join("rustc_2"), bin_folder.join("rustc"))
+        .expect("Failed to rename file");
+    fs::rename(&bin_folder, bin_folder.parent().unwrap().join("bin2"))
+        .expect("Failed to rename file");
+
+    assert_failed_command();
+
+    fs::rename(bin_folder.parent().unwrap().join("bin2"), &bin_folder)
+        .expect("Failed to rename file");
+    let rust_folder = bin_folder.parent().unwrap();
+    fs::rename(rust_folder, rust_folder.parent().unwrap().join("rust_2"))
+        .expect("Failed to rename file");
+
+    assert_failed_command();
+}