1

Summary

I'm fairly new to Rust and decided to use it to port an existing project into it. I intended to use clap to handle CLI options, but I keep getting errors.

What do I need to do for clap to install correctly so that it's usable in my project as a dependency (e.g. extern crate clap; [...] use clap::App; [...]?

I haven't had problems with other crates (so far), so I'm not sure what's so different here or if there's a problem with the crate itself.

I've already seen a few questions (e.g. this one), which simply suggests that the dependency be added into the .toml file or don't seem to provide a solution to what I'm seeing.

I'm in Ubuntu Linux, if that makes a difference.

What I Tried

Adding clap = "2.33.0" to my Cargo.toml file (see https://crates.io/crates/clap) causes VSCode (through RLS) to log the following:

{
    "resource": "[...]/Projects/takeout/Cargo.toml",
    "owner": "rust",
    "severity": 8,
    "message": "Could not compile `clap`.\nprocess didn't exit successfully: `[...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rls --crate-name clap [...]/.cargo/registry/src/github.com-1ecc6299db9ec823/clap-2.33.0/src/lib.rs --color never --crate-type lib --emit=dep-info,metadata -C debuginfo=2 --cfg 'feature=\"ansi_term\"' --cfg 'feature=\"atty\"' --cfg 'feature=\"color\"' --cfg 'feature=\"default\"' --cfg 'feature=\"strsim\"' --cfg 'feature=\"suggestions\"' --cfg 'feature=\"vec_map\"' -C metadata=630980a214d5fd10 -C extra-filename=-630980a214d5fd10 --out-dir [...]/Projects/takeout/target/rls/debug/deps -L dependency=[...]/Projects/takeout/target/rls/debug/deps --extern ansi_term=[...]/Projects/takeout/target/rls/debug/deps/libansi_term-1510a9addefc0253.rmeta --extern atty=[...]/Projects/takeout/target/rls/debug/deps/libatty-7c4847fd9fc1e3d9.rmeta --extern bitflags=[...]/Projects/takeout/target/rls/debug/deps/libbitflags-8369a9aec15a5abb.rmeta --extern strsim=[...]/Projects/takeout/target/rls/debug/deps/libstrsim-301d1cf239e9cd24.rmeta --extern textwrap=[...]/Projects/takeout/target/rls/debug/deps/libtextwrap-a799d71e2d028df4.rmeta --extern unicode_width=[...]/Projects/takeout/target/rls/debug/deps/libunicode_width-58e38dd9d658dcfb.rmeta --extern vec_map=[...]/Projects/takeout/target/rls/debug/deps/libvec_map-4f8e59c92e9953d8.rmeta --cap-lints allow --error-format=json --sysroot [...]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu` (exit code: 101)",
    "startLineNumber": 1,
    "startColumn": 1,
    "endLineNumber": 10000,
    "endColumn": 1
}

According to the README in the clap repo itself, just adding it should work:

For full usage, add clap as a dependency in your Cargo.toml to use from crates.io:

[dependencies]
clap = "~2.33"

But it doesn't.

I've tried it with and without the ~ prefix as well as clap = {version = "2.33", features = ["yaml"]}, which is also shown in the repo, but no luck. (Yes, I'm trying to load the CLI options from a .yaml file.)

Trying cargo install clap --version 2.33.0 from the shell simply returns an error message saying: error: specified package has no binaries.

Aiming directly at the Git repo also produces the same error message:

cargo install --git https://github.com/clap-rs/clap.git --tag v2.31.2 --features yaml                                                  101 ↵
    Updating git repository `https://github.com/clap-rs/clap.git`
  Installing clap v2.31.2 (https://github.com/clap-rs/clap.git?tag=v2.31.2#07c15d28)
error: specified package has no binaries

Note that there's no v2.33.0 tag in the Git repo (at the time of this writing).


Bonus if you know how to get VSCode to stop marking everything as an error:

VSCode marks everything as an error

code_dredd
  • 5,915
  • 1
  • 25
  • 53
  • `cargo install` is only for binaries, not for libraries, so it's useless here. What happens when you run `cargo build` in your project? – Jmb Aug 20 '19 at 08:29
  • 1
    If I include `extern crate clap; ... use clap::App; ... let matches = App::from_yaml("cli.yaml").get_matches();`, a `cargo build` command with `clap = "2.33.0"` in the `.toml` file results in ```error[E0599]: no function or associated item named `from_yaml` found for type `clap::App<'_, '_>` in the current scope```. – code_dredd Aug 20 '19 at 08:38
  • @Jmb I understand that `cargo install` is only for binaries instead of libraries (after the fact, but before posting), but I would've expected the initial entry into `Cargo.toml`, under `[dependencies]` to have worked "out of the box". – code_dredd Aug 20 '19 at 08:41
  • 1
    If you want to use `App::from_yaml`, you have to [include the `yaml` feature](https://docs.rs/clap/2.33.0/clap/#opt-in-features). Does the issue happen without that code in main.rs? – E_net4 Aug 20 '19 at 08:47
  • 1
    Clap does not support yaml by default. You need to put `clap = {version = "~2.33.0", features = ["yaml"]}` in your `Cargo.toml` if you want yaml support: https://docs.rs/clap/2.33.0/clap/#optional-dependencies--features – Jmb Aug 20 '19 at 08:48
  • @Jmb I updated my `Cargo.toml` file to use `clap = { version = "2.33.0", features = ["yaml"] }` again. I was able to run `cargo build` successfully now (from the shell). I still have vscode complaining and underlining everything in red in the `Cargo.toml` file. Any suggestions to fix this completely? It seems close to a full resolution. – code_dredd Aug 20 '19 at 09:28

1 Answers1

4

cargo install

There's misunderstanding about the cargo install command. You can learn more about it here.

This command manages Cargo’s local set of installed binary crates. Only packages which have executable [[bin]] or [[example]] targets can be installed, and all executables are installed into the installation root’s bin folder.

It's not your case. The only thing you have to do is to list clap in the dependencies section (Cargo.toml). That's all. No need to use cargo install at all. cargo build, cargo run, ... commands will download & compile & statically link all dependencies.

An example

Folder structure:

.
├── Cargo.toml
└── src
    ├── cli.yaml
    └── main.rs

Current directory:

$ pwd
/Users/robertvojta/Projects/stackoverflow/clap-yaml

Cargo.toml content:

[package]
name = "clap-yaml"
version = "0.1.0"
authors = ["Zrzka"]
edition = "2018"

[dependencies]
clap = { version = "2.33.0", features = ["yaml"] }

src/cli.yaml content:

name: clap-yaml
version: "1.0"
author: Zrzka
about: Stackoverflow sample
args:
  - lang:
      short: l
      long: lang
      default_value: cz
      takes_value: true
      possible_values:
        - cz
        - en

src/main.rs content:

use clap::{App, load_yaml};

fn main() {
    let yaml = load_yaml!("cli.yaml");
    let matches = App::from_yaml(yaml).get_matches();

    match matches.value_of("lang").unwrap() {
        "cz" => println!("Ahoj"),
        "en" => println!("Hello"),
        _ => unreachable!("see possible_values in yaml, handled by clap"),
    };
}

Run it with cargo:

$ cargo -q run -- --lang en
Hello

Run it directly:

$ cargo build
    ...
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
$ target/debug/clap-yaml --lang cz
Ahoj

Visual Studio Code

I still have vscode complaining and underlining everything in red in the Cargo.toml file. Any suggestions to fix this completely? It seems close to a full resolution.

I can confirm that this problem do exist in Rust 1.34.0. I did install this version and I've got same symptoms:

  • could not compile clap
  • the whole Cargo.toml is underlined (error)

There're two ways how to fix this.

Update your Cargo.toml file dependencies section manually if you'd like to stick with Rust 1.34.0:

[dependencies]
bitflags = "=1.0.4"
clap = { version = "2.33.0", features = ["yaml"] }

Or update your Rust toolchain to >= 1.35.0.

I just tested both ways and it works.

Related issues:

zrzka
  • 20,249
  • 5
  • 47
  • 73
  • It would seem like vscode is what led me astray. I *had* added `clap = { version = "2.33.0", features = ["yaml"] }` into my `Cargo.toml` file at first, but the vscode + rls in this case mark everything in the `.toml` file as having errors. Any suggestions to fix this last detail? – code_dredd Aug 20 '19 at 09:36
  • Did update my answer. I don't have these issues. I'd guess that it can be caused by (not) installed extensions, etc. Restart VSCode and try to open your project folder again. Also I'm not VSCode user, I've got it only for checking / testing how the Rust support looks like there. Anyway, this kind of problem is out of scope for the SO. – zrzka Aug 20 '19 at 09:40
  • Even after that, I still have the `.toml` file all underlined. In any case, I've added a screenshot to the OP and accepted your answer. If you have more suggestions on that, I'd like to hear them. – code_dredd Aug 20 '19 at 09:47
  • Hover your mouse cursor over these lines and VS Code will show a popup with an explanation what's wrong and which plugin thinks it's wrong. See [this](https://www.dropbox.com/s/1hnvqm5lyru2li0/Screenshot%202019-08-20%20at%2011.51.17.png?dl=0). – zrzka Aug 20 '19 at 09:53
  • Yes, I've seen that and it only has the clap compilation error message I had included in the OP. Removing clap line from the file is what makes vscode happy. Wondering if it's a vscode issue specifically on Linux. – code_dredd Aug 20 '19 at 10:16
  • @code_dredd what's your Rust compiler version? I've found couple of issues like [this one](https://github.com/clap-rs/clap/issues/1467) or [this one](https://github.com/rust-lang/rls/issues/1449). `bitflags` crate 1.0.5 was yanked, but who knows, you can have it locally, you can have old Rust toolchain, RLS, ... – zrzka Aug 20 '19 at 13:19
  • Rust: `rustc 1.34.0 (91856ed52 2019-04-10)`; vscode `1.37.1`; RLS: `0.6.1`. I had read a bit about `bitflags`, but wasn't sure how to go about it or if that's just part of the installation and I don't need to take any action(s) on it. – code_dredd Aug 21 '19 at 01:39
  • Just FYI, the `Cargo.lock` file has the following version for `bitflags`: `version = "1.1.0"`. – code_dredd Aug 21 '19 at 04:16
  • @code_dredd thanks for the info, see my updated answer (Visual Studio Code section). I can reproduce this on macOS -> not a Linux issue. You have to either a) update Rust to >= 1.35.0, b) add `bitflags = "=1.0.4"` to the `Cargo.toml` file `dependencies` section. Both ways fixes the issue in the Visual Studio Code. – zrzka Aug 21 '19 at 06:14
  • 1
    Confirmed. Updating rust with `rustup update` gets `rustc` to version `1.37.0` and *completely* fixes the problem. Thanks for the follow up and for reproducing the issue. – code_dredd Aug 21 '19 at 06:24