Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message when a library is not found #158

Merged
merged 4 commits into from
Dec 20, 2023

Conversation

Finchiedev
Copy link
Contributor

I've noticed that people trying to build a crate which links against a missing library can sometimes be confused by this library's error output. This PR introduces a re-worked error message for that case that focuses on communicating:

  • What's gone wrong?
  • How can I diagnose the cause?
  • What can I do to fix this?

Before (trimmed build.rs output for libsodium-sys-stable):

--- stderr
thread 'main' panicked at build.rs:128:13:
Error: `PKG_CONFIG_ALLOW_SYSTEM_CFLAGS="1" PKG_CONFIG_ALLOW_SYSTEM_LIBS="1" "pkg-config" "--libs" "--cflags" "libsodium"` did not exit successfully: exit status: 1
error: could not find system library 'libsodium' required by the 'libsodium-sys-stable' crate

--- stderr
Package libsodium was not found in the pkg-config search path.
Perhaps you should add the directory containing `libsodium.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libsodium' found

After:

--- stderr
pkg-config exited with status code 1
> PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=1 PKG_CONFIG_ALLOW_SYSTEM_LIBS=1 pkg-config --libs --cflags libsodium

The system library `libsodium` required by crate `libsodium-sys-stable` was not found.
The file `libsodium.pc` needs to be installed and the PKG_CONFIG_PATH environment variable must contain its parent directory.
PKG_CONFIG_PATH contains the following:
    - /nix/store/7yx2i1wm89zhypky0l0lmmpl10yf0bh7-openssl-3.0.12-dev/lib/pkgconfig
    - /nix/store/fak09pxs42fqcgngf2yznw83f88kmw7s-zlib-1.3-dev/lib/pkgconfig

HINT: you may need to install a package such as libsodium, libsodium-dev or libsodium-devel.

Thanks to everyone for their invaluable feedback on zulip: https://rust-lang.zulipchat.com/#narrow/stream/351149-t-libs.2Fcrates/topic/More.20helpful.20pkg-config.20output

If there's anything that should be changed please let me know :)

Mostly just small tweaks to existing output, including the following new output:
- Potential packages to install
- Items in PKG_CONFIG_PATH
- Command invocation
@Finchiedev
Copy link
Contributor Author

Finchiedev commented Dec 17, 2023

Missed some features introduced in 1.57; specifically Command::get_{env, args, program}. Will write a solution and push the fix.

…bles

Before, this was accomplished by using `get_env` and `get_args` manually, but these features are unavailable in 1.30 (current MSRV). To get around this, `Command` is wrapped as `WrappedCommand` and tracks any environment variables or arguments that are set by the caller. Then, the `Display` implementation actually handles pretty-printing the invocation, with the added benefit that other errors can take advantage of this nicer output!
src/lib.rs Outdated
let hint = if let Some(search_path) = search_path {
writeln!(
f,
"PKG_CONFIG_PATH contains the following:\n{}",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this confusing for NixOS users? Maybe remember from which variable the search path comes and print that here.

Looks good to me otherwise, thanks a lot :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch! Will write a quick fix.

As pointed out by @sdroege in rust-lang#158, Nix users would be misled when
`PKG_CONFIG_PATH_FOR_TARGET` is listed in error output but mistakenly
named `PKG_CONFIG_PATH`.
@Finchiedev
Copy link
Contributor Author

Apologies for the commit spam, I'm very out of practice using Git! Please let me know if there is anything else I can do.

@sdroege
Copy link
Collaborator

sdroege commented Dec 20, 2023

Thanks :)

@sdroege sdroege merged commit 9e485f0 into rust-lang:master Dec 20, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants