Skip to content

Commit

Permalink
Print correct variable name for search path
Browse files Browse the repository at this point in the history
As pointed out by @sdroege in #158, Nix users would be misled when
`PKG_CONFIG_PATH_FOR_TARGET` is listed in error output but mistakenly
named `PKG_CONFIG_PATH`.
  • Loading branch information
Finchiedev committed Dec 19, 2023
1 parent a0640aa commit 1ac32cd
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,22 +326,25 @@ impl fmt::Display for Error {

// There will be no status code if terminated by signal
if let Some(_code) = output.status.code() {
// NixOS uses a wrapper script for pkg-config that sets the custom
// Nix uses a wrapper script for pkg-config that sets the custom
// environment variable PKG_CONFIG_PATH_FOR_TARGET
let search_path =
if let Ok(path_for_target) = env::var("PKG_CONFIG_PATH_FOR_TARGET") {
Some(path_for_target)
} else if let Ok(path) = env::var("PKG_CONFIG_PATH") {
Some(path)
} else {
None
};
let search_locations = ["PKG_CONFIG_PATH_FOR_TARGET", "PKG_CONFIG_PATH"];

// Find a search path to use
let mut search_data = None;
for location in search_locations {
if let Ok(search_path) = env::var(location) {
search_data = Some((location, search_path));
break;
}
}

// Guess the most reasonable course of action
let hint = if let Some(search_path) = search_path {
let hint = if let Some((search_location, search_path)) = search_data {
writeln!(
f,
"PKG_CONFIG_PATH contains the following:\n{}",
"{} contains the following:\n{}",
search_location,
search_path
.split(':')
.map(|path| format!(" - {}", path))
Expand All @@ -351,8 +354,13 @@ impl fmt::Display for Error {

format!("you may need to install a package such as {name}, {name}-dev or {name}-devel.", name=name)
} else {
writeln!(f, "PKG_CONFIG_PATH environment variable is not set")?;
format!("If you have installed the library, try adding its parent directory to your PATH.")
// Even on Nix, setting PKG_CONFIG_PATH seems to be a viable option
writeln!(f, "The PKG_CONFIG_PATH environment variable is not set.")?;

format!(
"if you have installed the library, try setting PKG_CONFIG_PATH to the directory containing `{}.pc`.",
name
)
};

// Try and nudge the user in the right direction so they don't get stuck
Expand Down

0 comments on commit 1ac32cd

Please sign in to comment.