diff --git a/crates/config/src/lib.rs b/crates/config/src/lib.rs index e7ec1854d4dc0..172bb1fd6000d 100644 --- a/crates/config/src/lib.rs +++ b/crates/config/src/lib.rs @@ -957,6 +957,15 @@ impl Config { self.create_project(false, true) } + /// Same as [`Self::ephemeral_project()`] but configures the project to not emit any artifacts. + pub fn solar_project(&self) -> Result, SolcError> { + let mut project = self.ephemeral_project()?; + project.update_output_selection(|selection| { + *selection = OutputSelection::common_output_selection([]); + }); + Ok(project) + } + /// Builds mapping with additional settings profiles. fn additional_settings( &self, diff --git a/crates/forge/src/cmd/eip712.rs b/crates/forge/src/cmd/eip712.rs index d227406f9e77d..595016bc5b614 100644 --- a/crates/forge/src/cmd/eip712.rs +++ b/crates/forge/src/cmd/eip712.rs @@ -1,25 +1,19 @@ use alloy_primitives::{B256, keccak256}; use clap::{Parser, ValueHint}; use eyre::Result; -use foundry_cli::{ - opts::{BuildOpts, configure_pcx}, - utils::LoadConfig, -}; +use foundry_cli::{opts::BuildOpts, utils::LoadConfig}; +use foundry_common::compile::ProjectCompiler; use serde::Serialize; -use solar::{ - parse::interface::Session, - sema::{ - Gcx, Hir, - hir::StructId, - ty::{Ty, TyKind}, - }, +use solar::sema::{ + Gcx, Hir, + hir::StructId, + ty::{Ty, TyKind}, }; use std::{ collections::BTreeMap, fmt::{Display, Formatter, Result as FmtResult, Write}, ops::ControlFlow, path::{Path, PathBuf}, - slice, }; foundry_config::impl_figment_convert!(Eip712Args, build); @@ -58,18 +52,10 @@ impl Display for Eip712Output { impl Eip712Args { pub fn run(self) -> Result<()> { let config = self.build.load_config()?; - - let mut sess = Session::builder().with_stderr_emitter().build(); - sess.dcx = sess.dcx.set_flags(|flags| flags.track_diagnostics = false); - let mut compiler = solar::sema::Compiler::new(sess); - + let project = config.solar_project()?; + let mut output = ProjectCompiler::new().files([self.target_path]).compile(&project)?; + let compiler = output.parser_mut().solc_mut().compiler_mut(); compiler.enter_mut(|compiler| -> Result<()> { - // Set up the parsing context with the project paths and sources. - let mut pcx = compiler.parse(); - configure_pcx(&mut pcx, &config, None, Some(slice::from_ref(&self.target_path)))?; - - // Parse and resolve - pcx.parse(); let Ok(ControlFlow::Continue(())) = compiler.lower_asts() else { return Ok(()) }; let gcx = compiler.gcx(); let resolver = Resolver::new(gcx); @@ -97,7 +83,13 @@ impl Eip712Args { Ok(()) })?; - eyre::ensure!(compiler.sess().dcx.has_errors().is_ok(), "errors occurred"); + // `compiler.sess()` inside of `ProjectCompileOutput` is built with `with_buffer_emitter`. + let diags = compiler.sess().dcx.emitted_diagnostics().unwrap(); + if compiler.sess().dcx.has_errors().is_err() { + eyre::bail!("{diags}"); + } else { + let _ = sh_print!("{diags}"); + } Ok(()) } diff --git a/crates/forge/tests/cli/eip712.rs b/crates/forge/tests/cli/eip712.rs index 3280b4bbd16f9..27a84f6880804 100644 --- a/crates/forge/tests/cli/eip712.rs +++ b/crates/forge/tests/cli/eip712.rs @@ -57,6 +57,9 @@ library Structs2 { cmd.forge_fuse().args(["eip712", path.to_string_lossy().as_ref()]).assert_success().stdout_eq( str![[r#" +[COMPILING_FILES] with [SOLC_VERSION] +[SOLC_VERSION] [ELAPSED] +No files changed, compilation skipped Structs.sol > Structs > Foo: - type: Foo(Bar bar)Art(uint256 id)Bar(Art art) - hash: 0x6d9b732373bd999fde4072274c752e03f7437067dd75521eb406d8edf1d30f7d @@ -179,6 +182,9 @@ library InsideLibrary { cmd.forge_fuse().args(["eip712", path.to_string_lossy().as_ref()]).assert_success().stdout_eq( str![[r#" +[COMPILING_FILES] with [SOLC_VERSION] +[SOLC_VERSION] [ELAPSED] +No files changed, compilation skipped FreeStanding: - type: FreeStanding(uint256 id,string name) - hash: 0xfb3c934b2382873277133498bde6eb3914ab323e3bef8b373ebcd423969bf1a2