Skip to content

Commit

Permalink
Add flag to reverse errors (#4142)
Browse files Browse the repository at this point in the history
Adds the ability to reverse errors and warnings in either the command line or via a BuildProfile.

closes #3860
  • Loading branch information
gr00vytvniks committed Jun 21, 2023
1 parent fd8a28b commit 00982e4
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub struct BuildProfile {
pub json_abi_with_callpaths: bool,
#[serde(default)]
pub error_on_warnings: bool,
pub reverse_results: bool,
}

impl DependencyDetails {
Expand Down Expand Up @@ -697,6 +698,7 @@ impl BuildProfile {
include_tests: false,
json_abi_with_callpaths: false,
error_on_warnings: false,
reverse_results: false,
}
}

Expand All @@ -714,6 +716,7 @@ impl BuildProfile {
include_tests: false,
json_abi_with_callpaths: false,
error_on_warnings: false,
reverse_results: false,
}
}
}
Expand Down
24 changes: 21 additions & 3 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ pub struct PrintOpts {
pub intermediate_asm: bool,
/// Print the generated Sway IR (Intermediate Representation).
pub ir: bool,
/// Output build errors and warnings in reverse order.
pub reverse_order: bool,
}

#[derive(Default, Clone)]
Expand Down Expand Up @@ -1804,8 +1806,9 @@ pub fn compile(
let sway_build_config =
sway_build_config(pkg.manifest_file.dir(), &entry_path, pkg.target, profile)?;
let terse_mode = profile.terse;
let reverse_results = profile.reverse_results;
let fail = |warnings, errors| {
print_on_failure(engines.se(), terse_mode, warnings, errors);
print_on_failure(engines.se(), terse_mode, warnings, errors, reverse_results);
bail!("Failed to compile {}", pkg.name);
};

Expand Down Expand Up @@ -2314,7 +2317,13 @@ pub fn build(
};

let fail = |warnings, errors| {
print_on_failure(engines.se(), profile.terse, warnings, errors);
print_on_failure(
engines.se(),
profile.terse,
warnings,
errors,
profile.reverse_results,
);
bail!("Failed to compile {}", pkg.name);
};

Expand Down Expand Up @@ -2410,7 +2419,16 @@ pub fn build(
contract_id_value.clone(),
) {
Ok(o) => o,
Err(errs) => return fail(&[], &errs),
Err(errs) => {
print_on_failure(
engines.se(),
profile.terse,
&[],
&errs,
profile.reverse_results,
);
bail!("Failed to compile {}", pkg.name);
}
};

let mut compiled = compile(
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy) -> pkg::BuildOpts {
finalized_asm: cmd.print.finalized_asm,
intermediate_asm: cmd.print.intermediate_asm,
ir: cmd.print.ir,
reverse_order: cmd.print.reverse_order,
},
time_phases: cmd.print.time_phases,
metrics_outfile: cmd.print.metrics_outfile.clone(),
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/op/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
finalized_asm: cmd.print.finalized_asm,
intermediate_asm: cmd.print.intermediate_asm,
ir: cmd.print.ir,
reverse_order: cmd.print.reverse_order,
},
minify: pkg::MinifyOpts {
json_abi: cmd.minify.json_abi,
Expand Down
20 changes: 16 additions & 4 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,27 @@ pub fn print_on_failure(
terse_mode: bool,
warnings: &[CompileWarning],
errors: &[CompileError],
reverse_results: bool,
) {
let e_len = errors.len();
let w_len = warnings.len();

if !terse_mode {
warnings
.iter()
.for_each(|w| format_warning(source_engine, w));
errors.iter().for_each(|e| format_err(source_engine, e));
if reverse_results {
warnings
.iter()
.rev()
.for_each(|w| format_warning(source_engine, w));
errors
.iter()
.rev()
.for_each(|e| format_err(source_engine, e));
} else {
warnings
.iter()
.for_each(|w| format_warning(source_engine, w));
errors.iter().for_each(|e| format_err(source_engine, e));
}
}

if e_len == 0 && w_len > 0 {
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn opts_from_cmd(cmd: Command) -> forc_test::Opts {
finalized_asm: cmd.build.print.finalized_asm,
intermediate_asm: cmd.build.print.intermediate_asm,
ir: cmd.build.print.ir,
reverse_order: cmd.build.print.reverse_order,
},
time_phases: cmd.build.print.time_phases,
metrics_outfile: cmd.build.print.metrics_outfile,
Expand Down
3 changes: 3 additions & 0 deletions forc/src/cli/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ pub struct Print {
/// Output the time elapsed over each part of the compilation process.
#[clap(long)]
pub time_phases: bool,
/// Output build errors and warnings in reverse order.
#[clap(long)]
pub reverse_order: bool,
/// Output compilation metrics into file.
#[clap(long)]
pub metrics_outfile: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
finalized_asm: cmd.build.print.finalized_asm,
intermediate_asm: cmd.build.print.intermediate_asm,
ir: cmd.build.print.ir,
reverse_order: cmd.build.print.reverse_order,
},
time_phases: cmd.build.print.time_phases,
metrics_outfile: cmd.build.print.metrics_outfile,
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_contract_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ fn build_opts_from_cmd(cmd: &ContractIdCommand) -> pkg::BuildOpts {
finalized_asm: cmd.print.finalized_asm,
intermediate_asm: cmd.print.intermediate_asm,
ir: cmd.print.ir,
reverse_order: cmd.print.reverse_order,
},
time_phases: cmd.print.time_phases,
metrics_outfile: cmd.print.metrics_outfile.clone(),
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_predicate_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ fn build_opts_from_cmd(cmd: PredicateRootCommand) -> pkg::BuildOpts {
finalized_asm: cmd.print.finalized_asm,
intermediate_asm: cmd.print.intermediate_asm,
ir: cmd.print.ir,
reverse_order: cmd.print.reverse_order,
},
time_phases: cmd.print.time_phases,
metrics_outfile: cmd.print.metrics_outfile,
Expand Down

0 comments on commit 00982e4

Please sign in to comment.