Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Improve error output of wasm-builder when wasm ins't installed #7105

Merged
merged 1 commit into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions utils/wasm-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 6 additions & 1 deletion utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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()
}
47 changes: 37 additions & 10 deletions utils/wasm-builder/src/prerequisites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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()
Expand All @@ -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<String> {
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");

Expand All @@ -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),
}
}
)
Expand Down
2 changes: 1 addition & 1 deletion utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down