Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(forge): added --json argument to forge build command #6465

Merged
merged 7 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,4 @@ revm = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-interpreter = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-precompile = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-primitives = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }

4 changes: 2 additions & 2 deletions crates/cast/bin/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl StorageArgs {
project.auto_detect = auto_detect;

// Compile
let mut out = suppress_compile(&project)?;
let mut out = suppress_compile(&project, true)?;
let artifact = {
let (_, mut artifact) = out
.artifacts()
Expand All @@ -160,7 +160,7 @@ impl StorageArgs {
let solc = Solc::find_or_install_svm_version(MIN_SOLC.to_string())?;
project.solc = solc;
project.auto_detect = false;
if let Ok(output) = suppress_compile(&project) {
if let Ok(output) = suppress_compile(&project, true) {
out = output;
let (_, new_artifact) = out
.artifacts()
Expand Down
28 changes: 21 additions & 7 deletions crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,32 +284,45 @@ pub fn compile_with_filter(
ProjectCompiler::with_filter(print_names, print_sizes, skip).compile(project)
}

/// Compiles the provided [`Project`], throws if there's any compiler error and logs whether
/// Compiles the provided [`Project`], throws if there's any compiler error (if throw err is true) and logs whether
/// compilation was successful or if there was a cache hit.
/// Doesn't print anything to stdout, thus is "suppressed".
pub fn suppress_compile(project: &Project) -> Result<ProjectCompileOutput> {
pub fn suppress_compile(project: &Project, throw_err: bool) -> Result<ProjectCompileOutput> {
let output = foundry_compilers::report::with_scoped(
&foundry_compilers::report::Report::new(NoReporter::default()),
|| project.compile(),
)?;

if output.has_compiler_errors() {
if output.has_compiler_errors() && throw_err {
eyre::bail!(output.to_string())
}

Ok(output)
}

/// Depending on whether the `skip` is empty this will [`suppress_compile_sparse`] or
/// [`suppress_compile`]
/// [`suppress_compile`] and throw if there's any compiler error
pub fn suppress_compile_with_filter(
project: &Project,
skip: Vec<SkipBuildFilter>,
) -> Result<ProjectCompileOutput> {
if skip.is_empty() {
suppress_compile(project)
suppress_compile(project, true)
} else {
suppress_compile_sparse(project, SkipBuildFilters(skip), true)
}
}

/// Depending on whether the `skip` is empty this will [`suppress_compile_sparse`] or
/// [`suppress_compile`] and does not throw if there's any compiler error
pub fn suppress_compile_with_filter_json(
project: &Project,
skip: Vec<SkipBuildFilter>,
) -> Result<ProjectCompileOutput> {
if skip.is_empty() {
suppress_compile(project, false)
} else {
suppress_compile_sparse(project, SkipBuildFilters(skip))
suppress_compile_sparse(project, SkipBuildFilters(skip), false)
}
}

Expand All @@ -321,13 +334,14 @@ pub fn suppress_compile_with_filter(
pub fn suppress_compile_sparse<F: FileFilter + 'static>(
project: &Project,
filter: F,
throw_err: bool
) -> Result<ProjectCompileOutput> {
let output = foundry_compilers::report::with_scoped(
&foundry_compilers::report::Report::new(NoReporter::default()),
|| project.compile_sparse(filter),
)?;

if output.has_compiler_errors() {
if output.has_compiler_errors() && throw_err {
eyre::bail!(output.to_string())
}

Expand Down
13 changes: 12 additions & 1 deletion crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ pub struct BuildArgs {
#[clap(flatten)]
#[serde(skip)]
pub watch: WatchArgs,

///Output the compilation errors in the json format.
/// This is useful when you want to use the output in other tools.
#[clap(long)]
#[serde(skip)]
pub json: bool,
}

impl BuildArgs {
Expand All @@ -86,7 +92,12 @@ impl BuildArgs {

let filters = self.skip.unwrap_or_default();

if self.args.silent {
if self.json {
let output = compile::suppress_compile_with_filter_json(&project, filters)?;
let json = serde_json::to_string_pretty(&output.clone().output())?;
println!("{}", json);
Ok(output)
} else if self.args.silent {
compile::suppress_compile_with_filter(&project, filters)
} else {
let compiler = ProjectCompiler::with_filter(self.names, self.sizes, filters);
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ impl CreateArgs {
let project = self.opts.project()?;
let mut output = if self.json || self.opts.silent {
// Suppress compile stdout messages when printing json output or when silent
compile::suppress_compile(&project)
compile::suppress_compile(&project, true)
} else {
compile::compile(&project, false, false)
}?;
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl InspectArgs {
*contract_path = target_path.to_string_lossy().to_string();
compile::compile_files(&project, vec![target_path], true)
} else {
compile::suppress_compile(&project)
compile::suppress_compile(&project, true)
}?;

// Find the artifact
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/script/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl ScriptArgs {

// We received `contract_name`, and need to find its file path.
let output = if self.opts.args.silent {
compile::suppress_compile(&project)
compile::suppress_compile(&project, true)
} else {
compile::compile(&project, false, false)
}?;
Expand Down
8 changes: 4 additions & 4 deletions crates/forge/bin/cmd/selectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl SelectorsSubcommands {
};

let project = build_args.project()?;
let outcome = compile::suppress_compile(&project)?;
let outcome = compile::suppress_compile(&project, true)?;
let artifacts = if all {
outcome
.into_artifacts_with_files()
Expand Down Expand Up @@ -129,7 +129,7 @@ impl SelectorsSubcommands {
*contract_path = target_path.to_string_lossy().to_string();
compile::compile_files(&first_project, vec![target_path], true)
} else {
compile::suppress_compile(&first_project)
compile::suppress_compile(&first_project, true)
}?;

// Build second project
Expand All @@ -139,7 +139,7 @@ impl SelectorsSubcommands {
*contract_path = target_path.to_string_lossy().to_string();
compile::compile_files(&second_project, vec![target_path], true)
} else {
compile::suppress_compile(&second_project)
compile::suppress_compile(&second_project, true)
}?;

// Find the artifacts
Expand Down Expand Up @@ -197,7 +197,7 @@ impl SelectorsSubcommands {

// compile the project to get the artifacts/abis
let project = build_args.project()?;
let outcome = compile::suppress_compile(&project)?;
let outcome = compile::suppress_compile(&project, true)?;
let artifacts = if let Some(contract) = contract {
let found_artifact = outcome.find_first(&contract);
let artifact = found_artifact
Expand Down
4 changes: 2 additions & 2 deletions crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ impl TestArgs {
let output = match (config.sparse_mode, self.opts.silent | self.json) {
(false, false) => compiler.compile(&project),
(true, false) => compiler.compile_sparse(&project, filter.clone()),
(false, true) => compile::suppress_compile(&project),
(true, true) => compile::suppress_compile_sparse(&project, filter.clone()),
(false, true) => compile::suppress_compile(&project, true),
(true, true) => compile::suppress_compile_sparse(&project, filter.clone(), true),
}?;
// Create test options from general project settings
// and compiler output
Expand Down