Skip to content

cargo run without --bin builds lib.rs #9235

Open
@cecton

Description

Problem

In a package with a single bin and a lib, cargo run builds lib.rs before building main.rs while cargo run --bin <name_of_the_package> builds only main.rs.

Steps

  1. I have a package with a lib.rs that I use for target WASM and a binary main.rs that I use to run a CLI. Some dependencies are only useful for the lib, some other dependencies are only useful for the bin.
  2. I use this to distinguish where I want them:
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = "^0.2"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
structopt = "0.3"
  1. When I do cargo run --bin <name_of_the_package> it builds and run successfully the CLI.
  2. When I do cargo run it tries to build lib.rs which fails because the dependencies are not available.
$ cargo run
   Compiling basic v0.1.0 (/home/cecile/repos/wasm-run/examples/basic)
error[E0433]: failed to resolve: use of undeclared crate or module `wasm_bindgen`
 --> src/lib.rs:1:5
  |
1 | use wasm_bindgen::prelude::*;
  |     ^^^^^^^^^^^^ use of undeclared crate or module `wasm_bindgen`

What I expect: running cargo run --bin <name_of_the_package> does exactly the same than cargo run if there is only one bin in the package.

I wouldn't mind too much if it was just the dependencies but this is slowing down a lot the build process as it compiles the lib in WASM for no good reason.

Possible Solution(s)

...

Notes

cargo 1.50.0 (f04e7fab7 2021-02-04)

Metadata

Assignees

No one assigned

    Labels

    A-cargo-targetsArea: selection and definition of targets (lib, bins, examples, tests, benches)C-bugCategory: bugCommand-runS-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions