Skip to content

Commit d06c186

Browse files
kornelskisdroege
authored andcommitted
More verbose probe failure
1 parent 0ce2ca1 commit d06c186

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/lib.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,20 @@ pub enum Error {
122122
/// Contains the command and the cause.
123123
Command { command: String, cause: io::Error },
124124

125-
/// `pkg-config` did not exit sucessfully.
125+
/// `pkg-config` did not exit sucessfully after probing a library.
126126
///
127127
/// Contains the command and output.
128128
Failure { command: String, output: Output },
129129

130+
/// `pkg-config` did not exit sucessfully on the first attempt to probe a library.
131+
///
132+
/// Contains the command and output.
133+
ProbeFailure {
134+
name: String,
135+
command: String,
136+
output: Output,
137+
},
138+
130139
#[doc(hidden)]
131140
// please don't match on this, we're likely to add more variants over time
132141
__Nonexhaustive,
@@ -186,30 +195,46 @@ impl fmt::Display for Error {
186195
_ => write!(f, "Failed to run command `{}`, because: {}", command, cause),
187196
}
188197
}
198+
Error::ProbeFailure {
199+
ref name,
200+
ref command,
201+
ref output,
202+
} => {
203+
write!(
204+
f,
205+
"`{}` did not exit successfully: {}\nerror: could not find system library '{}' required by the '{}' crate\n",
206+
command, output.status, name, env::var("CARGO_PKG_NAME").unwrap_or_default(),
207+
)?;
208+
format_output(output, f)
209+
}
189210
Error::Failure {
190211
ref command,
191212
ref output,
192213
} => {
193-
let stdout = str::from_utf8(&output.stdout).unwrap();
194-
let stderr = str::from_utf8(&output.stderr).unwrap();
195214
write!(
196215
f,
197216
"`{}` did not exit successfully: {}",
198217
command, output.status
199218
)?;
200-
if !stdout.is_empty() {
201-
write!(f, "\n--- stdout\n{}", stdout)?;
202-
}
203-
if !stderr.is_empty() {
204-
write!(f, "\n--- stderr\n{}", stderr)?;
205-
}
206-
Ok(())
219+
format_output(output, f)
207220
}
208221
Error::__Nonexhaustive => panic!(),
209222
}
210223
}
211224
}
212225

226+
fn format_output(output: &Output, f: &mut fmt::Formatter) -> fmt::Result {
227+
let stdout = String::from_utf8_lossy(&output.stdout);
228+
if !stdout.is_empty() {
229+
write!(f, "\n--- stdout\n{}", stdout)?;
230+
}
231+
let stderr = String::from_utf8_lossy(&output.stderr);
232+
if !stderr.is_empty() {
233+
write!(f, "\n--- stderr\n{}", stderr)?;
234+
}
235+
Ok(())
236+
}
237+
213238
/// Deprecated in favor of the probe_library function
214239
#[doc(hidden)]
215240
pub fn find_library(name: &str) -> Result<Library, String> {
@@ -353,7 +378,14 @@ impl Config {
353378

354379
let mut library = Library::new();
355380

356-
let output = run(self.command(name, &["--libs", "--cflags"]))?;
381+
let output = run(self.command(name, &["--libs", "--cflags"])).map_err(|e| match e {
382+
Error::Failure { command, output } => Error::ProbeFailure {
383+
name: name.to_owned(),
384+
command,
385+
output,
386+
},
387+
other => other,
388+
})?;
357389
library.parse_libs_cflags(name, &output, self);
358390

359391
let output = run(self.command(name, &["--modversion"]))?;

0 commit comments

Comments
 (0)