Skip to content

Commit

Permalink
Fail fast and more helpfully on invalid compile argument (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay authored Feb 15, 2022
1 parent a5bda9c commit 421f06b
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use std::ffi::{OsStr, OsString};
use std::fmt::{self, Display};
use std::fs;
use std::io::{self, BufRead, BufReader, Read, Write};
use std::path::{Path, PathBuf};
use std::path::{Component, Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
Expand Down Expand Up @@ -139,6 +139,8 @@ enum ErrorKind {
ToolExecError,
/// Error occurred due to missing external tools.
ToolNotFound,
/// One of the function arguments failed validation.
InvalidArgument,
}

/// Represents an internal error that occurred, with an explanation.
Expand Down Expand Up @@ -943,6 +945,17 @@ impl Build {
///
/// This will return a result instead of panicing; see compile() for the complete description.
pub fn try_compile(&self, output: &str) -> Result<(), Error> {
let mut output_components = Path::new(output).components();
match (output_components.next(), output_components.next()) {
(Some(Component::Normal(_)), None) => {}
_ => {
return Err(Error::new(
ErrorKind::InvalidArgument,
"argument of `compile` must be a single normal path component",
));
}
}

let (lib_name, gnu_lib_name) = if output.starts_with("lib") && output.ends_with(".a") {
(&output[3..output.len() - 2], output.to_owned())
} else {
Expand Down

0 comments on commit 421f06b

Please sign in to comment.