Skip to content

Commit 9cbd047

Browse files
committed
Doc changes to cover how to use external crates in doctests (and relation to rustdoc)
1 parent 9532dbc commit 9cbd047

File tree

6 files changed

+59
-183
lines changed

6 files changed

+59
-183
lines changed

guide/src/SUMMARY.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
- [Installation](guide/installation.md)
88
- [Reading Books](guide/reading.md)
99
- [Creating a Book](guide/creating.md)
10-
- [Writing Code Samples](guide/writing.md)
1110

1211
# Reference Guide
1312

guide/src/cli/test.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ of code samples that could get outdated as the language evolves. Therefore it is
88
them to be able to automatically test these code examples.
99

1010
mdBook supports a `test` command that will run code samples as doc tests for your book. At
11-
the moment, only Rust doc tests are supported.
12-
13-
For details on writing code samples and runnable code samples in your book, see [Writing](../guide/writing.md).
11+
the moment, only Rust doc tests are supported.
1412

1513
#### Specify a directory
1614

@@ -21,10 +19,22 @@ instead of the current working directory.
2119
mdbook test path/to/book
2220
```
2321

24-
#### `--library-path`
22+
#### `--dest-dir`
23+
24+
The `--dest-dir` (`-d`) option allows you to change the output directory for the
25+
book. Relative paths are interpreted relative to the book's root directory. If
26+
not specified it will default to the value of the `build.build-dir` key in
27+
`book.toml`, or to `./book`.
28+
29+
#### `--chapter`
30+
31+
The `--chapter` (`-c`) option allows you to test a specific chapter of the
32+
book using the chapter name or the relative path to the chapter.
33+
34+
#### `--library-path` `[`deprecated`]`
2535

26-
> Note: This argument doesn't provide sufficient information for current Rust compilers.
27-
Instead, add `package-dir` to your ***book.toml***, as described in [configuration](/format/configuration/general.md#rust-options).
36+
> Note: This argument doesn't provide sufficient extern crate information to run doc tests in current Rust compilers.
37+
Instead, add **manifest** to point to a **Cargo.toml** file in your ***book.toml***, as described in [rust configuration](/format/configuration/general.html#rust-options).
2838

2939

3040
The `--library-path` (`-L`) option allows you to add directories to the library
@@ -41,15 +51,3 @@ mdbook test my-book -L target/debug/deps/
4151

4252
See the `rustdoc` command-line [documentation](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#-l--library-path-where-to-look-for-dependencies)
4353
for more information.
44-
45-
#### `--dest-dir`
46-
47-
The `--dest-dir` (`-d`) option allows you to change the output directory for the
48-
book. Relative paths are interpreted relative to the book's root directory. If
49-
not specified it will default to the value of the `build.build-dir` key in
50-
`book.toml`, or to `./book`.
51-
52-
#### `--chapter`
53-
54-
The `--chapter` (`-c`) option allows you to test a specific chapter of the
55-
book using the chapter name or the relative path to the chapter.

guide/src/format/configuration/general.md

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ authors = ["John Doe"]
1111
description = "The example book covers examples."
1212

1313
[rust]
14-
edition = "2018"
14+
manifest = "./Cargo.toml"
1515

1616
[build]
1717
build-dir = "my-example-book"
@@ -30,14 +30,24 @@ limit-results = 15
3030

3131
## Supported configuration options
3232

33-
It is important to note that **any** relative path specified in the
33+
> Note: **any** relative path specified in the
3434
configuration will always be taken relative from the root of the book where the
3535
configuration file is located.
3636

3737
### General metadata
3838

3939
This is general information about your book.
4040

41+
```toml
42+
[book]
43+
title = "Example book"
44+
authors = ["John Doe", "Jane Doe"]
45+
description = "The example book covers examples."
46+
src = "my-src" # source files in `root/my-src` instead of `root/src`
47+
language = "en"
48+
text-direction = "ltr"
49+
```
50+
4151
- **title:** The title of the book
4252
- **authors:** The author(s) of the book
4353
- **description:** A description for the book, which is added as meta
@@ -50,44 +60,25 @@ This is general information about your book.
5060
- **text-direction**: The direction of text in the book: Left-to-right (LTR) or Right-to-left (RTL). Possible values: `ltr`, `rtl`.
5161
When not specified, the text direction is derived from the book's `language` attribute.
5262

53-
**book.toml**
54-
```toml
55-
[book]
56-
title = "Example book"
57-
authors = ["John Doe", "Jane Doe"]
58-
description = "The example book covers examples."
59-
src = "my-src" # the source files will be found in `root/my-src` instead of `root/src`
60-
language = "en"
61-
text-direction = "ltr"
62-
```
63-
6463
### Rust options
6564

6665
Options for the Rust language, relevant to running tests and playground
6766
integration.
6867

6968
```toml
7069
[rust]
71-
package-dir = "folder/for/Cargo.toml"
72-
edition = "2015" # the default edition for code blocks
70+
manifest = "path/for/Cargo.toml"
71+
edition = "2015" # [deprecated] the default edition for code blocks
7372
```
7473

75-
- **package-dir**: Folder containing a Cargo package whose targets and dependencies
76-
you want to use in your book's code samples.
77-
It must be specified if you want to test code samples with `use` statements, even if
78-
there is a `Cargo.toml` in the folder containing the `book.toml`.
79-
This can be a relative path, relative to the folder containing `book.toml`.
80-
81-
- **edition**: Rust edition to use by default for the code snippets. Default
82-
is `"2015"`. Individual code blocks can be controlled with the `edition2015`,
83-
`edition2018` or `edition2021` annotations, such as:
84-
85-
~~~text
86-
```rust,edition2015
87-
// This only works in 2015.
88-
let try = true;
89-
```
90-
~~~
74+
- **manifest**: Path to a ***Cargo.toml*** file which is used to resolve dependencies of your sample code. mdBook also uses the `package.edition` configured in the cargo project as the default for code snippets in your book.
75+
See [Using External Crates and Dependencies](/format/mdbook.html#using-external-crates-and-dependencies) for details.
76+
77+
- **edition** `[`deprecated`]`: Rust edition to use by default for the code snippets.
78+
Default is `"2015"`. Individual code blocks can be controlled with the `edition2015`,
79+
`edition2018` or `edition2021` annotations, as described in [Rust code block attributes](/format/mdbook.html#rust-code-block-attributes).
80+
This option is deprecated because it's only useful if your code samples don't depend on external crates or you're not doctest'ing them. In any case, this option cannot be specified if **manifest** is configured.
81+
9182

9283

9384
### Build options

guide/src/format/mdbook.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# mdBook-specific features
22

3+
# Features for code blocks
4+
5+
These capabilities primarily affect how the user sees or interacts with code samples in your book and are supported directly by mdBook. Some also affect documentation tests (for which mdBook invokes `rustdoc --test`): this is detailed in the sections below.
6+
37
## Hiding code lines
48

59
There is a feature in mdBook that lets you hide code lines by prepending them with a specific prefix.
@@ -306,6 +310,21 @@ And the `editable` attribute will enable the [editor] as described at [Rust code
306310

307311
[Rust Playground]: https://play.rust-lang.org/
308312

313+
## Using external crates and dependencies
314+
315+
If your code samples depend on external crates, you will probably want to include `use <crate>` statements in the code and want them to resolve and allow documentation tests to run.
316+
To configure this:
317+
318+
1. Create a ***Cargo.toml*** file with a `[package.dependencies]` section that defines a dependency for each `<crate>` you want to use in any sample. If your book is already embedded in an existing Cargo project, you may be able to use the existing project `Cargo.toml`.
319+
2. In your ***book.toml***:
320+
* configure the path to ***Cargo.toml** in `rust.manifest`, as described in [rust configuration](/format/configuration/general.html#rust-options).
321+
* remove `rust.edition` if it is configured. The edition of rust compiler will be as specified in the ***Cargo.toml***.
322+
* Refrain from invoking `mdbook test` with `-L` or `--library-path` argument. This, too, will be inferred from cargo project configuration
323+
324+
# Features for general content
325+
326+
These can be used in markdown text (outside code blocks).
327+
309328
## Controlling page \<title\>
310329

311330
A chapter can set a \<title\> that is different from its entry in the table of

guide/src/guide/writing.md

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/utils/extern_args.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ impl ExternArgs {
6767
let proj_root = cargo_path
6868
.canonicalize()?
6969
.parent()
70-
.ok_or(anyhow!("can't find parent of {:?}", cargo_path))?.to_owned();
70+
.ok_or(anyhow!("can't find parent of {:?}", cargo_path))?
71+
.to_owned();
7172
let mut manifest = Manifest::from_path(&cargo_path)?;
7273
manifest.complete_from_path(&proj_root)?; // try real hard to determine bin or lib
7374
let package = manifest

0 commit comments

Comments
 (0)