Set exit code for CLI errors

Fixes #1241
This commit is contained in:
Laurenz 2023-05-22 15:15:38 +02:00
parent 88553fe3c0
commit 0214569f3a

View File

@ -1,12 +1,13 @@
mod args; mod args;
mod trace; mod trace;
use std::cell::{RefCell, RefMut}; use std::cell::{Cell, RefCell, RefMut};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{self, File}; use std::fs::{self, File};
use std::hash::Hash; use std::hash::Hash;
use std::io::{self, Write}; use std::io::{self, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::ExitCode;
use atty::Stream; use atty::Stream;
use clap::Parser; use clap::Parser;
@ -33,8 +34,12 @@ use crate::args::{CliArguments, Command, CompileCommand};
type CodespanResult<T> = Result<T, CodespanError>; type CodespanResult<T> = Result<T, CodespanError>;
type CodespanError = codespan_reporting::files::Error; type CodespanError = codespan_reporting::files::Error;
thread_local! {
static EXIT: Cell<ExitCode> = Cell::new(ExitCode::SUCCESS);
}
/// Entry point. /// Entry point.
fn main() { fn main() -> ExitCode {
let arguments = CliArguments::parse(); let arguments = CliArguments::parse();
let _guard = match crate::trace::init_tracing(&arguments) { let _guard = match crate::trace::init_tracing(&arguments) {
Ok(guard) => guard, Ok(guard) => guard,
@ -52,8 +57,16 @@ fn main() {
}; };
if let Err(msg) = res { if let Err(msg) = res {
set_failed();
print_error(&msg).expect("failed to print error"); print_error(&msg).expect("failed to print error");
} }
EXIT.with(|cell| cell.get())
}
/// Ensure a failure exit code.
fn set_failed() {
EXIT.with(|cell| cell.set(ExitCode::FAILURE));
} }
/// Print an application-level error (independent from a source file). /// Print an application-level error (independent from a source file).
@ -183,11 +196,6 @@ fn compile(mut command: CompileSettings) -> StrResult<()> {
} }
if !command.watch { if !command.watch {
// Return with non-zero exit code in case of error.
if !ok {
std::process::exit(1);
}
return Ok(()); return Ok(());
} }
@ -262,6 +270,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult
// Print diagnostics. // Print diagnostics.
Err(errors) => { Err(errors) => {
set_failed();
status(command, Status::Error).unwrap(); status(command, Status::Error).unwrap();
print_diagnostics(world, *errors) print_diagnostics(world, *errors)
.map_err(|_| "failed to print diagnostics")?; .map_err(|_| "failed to print diagnostics")?;