Skip to content

Commit 5c177f3

Browse files
committed
Auto merge of #6898 - ehuss:doc-auto-discovery, r=alexcrichton
Update documentation for auto-discovery. The old documentation was slightly wrong, and didn't really go into much detail. This adds some elaboration.
2 parents bb8a583 + eed6bdc commit 5c177f3

File tree

1 file changed

+44
-7
lines changed

1 file changed

+44
-7
lines changed

src/doc/src/reference/manifest.md

+44-7
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,6 @@ Cargo will also treat any files located in `src/bin/*.rs` as executables. If you
648648
executable consists of more than just one source file, you might also use a directory
649649
inside `src/bin` containing a `main.rs` file which will be treated as an executable
650650
with a name of the parent directory.
651-
Do note, however, once you add a `[[bin]]` section ([see
652-
below](#configuring-a-target)), Cargo will no longer automatically build files
653-
located in `src/bin/*.rs`. Instead you must create a `[[bin]]` section for
654-
each file you want to build.
655651

656652
Your package can optionally contain folders named `examples`, `tests`, and
657653
`benches`, which Cargo will treat as containing examples,
@@ -685,6 +681,11 @@ package, you should remember to use Rust's module system, which you can read
685681
about in [the
686682
book](../book/ch07-00-managing-growing-projects-with-packages-crates-and-modules.html).
687683

684+
See [Configuring a target](#configuring-a-target) below for more details on
685+
manually configuring target settings. See [Target
686+
auto-discovery](#target-auto-discovery) below for more information on
687+
controlling how Cargo automatically infers targets.
688+
688689
### Examples
689690

690691
Files located under `examples` are example uses of the functionality provided by
@@ -801,9 +802,45 @@ name = "my-cool-binary"
801802
path = "src/my-cool-binary.rs"
802803
```
803804

804-
The `[package]` also includes the optional `autobins`, `autoexamples`,
805-
`autotests`, and `autobenches` keys to explicitly opt-in or opt-out of
806-
auto-discovering specific target kinds.
805+
#### Target auto-discovery
806+
807+
By default, Cargo automatically determines the targets to build based on the
808+
[layout of the files](#the-project-layout) on the filesystem. The target
809+
configuration tables, such as `[lib]`, `[[bin]]`, `[[test]]`, `[[bench]]`, or
810+
`[[example]]`, can be used to add additional targets that don't follow the
811+
standard directory layout.
812+
813+
The automatic target discovery can be disabled so that only manually
814+
configured targets will be built. Setting the keys `autobins`, `autoexamples`,
815+
`autotests`, or `autobenches` to `false` in the `[package]` section will
816+
disable auto-discovery of the corresponding target type.
817+
818+
Disabling automatic discovery should only be needed for specialized
819+
situations. For example, if you have a library where you want a *module* named
820+
`bin`, this would present a problem because Cargo would usually attempt to
821+
compile anything in the `bin` directory as an executable. Here is a sample
822+
layout of this scenario:
823+
824+
```
825+
├── Cargo.toml
826+
└── src
827+
   ├── lib.rs
828+
   └── bin
829+
      └── mod.rs
830+
```
831+
832+
To prevent Cargo from inferring `src/bin/mod.rs` as an executable, set
833+
`autobins = false` in `Cargo.toml` to disable auto-discovery:
834+
835+
```toml
836+
[package]
837+
#
838+
autobins = false
839+
```
840+
841+
> **Note**: For packages with the 2015 edition, the default for auto-discovery
842+
> is `false` if at least one target is manually defined in `Cargo.toml`.
843+
> Beginning with the 2018 edition, the default is always `true`.
807844
808845
#### The `required-features` field (optional)
809846

0 commit comments

Comments
 (0)