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

cargo install --bins errors if no binaries were matched with default features #10289

Closed
jonhoo opened this issue Jan 13, 2022 · 2 comments · Fixed by #10508
Closed

cargo install --bins errors if no binaries were matched with default features #10289

jonhoo opened this issue Jan 13, 2022 · 2 comments · Fixed by #10508
Labels

Comments

@jonhoo
Copy link
Contributor

jonhoo commented Jan 13, 2022

Problem

cargo install --bins currently fails with a helpful error message if trying to run cargo install on a library package

bail!(
"there is nothing to install in `{}`, because it has no binaries\n\
`cargo install` is only for installing programs, and can't be used with libraries.\n\
To use a library crate, add it as a dependency in a Cargo project instead.",
pkg
);

However, it also fails (with a less helpful error message) if trying to cargo install on a package that has binaries, but where none of them are matched by the given specifier/feature combinations:

if binaries.is_empty() {
bail!("no binaries are available for install using the selected features");
}

It's unclear to me whether this should be an error in general since we already emit a warning if no binaries are matched and since cargo succeeded at what it was asked to do ("install no binaries"). But there is a specific case in which I think Cargo exiting with an error is particularly unwanted: cargo install --bins when all binaries require non-default features. For example:

[package]
name = "ex"
version = "0.1.0"
edition = "2021"

[features]
ex = []

[[bin]]
name = "main"
path = "src/ex.rs"
required-features = ["ex"]
$ cargo install --path . --bins
  Installing ex v0.1.0 (/Users/jongje/ex)
warning: Target filter `bins` specified, but no targets matched. This is a no-op
    Finished release [optimized] target(s) in 0.00s
error: no binaries are available for install using the selected features

In this case, the user asked to install "all binaries with default features", and did not specify any "selected features", and so "install nothing" seems like the correct thing for Cargo to do, and an error seems unwarranted. It also presents a challenge for "default" build scripts (think Rust CI job templates and the like) that want to "install binaries if there are any", which cannot just blanket run cargo install --bins --path . since it will fail the job for a crate with only non-default binaries. Instead, such scripts have to both search for and resolve features to figure out whether cargo install is "safe" to run.

Steps

No response

Possible Solution(s)

My take here is that cargo install --bins should never fail due to selecting no binaries, even if the crate has no binaries in the first place. It should just warn. I think there may be an exception to a library-only crate to help newcomers who are confused about what install does in the first place, but otherwise --bins says (to me) "install all selected", which is true even if none were selected.

Notes

/cc #9576

Version

cargo 1.57.0 (b2e52d7ca 2021-10-21)
release: 1.57.0
commit-hash: b2e52d7cab0a286ee9fcc0c17510b1e72fcb53eb
commit-date: 2021-10-21
host: x86_64-apple-darwin
libgit2: 1.3.0 (sys:0.13.23 vendored)
libcurl: 7.64.1 (sys:0.4.49+curl-7.79.1 system ssl:(SecureTransport) LibreSSL/2.8.3)
os: Mac OS 11.6.2 [64-bit]
@jonhoo jonhoo added the C-bug Category: bug label Jan 13, 2022
@weihanglo
Copy link
Member

I guess we can extend this a little bit by telling user why exactly no bin is installed and how they can do to discover the required features. The warning message can be improved.

@jonhoo
Copy link
Contributor Author

jonhoo commented Jan 21, 2022

My argument here is more that this shouldn't be an error in the first place, only a warning. Since nothing actually went wrong.

bors added a commit that referenced this issue Apr 1, 2022
Don't error if no binaries were installed

### What does this PR try to resolve?

Fixes #10289, which contains a thorough description of the problem.

Briefly, if we interpret `cargo install` and `cargo install --bins` as "install
the binaries that are available", it should not be considered an error
if no binaries ended up being installed due to required features.
Instead, this should provide the user with a warning that this may not
have been what they intended.

### Additional information

Given that #9576 seems to have stalled, I figured I'd try to land this first [after all](https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/cargo.20install.20--bins.20when.20no.20binaries.20match).
@bors bors closed this as completed in a9645e1 Apr 1, 2022
Hezuikn pushed a commit to Hezuikn/cargo that referenced this issue Sep 22, 2022
If we interpret `cargo install` and `cargo install --bins` as "install
the binaries that are available", it should not be considered an error
if no binaries ended up being installed due to required features.
Instead, this should provide the user with a warning that this may not
have been what they intended.

Fixes rust-lang#10289.
Hezuikn pushed a commit to Hezuikn/cargo that referenced this issue Sep 22, 2022
If we interpret `cargo install` and `cargo install --bins` as "install
the binaries that are available", it should not be considered an error
if no binaries ended up being installed due to required features.
Instead, this should provide the user with a warning that this may not
have been what they intended.

Fixes rust-lang#10289.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants