cargo-edit icon indicating copy to clipboard operation
cargo-edit copied to clipboard

`cargo install cargo-edit --no-default-features` fails to compile

Open zacknewman opened this issue 4 months ago • 6 comments

[zack@laptop ~]$ cargo install cargo-edit --no-default-features
⋮
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `clap`
  --> /home/zack/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/cargo-edit-0.13.7/src/errors.rs:55:11
   |
55 | impl From<clap::Error> for CliError {
   |           ^^^^ use of unresolved module or unlinked crate `clap`
   |
   = help: if you wanted to use a crate named `clap`, use `cargo add clap` to add it to your `Cargo.toml`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `clap`
  --> /home/zack/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/cargo-edit-0.13.7/src/errors.rs:56:18
   |
56 |     fn from(err: clap::Error) -> CliError {
   |                  ^^^^ use of unresolved module or unlinked crate `clap`
   |
   = help: if you wanted to use a crate named `clap`, use `cargo add clap` to add it to your `Cargo.toml`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `cargo-edit` (lib) due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: failed to compile `cargo-edit v0.13.7`, intermediate artifacts can be found at `/tmp/cargo-installgFtuDU`.
To reuse those artifacts with a future compilation, set the environment variable `CARGO_TARGET_DIR` to that path.

Machine info:

[zack@laptop ~]$ uname -a
Linux laptop 6.16.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 23 Aug 2025 15:32:49 +0000 x86_64 GNU/Linux
[zack@laptop ~]$ cargo -V
cargo 1.89.0 (c24e10642 2025-06-23)

If one must enable certain features, ideally compile_error would be used informing a user the minimum set of features that must be enabled; otherwise any use of clap should be gated behind a cfg attribute based on the set of features that enable it.

zacknewman avatar Aug 27 '25 18:08 zacknewman

While this is a bug, its relatively minor in my opinion.

The big issue is that cargo does not report the error about nothing to install until after it has compiled things

$ cargo install pulldown-cmark --no-default-features
    Updating crates.io index
  Installing pulldown-cmark v0.13.0
    Updating crates.io index
     Locking 3 packages to latest compatible versions
   Compiling pulldown-cmark v0.13.0
   Compiling bitflags v2.9.3
   Compiling unicase v2.8.1
   Compiling memchr v2.7.5
    Finished `release` profile [optimized] target(s) in 2.12s
warning: none of the package's binaries are available for install using the selected features
  bin "pulldown-cmark" requires the features: `getopts`
  example "broken-link-callbacks" requires the features: `html`
  example "event-filter" requires the features: `html`
  example "events" requires the features:
  example "footnote-rewrite" requires the features: `html`
  example "normalize-wikilink" requires the features: `html`
  example "parser-map-event-print" requires the features: `html`
2 more targets also requires features not enabled. See them in the Cargo.toml file.
Consider enabling some of the needed features by passing, e.g., `--features="getopts"`

This is being tracked in rust-lang/cargo#8970

epage avatar Aug 27 '25 18:08 epage

I'm unsure I understand. I cloned the repo and added a simple #[cfg(feature = "cli")] to the mentioned From impl, and cargo build --release --no-default-features compiles just fine. Are you saying that it shouldn't compile at all if cli is not enabled or that cargo-edit is useless without at least that feature enabled? If so, then compile_error would still be appropriate in my opinion.

zacknewman avatar Aug 27 '25 18:08 zacknewman

cargo build --release --no-default-features will build but cargo install --release --no-default-features will error because all of the binaries require some feature to be enabled.

cargo-edit is a library (which is all that is left without features) is not generally a priority, see https://docs.rs/cargo-edit/latest/cargo_edit/#semver-compatibility

The right fix is adding the appropriate #[cfg]s, its just a low priority for me to do anything about it.

epage avatar Aug 27 '25 19:08 epage

OK, can you tell me what the minimum set of features are for the binary? Is cli enough?

zacknewman avatar Aug 27 '25 19:08 zacknewman

We have a feature per binary, see https://github.com/killercup/cargo-edit/blob/d722ed83f660d7848fe9e673d81ff3c294990f49/Cargo.toml#L38-L56

epage avatar Aug 27 '25 19:08 epage

You can set a compiler_error in lib.rs to short-circuit erroneous compilation:

#[cfg(not(feature = "cli"))]
{
    compile_error!("At least 1 feature is required to install");
}

This would help avoid duplicate issues in the future and provide a more meaningful error message.

Please disregard if this crate is intended to be a lib crate as well as a binary crate.

2bndy5 avatar Sep 11 '25 20:09 2bndy5