Skip to content

Commit 47cd856

Browse files
PronossmattssegrandizzyDaniPopes
authored
chore: update std::process::exit(0) calls in ProjectCompiler::compile (#10328)
* Update compile.rs * Update compile.rs * Update compile.rs * Update compile.rs * Reverted changes in compile.rs * Added TODO comments in compile.rs * return result --------- Co-authored-by: Matthias Seitz <[email protected]> Co-authored-by: grandizzy <[email protected]> Co-authored-by: DaniPopes <[email protected]>
1 parent c700851 commit 47cd856

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

crates/common/src/compile.rs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ impl ProjectCompiler {
157157
{
158158
self.project_root = project.root().to_path_buf();
159159

160-
// TODO: Avoid process::exit
160+
// TODO: Avoid using std::process::exit(0).
161+
// Replacing this with a return (e.g., Ok(ProjectCompileOutput::default())) would be more
162+
// idiomatic, but it currently requires a `Default` bound on `C::Language`, which
163+
// breaks compatibility with downstream crates like `foundry-cli`. This would need a
164+
// broader refactor across the call chain. Leaving it as-is for now until a larger
165+
// refactor is feasible.
161166
if !project.paths.has_input_files() && self.files.is_empty() {
162167
sh_println!("Nothing to compile")?;
163-
// nothing to do here
164168
std::process::exit(0);
165169
}
166170

@@ -204,7 +208,7 @@ impl ProjectCompiler {
204208
let quiet = self.quiet.unwrap_or(false);
205209
let bail = self.bail.unwrap_or(true);
206210

207-
let output = with_compilation_reporter(self.quiet.unwrap_or(false), || {
211+
let output = with_compilation_reporter(quiet, || {
208212
tracing::debug!("compiling project");
209213

210214
let timer = Instant::now();
@@ -229,7 +233,7 @@ impl ProjectCompiler {
229233
}
230234
}
231235

232-
self.handle_output(&output);
236+
self.handle_output(&output)?;
233237
}
234238

235239
Ok(output)
@@ -239,7 +243,7 @@ impl ProjectCompiler {
239243
fn handle_output<C: Compiler<CompilerContract = Contract>>(
240244
&self,
241245
output: &ProjectCompileOutput<C>,
242-
) {
246+
) -> Result<()> {
243247
let print_names = self.print_names.unwrap_or(false);
244248
let print_sizes = self.print_sizes.unwrap_or(false);
245249

@@ -251,17 +255,17 @@ impl ProjectCompiler {
251255
}
252256

253257
if shell::is_json() {
254-
let _ = sh_println!("{}", serde_json::to_string(&artifacts).unwrap());
258+
sh_println!("{}", serde_json::to_string(&artifacts).unwrap())?;
255259
} else {
256260
for (version, names) in artifacts {
257-
let _ = sh_println!(
261+
sh_println!(
258262
" compiler version: {}.{}.{}",
259263
version.major,
260264
version.minor,
261265
version.patch
262-
);
266+
)?;
263267
for name in names {
264-
let _ = sh_println!(" - {name}");
268+
sh_println!(" - {name}")?;
265269
}
266270
}
267271
}
@@ -270,7 +274,7 @@ impl ProjectCompiler {
270274
if print_sizes {
271275
// add extra newline if names were already printed
272276
if print_names && !shell::is_json() {
273-
let _ = sh_println!();
277+
sh_println!()?;
274278
}
275279

276280
let mut size_report =
@@ -317,19 +321,22 @@ impl ProjectCompiler {
317321
}
318322
}
319323

320-
let _ = sh_println!("{size_report}");
321-
322-
// TODO: avoid process::exit
323-
// exit with error if any contract exceeds the size limit, excluding test contracts.
324-
if size_report.exceeds_runtime_size_limit() {
325-
std::process::exit(1);
326-
}
324+
sh_println!("{size_report}")?;
327325

326+
eyre::ensure!(
327+
!size_report.exceeds_runtime_size_limit(),
328+
"some contracts exceed the runtime size limit \
329+
(EIP-170: {CONTRACT_RUNTIME_SIZE_LIMIT} bytes)"
330+
);
328331
// Check size limits only if not ignoring EIP-3860
329-
if !self.ignore_eip_3860 && size_report.exceeds_initcode_size_limit() {
330-
std::process::exit(1);
331-
}
332+
eyre::ensure!(
333+
self.ignore_eip_3860 || !size_report.exceeds_initcode_size_limit(),
334+
"some contracts exceed the initcode size limit \
335+
(EIP-3860: {CONTRACT_INITCODE_SIZE_LIMIT} bytes)"
336+
);
332337
}
338+
339+
Ok(())
333340
}
334341
}
335342

0 commit comments

Comments
 (0)