From c0eea805f92b503606cdbda586f123ea952b630a Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Mon, 26 Jun 2023 14:15:08 +0300 Subject: [PATCH 1/3] feat(wasm-builder): remove username from panic messages --- Cargo.lock | 1 + utils/wasm-builder/Cargo.toml | 1 + utils/wasm-builder/src/cargo_command.rs | 25 ++++++++++++++++++++++ utils/wasm-builder/src/lib.rs | 28 +++++++++++++++++++++++-- utils/wasm-builder/src/wasm_project.rs | 5 +++++ 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c45ba6245c..bc801c9071c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4238,6 +4238,7 @@ dependencies = [ "cargo_metadata", "chrono", "colored", + "dirs", "gear-core", "gear-wasm-instrument", "gmeta", diff --git a/utils/wasm-builder/Cargo.toml b/utils/wasm-builder/Cargo.toml index 79044527114..c876488ce9e 100644 --- a/utils/wasm-builder/Cargo.toml +++ b/utils/wasm-builder/Cargo.toml @@ -20,6 +20,7 @@ log.workspace = true pathdiff.workspace = true which.workspace = true colored.workspace = true +dirs.workspace = true gmeta.workspace = true gear-core.workspace = true gear-wasm-instrument.workspace = true diff --git a/utils/wasm-builder/src/cargo_command.rs b/utils/wasm-builder/src/cargo_command.rs index 5cfc7148003..99d0a90e296 100644 --- a/utils/wasm-builder/src/cargo_command.rs +++ b/utils/wasm-builder/src/cargo_command.rs @@ -31,6 +31,7 @@ pub struct CargoCommand { target_dir: PathBuf, features: Vec, toolchain: Toolchain, + paths_to_remap: Vec<(PathBuf, &'static str)>, } impl CargoCommand { @@ -44,6 +45,7 @@ impl CargoCommand { target_dir: "target".into(), features: vec![], toolchain: Toolchain::nightly(), + paths_to_remap: vec![], } } @@ -74,6 +76,13 @@ impl CargoCommand { self.toolchain = toolchain; } + /// Set paths to remap. + /// + /// Used to hide the username from the panic message. + pub fn set_paths_to_remap(&mut self, paths_to_remap: &[(PathBuf, &'static str)]) { + self.paths_to_remap = paths_to_remap.into(); + } + /// Execute the `cargo` command with invoking supplied arguments. pub fn run(&self) -> Result<()> { let mut cargo = Command::new(&self.path); @@ -101,6 +110,22 @@ impl CargoCommand { self.remove_cargo_encoded_rustflags(&mut cargo); + if !self.paths_to_remap.is_empty() { + // `--remap-path-prefix` is used to remove username from panic messages + // https://doc.rust-lang.org/rustc/command-line-arguments.html#--remap-path-prefix-remap-source-names-in-output + let global_encoded_rustflags = self + .paths_to_remap + .iter() + .map(|(from, to)| format!("--remap-path-prefix={from}={to}", from = from.display())) + .collect::>() + .join("\x1f"); + + // The environment variable `CARGO_ENCODED_RUSTFLAGS` is used to globally remap path prefix. + // It is also separated by `\x1f` to support folders with spaces or any unusual characters. + // Unlike `cargo rust`, this is useful for passing flags to all dependencies (i.e. globally). + cargo.env("CARGO_ENCODED_RUSTFLAGS", global_encoded_rustflags); + } + let status = cargo.status().context("unable to execute cargo command")?; ensure!( status.success(), diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index c800384f933..f8f6bd7ba75 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -17,10 +17,14 @@ // along with this program. If not, see . use crate::{cargo_command::CargoCommand, cargo_toolchain::Toolchain, wasm_project::WasmProject}; -use anyhow::Result; +use anyhow::{Context, Result}; use gmeta::{Metadata, MetadataRepr}; use regex::Regex; -use std::{env, path::Path, process}; +use std::{ + env, + path::{Path, PathBuf}, + process, +}; use wasm_project::ProjectType; mod builder_error; @@ -103,6 +107,7 @@ impl WasmBuilder { let profile = if profile == "debug" { "dev" } else { profile }; self.cargo.set_profile(profile.to_string()); self.cargo.set_features(&self.enabled_features()?); + self.cargo.set_paths_to_remap(&self.paths_to_remap()?); self.cargo.run()?; self.wasm_project.postprocess() @@ -159,6 +164,25 @@ impl WasmBuilder { } Ok(matched_features) } + + fn paths_to_remap(&self) -> Result> { + let home_dir = dirs::home_dir().context("unable to get home directory")?; + + let project_dir = self.wasm_project.original_dir(); + + let cargo_dir = std::env::var_os("CARGO_HOME") + .map(PathBuf::from) + .context("unable to get cargo home directory")?; + + let cargo_checkouts_dir = cargo_dir.join("git").join("checkouts"); + + Ok(vec![ + (home_dir, "/home"), + (project_dir, "/proj"), + (cargo_dir, "/cargo"), + (cargo_checkouts_dir, "/deps"), + ]) + } } impl Default for WasmBuilder { diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index d9ae8c56e64..93da7ca311f 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -120,6 +120,11 @@ impl WasmProject { self.out_dir.join("Cargo.toml") } + /// Return the path to the original project directory. + pub fn original_dir(&self) -> PathBuf { + self.original_dir.clone() + } + /// Return the path to the target directory. pub fn target_dir(&self) -> PathBuf { self.target_dir.clone() From 3459ed01a7c4a7719f257ef01d51ae9ea7e14d38 Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Tue, 27 Jun 2023 22:54:57 +0300 Subject: [PATCH 2/3] apply suggested changes --- utils/wasm-builder/src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index f8f6bd7ba75..dc9a1cbfc38 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -107,7 +107,9 @@ impl WasmBuilder { let profile = if profile == "debug" { "dev" } else { profile }; self.cargo.set_profile(profile.to_string()); self.cargo.set_features(&self.enabled_features()?); - self.cargo.set_paths_to_remap(&self.paths_to_remap()?); + if profile == "release" { + self.cargo.set_paths_to_remap(&self.paths_to_remap()?); + } self.cargo.run()?; self.wasm_project.postprocess() @@ -178,7 +180,7 @@ impl WasmBuilder { Ok(vec![ (home_dir, "/home"), - (project_dir, "/proj"), + (project_dir, "/code"), (cargo_dir, "/cargo"), (cargo_checkouts_dir, "/deps"), ]) From 8eb140a5f7b14493100a9407da60c7d7c65880dd Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Wed, 28 Jun 2023 14:57:35 +0300 Subject: [PATCH 3/3] improve condition --- utils/wasm-builder/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index dc9a1cbfc38..8817503a055 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -107,7 +107,7 @@ impl WasmBuilder { let profile = if profile == "debug" { "dev" } else { profile }; self.cargo.set_profile(profile.to_string()); self.cargo.set_features(&self.enabled_features()?); - if profile == "release" { + if profile != "dev" { self.cargo.set_paths_to_remap(&self.paths_to_remap()?); }