Skip to content

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

@jonhoo

Description

@jonhoo

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]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions