Skip to content

Commit 64f2da4

Browse files
Merge pull request #2272 from multiversx/build-err-fix
sc-meta - build error fixes
2 parents 5fb0bf2 + 496204d commit 64f2da4

File tree

9 files changed

+153
-112
lines changed

9 files changed

+153
-112
lines changed

.github/workflows/actions.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
generate-report:
6666
name: Generate Report
6767
needs: build-contracts
68-
uses: multiversx/mx-sc-actions/.github/workflows/report.yml@v6.0.0
68+
uses: multiversx/mx-sc-actions/.github/workflows/report.yml@739363a9614794fef598ab161498563969ea6437 # report fix, TODO: release v6.0.1
6969
with:
7070
rust-toolchain: 1.87
7171
runs-on: ubuntu-latest

framework/meta-lib/src/contract/meta_config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl MetaConfig {
114114
for contract_variant in &self.sc_config.contracts {
115115
contract_variant
116116
.build_contract(&build_args, &self.output_dir)
117-
.unwrap();
117+
.unwrap_or_else(|err| panic!("{err}"));
118118
}
119119
}
120120

framework/meta-lib/src/contract/sc_config/execute_command.rs

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,34 @@ use std::fmt::Display;
33
use std::fmt::Formatter;
44
use std::process::{Command, ExitStatus, Stdio};
55

6+
use crate::print_util::format_command;
7+
8+
/// Contains a description of the command, for error reporting purposes.
9+
#[derive(Clone, Debug)]
10+
pub struct CommandInfo {
11+
command_string: String,
12+
}
13+
14+
impl From<&mut Command> for CommandInfo {
15+
fn from(command: &mut Command) -> Self {
16+
CommandInfo {
17+
command_string: format_command(command),
18+
}
19+
}
20+
}
21+
22+
impl Display for CommandInfo {
23+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24+
write!(f, "{}", self.command_string)
25+
}
26+
}
27+
28+
/// Represents errors that can occur when executing system commands.
629
#[derive(Debug)]
730
pub enum ExecuteCommandError {
8-
ErrorRunning(String),
9-
JobFailed(String),
10-
ErrorParsing(String),
31+
ErrorRunning(CommandInfo),
32+
JobFailed(CommandInfo),
33+
ErrorParsing(CommandInfo),
1134
ErrorRunningBuildProcess,
1235
}
1336

@@ -16,14 +39,15 @@ impl Error for ExecuteCommandError {}
1639
impl Display for ExecuteCommandError {
1740
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1841
let message = match self {
19-
ExecuteCommandError::ErrorRunning(job) => format!(
20-
"Error running {}: ensure it is installed and available in your system PATH.",
21-
job
42+
ExecuteCommandError::ErrorRunning(command_info) => format!(
43+
"Error running `{command_info}`: ensure it is installed and available in your system PATH.",
2244
),
23-
ExecuteCommandError::JobFailed(job) => {
24-
format!("Job {} failed.", job)
45+
ExecuteCommandError::JobFailed(command_info) => {
46+
format!("Job failed, command: `{command_info}`")
47+
}
48+
ExecuteCommandError::ErrorParsing(command_info) => {
49+
format!("Error parsing output of `{command_info}`")
2550
}
26-
ExecuteCommandError::ErrorParsing(job) => format!("Error parsing {} output", job),
2751
ExecuteCommandError::ErrorRunningBuildProcess => {
2852
"contract build process was not running".to_string()
2953
}
@@ -32,25 +56,21 @@ impl Display for ExecuteCommandError {
3256
}
3357
}
3458

35-
pub(crate) fn execute_command(
36-
command: &mut Command,
37-
job: &str,
38-
) -> Result<String, ExecuteCommandError> {
59+
pub(crate) fn execute_command(command: &mut Command) -> Result<String, ExecuteCommandError> {
3960
let output = command
4061
.stderr(Stdio::inherit())
4162
.output()
42-
.map_err(|_| ExecuteCommandError::ErrorRunning(job.to_string()))?;
63+
.map_err(|_| ExecuteCommandError::ErrorRunning(command.into()))?;
4364

4465
if !output.status.success() {
45-
return Err(ExecuteCommandError::JobFailed(job.to_string()));
66+
return Err(ExecuteCommandError::JobFailed(command.into()));
4667
}
4768

48-
String::from_utf8(output.stdout).map_err(|_| ExecuteCommandError::ErrorParsing(job.to_string()))
69+
String::from_utf8(output.stdout).map_err(|_| ExecuteCommandError::ErrorParsing(command.into()))
4970
}
5071

5172
pub(crate) fn execute_spawn_command(
5273
command: &mut Command,
53-
job: &str,
5474
) -> Result<ExitStatus, ExecuteCommandError> {
5575
let response = command
5676
.spawn()
@@ -59,7 +79,7 @@ pub(crate) fn execute_spawn_command(
5979
.map_err(|_| ExecuteCommandError::ErrorRunningBuildProcess)?;
6080

6181
if !response.success() {
62-
return Err(ExecuteCommandError::JobFailed(job.to_string()));
82+
return Err(ExecuteCommandError::JobFailed(command.into()));
6383
}
6484

6585
Ok(response)

framework/meta-lib/src/contract/sc_config/wasm_build.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,24 @@ impl ContractVariant {
3131

3232
print_build_command(self.wasm_output_name(build_args), &build_command);
3333

34-
let output_build_command = execute_spawn_command(&mut build_command, "cargo");
34+
let build_command_output = execute_spawn_command(&mut build_command);
3535

36-
match output_build_command {
36+
match build_command_output {
3737
Ok(_) => {}
38-
Err(ExecuteCommandError::JobFailed(_)) => {
38+
Err(ExecuteCommandError::JobFailed(err)) => {
3939
if !self.is_target_installed() {
40+
// the target not being installed is a common cause of build failure,
41+
// so we try to install it automatically
4042
self.install_wasm_target();
4143

42-
execute_spawn_command(&mut build_command, "cargo")
43-
.expect("error building contract");
44+
// try again after installing the target
45+
execute_spawn_command(&mut build_command)?;
46+
} else {
47+
return Err(ExecuteCommandError::JobFailed(err));
4448
}
4549
}
46-
Err(_) => {
47-
panic!("error building contract");
50+
Err(err) => {
51+
return Err(err);
4852
}
4953
}
5054

framework/meta-lib/src/tools/build_target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn is_target_installed(rustc_version: &RustcVersion, target_name: &str) -> b
4747

4848
print_util::print_rustup_check_target(rustc_version, target_name, &cmd);
4949

50-
let output_rustup_command = execute_command(&mut cmd, "rustup");
50+
let output_rustup_command = execute_command(&mut cmd);
5151
let str_output_rustup = match output_rustup_command {
5252
Ok(output) => output,
5353
Err(err) => {

framework/meta/src/cmd/check_wasmer_dependencies.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn check_wasmer_dependencies(path: &Path) {
1717
let mut command = Command::new(cargo);
1818
command.arg("tree").arg("-e").arg("features");
1919

20-
match execute_command(&mut command, path, "cargo") {
20+
match execute_command(&mut command, path) {
2121
Ok(output) => {
2222
if output.contains(WASMER_CRATE_NAME) && output.contains(WASMER_EXPERIMENTAL_CRATE_NAME)
2323
{
@@ -36,20 +36,16 @@ pub fn check_wasmer_dependencies(path: &Path) {
3636
};
3737
}
3838

39-
fn execute_command(
40-
command: &mut Command,
41-
path: &Path,
42-
job: &str,
43-
) -> Result<String, ExecuteCommandError> {
39+
fn execute_command(command: &mut Command, path: &Path) -> Result<String, ExecuteCommandError> {
4440
let output = command
4541
.current_dir(path)
4642
.stderr(Stdio::inherit())
4743
.output()
48-
.map_err(|_| ExecuteCommandError::ErrorRunning(job.to_string()))?;
44+
.map_err(|_| ExecuteCommandError::ErrorRunning(command.into()))?;
4945

5046
if !output.status.success() {
51-
return Err(ExecuteCommandError::JobFailed(job.to_string()));
47+
return Err(ExecuteCommandError::JobFailed(command.into()));
5248
}
5349

54-
String::from_utf8(output.stdout).map_err(|_| ExecuteCommandError::ErrorParsing(job.to_string()))
50+
String::from_utf8(output.stdout).map_err(|_| ExecuteCommandError::ErrorParsing(command.into()))
5551
}

framework/meta/src/cmd/template/contract_creator.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use convert_case::{Case, Casing};
2-
31
use crate::{cli::TemplateArgs, version::FrameworkVersion, version_history::LAST_TEMPLATE_VERSION};
42

53
use super::{
@@ -26,17 +24,9 @@ pub async fn create_contract(args: &TemplateArgs) {
2624
}
2725

2826
fn target_from_args(args: &TemplateArgs) -> ContractCreatorTarget {
29-
let new_name = args
30-
.name
31-
.as_deref()
32-
.unwrap_or(&args.template)
33-
.to_case(Case::Kebab);
34-
3527
let target_path = args.path.clone().unwrap_or_default();
36-
ContractCreatorTarget {
37-
target_path,
38-
new_name,
39-
}
28+
let new_name = args.name.as_deref().unwrap_or(&args.template);
29+
ContractCreatorTarget::new(target_path, new_name)
4030
}
4131

4232
pub(crate) fn get_repo_version(args_tag: &Option<String>) -> RepoVersion {

framework/meta/src/cmd/template/contract_creator_target.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
use std::path::PathBuf;
22

3+
use convert_case::{Case, Casing};
4+
35
#[derive(Clone)]
46
pub struct ContractCreatorTarget {
57
pub target_path: PathBuf,
68
pub new_name: String,
79
}
810

911
impl ContractCreatorTarget {
12+
/// Will convert new_name to kebab-case.
13+
pub fn new(contract_dir: PathBuf, new_name: &str) -> Self {
14+
Self {
15+
target_path: contract_dir,
16+
new_name: new_name.to_case(Case::Kebab),
17+
}
18+
}
19+
1020
pub fn contract_dir(&self) -> PathBuf {
1121
self.target_path.join(&self.new_name)
1222
}

0 commit comments

Comments
 (0)