Skip to content

Commit 7145ef2

Browse files
committed
cli: error exit code for process if error diagnostic was emitted
1 parent 2866207 commit 7145ef2

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

src/main.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,13 @@ fn finalize_verify_result(
482482
let (timeout, mem_limit) = (rlimit_options.timeout(), rlimit_options.mem_limit());
483483
match verify_result {
484484
#[allow(clippy::bool_to_int_with_if)]
485-
Ok(all_verified) => ExitCode::from(if all_verified { 0 } else { 1 }),
485+
Ok(all_verified) => {
486+
let server_exit_code = server.lock().unwrap().exit_code();
487+
if server_exit_code != ExitCode::SUCCESS {
488+
return server_exit_code;
489+
}
490+
ExitCode::from(if all_verified { 0 } else { 1 })
491+
}
486492
Err(VerifyError::Diagnostic(diagnostic)) => {
487493
server.lock().unwrap().add_diagnostic(diagnostic).unwrap();
488494
ExitCode::from(1)

src/servers/cli.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::{
2-
io::{self, IsTerminal},
3-
path::PathBuf,
4-
sync::{Arc, Mutex},
2+
io::{self, IsTerminal}, path::PathBuf, process::ExitCode, sync::{Arc, Mutex}
53
};
64

5+
use ariadne::ReportKind;
6+
77
use crate::{
88
ast::{Diagnostic, FileId, Files, SourceFilePath, Span, StoredFile},
99
driver::{SmtVcCheckResult, SourceUnitName},
@@ -17,13 +17,15 @@ use super::{unless_fatal_error, Server, ServerError};
1717
pub struct CliServer {
1818
werr: bool,
1919
files: Arc<Mutex<Files>>,
20+
has_emitted_errors: bool,
2021
}
2122

2223
impl CliServer {
2324
pub fn new(input_options: &InputOptions) -> Self {
2425
CliServer {
2526
werr: input_options.werr,
2627
files: Default::default(),
28+
has_emitted_errors: false,
2729
}
2830
}
2931

@@ -62,6 +64,8 @@ impl Server for CliServer {
6264
}
6365

6466
fn add_diagnostic(&mut self, diagnostic: Diagnostic) -> Result<(), VerifyError> {
67+
self.has_emitted_errors =
68+
self.has_emitted_errors || self.werr || diagnostic.kind() == ReportKind::Error;
6569
let files = self.files.lock().unwrap();
6670
print_diagnostic(&files, diagnostic)?;
6771
Ok(())
@@ -97,6 +101,14 @@ impl Server for CliServer {
97101
// Not relevant for CLI
98102
Ok(())
99103
}
104+
105+
fn exit_code(&self) -> ExitCode {
106+
if self.has_emitted_errors {
107+
ExitCode::FAILURE
108+
} else {
109+
ExitCode::SUCCESS
110+
}
111+
}
100112
}
101113

102114
fn print_diagnostic(mut files: &Files, diagnostic: Diagnostic) -> io::Result<()> {

src/servers/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
2-
error::Error,
3-
sync::{Arc, Mutex},
2+
error::Error, process::ExitCode, sync::{Arc, Mutex}
43
};
54

65
use crate::{
@@ -81,6 +80,13 @@ pub trait Server: Send {
8180

8281
/// Sends an unknown verification result for the not checked proc with the given span.
8382
fn handle_not_checked(&mut self, span: Span) -> Result<(), ServerError>;
83+
84+
/// Return an exit code for the process.
85+
///
86+
/// Default implementation returns `ExitCode::SUCCESS`.
87+
fn exit_code(&self) -> ExitCode {
88+
ExitCode::SUCCESS
89+
}
8490
}
8591

8692
fn unless_fatal_error(werr: bool, diagnostic: Diagnostic) -> Result<Diagnostic, VerifyError> {

0 commit comments

Comments
 (0)