cargo: contrary to manifest, cargo does not build src/bin/*.rs when toml contains [[bin]]

According to http://doc.crates.io/manifest.html#the-project-layout

Cargo will also treat any files located in src/bin/*.rs as executables.

… which is presented as an invariant. There is no mention that this is conditional.

My project started out with with a few binaries src/bin/*.rs. When src/bin/daemon.rs ballooned, I refactored it into src/bin/daemon/main.rs and organized extracted code into modules under src/bin/daemon.

Once I added a [[bin]] entry to Cargo.toml, cargo build only builds the daemon binary, and no longer adheres to the quote above.

I see two options here:

  1. Update the quoted portion of the manifest to indicate that *Cargo will treat any files located in /src/bin/*.rs as executables unless the toml contains a [[bin]] section.

  2. Modify the behavior of cargo to comply with the docs. cargo treats any files located in src/bin/*.rs that are not otherwise defined in a [[bin]] section as executables.

    a. cargo fetches list of binaries from src/bin/*.rs. b. cargo replaces any binaries from src/bin/*.rs with respective [[bin]] entries found in toml. c. cargo builds all executables in said list.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 19 (19 by maintainers)

Commits related to this issue

Most upvoted comments

Place thumbs-up/down votes for option 2 (modify cargo behavior) here.

Indeed I think this is fixed!

Oh note that this isn’t just related to the case of one binary having submodules, but also a case that sometimes you want to configure one [[bin]]. For example you might nowadays add required-features to only build some binaries when fatures are enabled. The problem with this is that even if you have the conventional layout of source files you’ve still got to specify all other [[bin]] sections because specifying any of them turns off Cargo’s auto-inference.