Skip to content

Commit

Permalink
Add forc build flag to treat warnings as errors (FuelLabs#4177)
Browse files Browse the repository at this point in the history
Closes FuelLabs#3190

## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.
  • Loading branch information
vaivaswatha authored Feb 24, 2023
1 parent af217d0 commit 8e797a1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ pub struct BuildProfile {
pub terse: bool,
pub time_phases: bool,
pub include_tests: bool,
pub error_on_warnings: bool,
}

impl Dependency {
Expand Down Expand Up @@ -604,6 +605,7 @@ impl BuildProfile {
terse: false,
time_phases: false,
include_tests: false,
error_on_warnings: false,
}
}

Expand All @@ -617,6 +619,7 @@ impl BuildProfile {
terse: false,
time_phases: false,
include_tests: false,
error_on_warnings: false,
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ pub struct BuildOpts {
pub release: bool,
/// Output the time elapsed over each part of the compilation process.
pub time_phases: bool,
/// Warnings must be treated as compiler errors.
pub error_on_warnings: bool,
/// Include all test functions within the build.
pub tests: bool,
/// List of constants to inject for each package.
Expand Down Expand Up @@ -1746,7 +1748,9 @@ pub fn compile(
Some(CompiledBytecode {
bytecode: bytes,
config_const_offsets: config_offsets,
}) if bc_res.errors.is_empty() => {
}) if bc_res.errors.is_empty()
&& (bc_res.warnings.is_empty() || !build_profile.error_on_warnings) =>
{
print_on_success(terse_mode, &pkg.name, &bc_res.warnings, &tree_type);

if let ProgramABI::Fuel(ref mut json_abi_program) = json_abi_program {
Expand Down Expand Up @@ -1870,6 +1874,7 @@ fn build_profile_from_opts(
release,
time_phases,
tests,
error_on_warnings,
..
} = build_options;
let mut selected_build_profile = BuildProfile::DEBUG;
Expand Down Expand Up @@ -1913,6 +1918,7 @@ fn build_profile_from_opts(
profile.terse |= pkg.terse;
profile.time_phases |= time_phases;
profile.include_tests |= tests;
profile.error_on_warnings |= error_on_warnings;

Ok((selected_build_profile.to_string(), profile))
}
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 @@ -171,6 +171,7 @@ fn build_opts_from_cmd(cmd: &cmd::Deploy) -> pkg::BuildOpts {
},
build_profile: cmd.build_profile.build_profile.clone(),
release: cmd.build_profile.release,
error_on_warnings: cmd.build_profile.error_on_warnings,
binary_outfile: cmd.build_output.bin_file.clone(),
debug_outfile: cmd.build_output.debug_file.clone(),
build_target: BuildTarget::default(),
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 @@ -167,6 +167,7 @@ fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
build_target: BuildTarget::default(),
build_profile: cmd.build_profile.build_profile.clone(),
release: cmd.build_profile.release,
error_on_warnings: cmd.build_profile.error_on_warnings,
time_phases: cmd.print.time_phases,
binary_outfile: cmd.build_output.bin_file.clone(),
debug_outfile: cmd.build_output.debug_file.clone(),
Expand Down
3 changes: 3 additions & 0 deletions forc-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub struct Opts {
///
/// If --build-profile is also provided, forc omits this flag and uses provided build-profile.
pub release: bool,
/// Should warnings be treated as errors?
pub error_on_warnings: bool,
/// Output the time elapsed over each part of the compilation process.
pub time_phases: bool,
}
Expand Down Expand Up @@ -262,6 +264,7 @@ impl Opts {
build_target: self.build_target,
build_profile: self.build_profile,
release: self.release,
error_on_warnings: self.error_on_warnings,
time_phases: self.time_phases,
tests: true,
const_inject_map,
Expand Down
18 changes: 13 additions & 5 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,25 @@ pub fn print_on_success_library(terse_mode: bool, proj_name: &str, warnings: &[C

pub fn print_on_failure(terse_mode: bool, warnings: &[CompileWarning], errors: &[CompileError]) {
let e_len = errors.len();
let w_len = warnings.len();

if !terse_mode {
warnings.iter().for_each(format_warning);
errors.iter().for_each(format_err);
}

println_red_err(&format!(
" Aborting due to {} {}.",
e_len,
if e_len > 1 { "errors" } else { "error" }
));
if e_len == 0 && w_len > 0 {
println_red_err(&format!(
" Aborting. {} warning(s) treated as error(s).",
warnings.len()
));
} else {
println_red_err(&format!(
" Aborting due to {} {}.",
e_len,
if e_len > 1 { "errors" } else { "error" }
));
}
}

fn format_err(err: &CompileError) {
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 @@ -174,6 +174,7 @@ fn opts_from_cmd(cmd: Command) -> forc_test::Opts {
},
build_profile: cmd.build.profile.build_profile,
release: cmd.build.profile.release,
error_on_warnings: cmd.build.profile.error_on_warnings,
binary_outfile: cmd.build.output.bin_file,
debug_outfile: cmd.build.output.debug_file,
build_target: cmd.build.build_target,
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 @@ -46,6 +46,9 @@ pub struct BuildProfile {
/// If --build-profile is also provided, forc omits this flag and uses provided build-profile.
#[clap(long)]
pub release: bool,
/// Treat warnings as errors.
#[clap(long)]
pub error_on_warnings: bool,
}

/// Options related to printing stages of compiler output.
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 @@ -32,6 +32,7 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
},
build_profile: cmd.build.profile.build_profile,
release: cmd.build.profile.release,
error_on_warnings: cmd.build.profile.error_on_warnings,
binary_outfile: cmd.build.output.bin_file,
debug_outfile: cmd.build.output.debug_file,
build_target: cmd.build.build_target,
Expand Down

0 comments on commit 8e797a1

Please sign in to comment.