From 0db356ec6756751c7496b3abb4ab5044a5b84539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 15 Sep 2020 00:06:44 +0200 Subject: [PATCH] Improve error output of wasm-builder when wasm ins't installed This improves the error message of wasm-builder when the wasm toolchain isn't installed. Currently we print that the wasm toolchain is not installed, but the actual problem is that there is a bug in the packaging in rust. This will now be much easier to debug, by printing the full error message of the compiler. --- Cargo.lock | 1 + utils/wasm-builder/Cargo.toml | 1 + utils/wasm-builder/src/lib.rs | 7 +++- utils/wasm-builder/src/prerequisites.rs | 47 +++++++++++++++++++------ utils/wasm-builder/src/wasm_project.rs | 2 +- 5 files changed, 46 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c33ff0020acae..d8602d2c6f3f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8858,6 +8858,7 @@ dependencies = [ name = "substrate-wasm-builder" version = "2.0.0" dependencies = [ + "ansi_term 0.12.1", "atty", "build-helper", "cargo_metadata", diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index 5e90625620508..de0f11e84670d 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -22,3 +22,4 @@ fs2 = "0.4.3" wasm-gc-api = "0.1.11" atty = "0.2.13" itertools = "0.8.2" +ansi_term = "0.12.1" diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index 500025c29678e..bb50729f71ce5 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -179,7 +179,7 @@ pub fn build_project_with_default_rustflags( bloaty.wasm_binary_bloaty_path_escaped(), ) }; - + write_file_if_changed( file_name.into(), format!( @@ -309,3 +309,8 @@ impl CargoCommand { .contains("-nightly") } } + +/// Returns `true` when color output is enabled. +fn color_output_enabled() -> bool { + env::var(crate::WASM_BUILD_NO_COLOR).is_err() +} diff --git a/utils/wasm-builder/src/prerequisites.rs b/utils/wasm-builder/src/prerequisites.rs index d7c15095762e8..2a9801744c45b 100644 --- a/utils/wasm-builder/src/prerequisites.rs +++ b/utils/wasm-builder/src/prerequisites.rs @@ -18,14 +18,24 @@ use std::fs; use tempfile::tempdir; +use ansi_term::Color; + +/// Print an error message. +fn print_error_message(message: &str) -> String { + if super::color_output_enabled() { + Color::Red.bold().paint(message).to_string() + } else { + message.into() + } +} /// Checks that all prerequisites are installed. /// /// # Returns /// Returns `None` if everything was found and `Some(ERR_MSG)` if something could not be found. -pub fn check() -> Option<&'static str> { +pub fn check() -> Option { if !check_nightly_installed(){ - return Some("Rust nightly not installed, please install it!") + return Some(print_error_message("Rust nightly not installed, please install it!")) } check_wasm_toolchain_installed() @@ -35,7 +45,7 @@ fn check_nightly_installed() -> bool { crate::get_nightly_cargo().is_nightly() } -fn check_wasm_toolchain_installed() -> Option<&'static str> { +fn check_wasm_toolchain_installed() -> Option { let temp = tempdir().expect("Creating temp dir does not fail; qed"); fs::create_dir_all(temp.path().join("src")).expect("Creating src dir does not fail; qed"); @@ -59,22 +69,39 @@ fn check_wasm_toolchain_installed() -> Option<&'static str> { fs::write(&test_file, "pub fn test() {}") .expect("Writing to the test file does not fail; qed"); - let err_msg = "Rust WASM toolchain not installed, please install it!"; + let err_msg = print_error_message("Rust WASM toolchain not installed, please install it!"); let manifest_path = manifest_path.display().to_string(); - crate::get_nightly_cargo() - .command() - .args(&["build", "--target=wasm32-unknown-unknown", "--manifest-path", &manifest_path]) + + let mut build_cmd = crate::get_nightly_cargo().command(); + + build_cmd.args(&["build", "--target=wasm32-unknown-unknown", "--manifest-path", &manifest_path]); + + if super::color_output_enabled() { + build_cmd.arg("--color=always"); + } + + build_cmd .output() - .map_err(|_| err_msg) + .map_err(|_| err_msg.clone()) .and_then(|s| if s.status.success() { Ok(()) } else { match String::from_utf8(s.stderr) { Ok(ref err) if err.contains("linker `rust-lld` not found") => { - Err("`rust-lld` not found, please install it!") + Err(print_error_message("`rust-lld` not found, please install it!")) }, - _ => Err(err_msg) + Ok(ref err) => Err( + format!( + "{}\n\n{}\n{}\n{}{}\n", + err_msg, + Color::Yellow.bold().paint("Further error information:"), + Color::Yellow.bold().paint("-".repeat(60)), + err, + Color::Yellow.bold().paint("-".repeat(60)), + ) + ), + Err(_) => Err(err_msg), } } ) diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index 4c927f7bdeabd..1d4a4484cf45e 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -449,7 +449,7 @@ fn build_project(project: &Path, default_rustflags: &str) { // We don't want to call ourselves recursively .env(crate::SKIP_BUILD_ENV, ""); - if env::var(crate::WASM_BUILD_NO_COLOR).is_err() { + if super::color_output_enabled() { build_cmd.arg("--color=always"); }