Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extern crate #2503

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
25a3fb3
extern_args module to get proper compiler args from Cargo.
bobhy Dec 9, 2024
a166f21
Add config.rust.package-dir and fix cargo output parser (maybe not fo…
bobhy Dec 10, 2024
c926113
Force --extern <name>=<path>.rmeta to .rlib. Cargo sometimes tries t…
bobhy Dec 10, 2024
3eb20b3
Modify user guide so code samples can reference external crates.
bobhy Dec 10, 2024
96c2aee
Update guide to describe configuring book for external crates;
bobhy Dec 10, 2024
7b3a1e2
Fix CI nits.
bobhy Dec 10, 2024
138256f
Replace unstable fs::set_modified() with ad-hoc "touch" function.
bobhy Dec 11, 2024
3306d20
Fuzzy up touch file test in case sleep() is not totally precise.
bobhy Dec 11, 2024
d9d1f35
trigger rebuild if project has main.rs (but broken for other binary t…
bobhy Dec 12, 2024
6104051
Make guide into workspace child so it can share same dependencies wit…
bobhy Dec 14, 2024
cb77a89
When running build, pull dependencies from the build of the doctest c…
bobhy Dec 14, 2024
7fa966f
Compile, but do not run the preprocessor examples.
bobhy Dec 14, 2024
1703aa1
fix broken unit tests
bobhy Dec 14, 2024
9532dbc
book.tom.l must configure path to Cargo.toml, not just root folder of…
bobhy Dec 15, 2024
9cbd047
Doc changes to cover how to use external crates in doctests (and rela…
bobhy Dec 15, 2024
9c5dec2
fix fmt not caught locally
bobhy Dec 15, 2024
658221c
Document deprecating `mdbook test -L`
bobhy Dec 16, 2024
a3fc58b
fix `mdbook test <path/to/root>;
bobhy Dec 17, 2024
d62904b
revert pulldown-cmark from 0.12.2 to 0.10.0 (sigh)
bobhy Dec 17, 2024
b2eb96c
finalize doc updates for new feature
bobhy Dec 17, 2024
248f490
All doctests in mdbook project now pass.
bobhy Dec 17, 2024
38c8f5b
Provide error context if can't find or open Cargo.toml;
bobhy Dec 18, 2024
ab5b3ab
Fix unintended change, per review
bobhy Jan 2, 2025
730e11c
fix clippy nags in files I changed
bobhy Jan 2, 2025
c209369
fix unit test failure
bobhy Jan 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## mdBook 0.4.43+ (tba)

### Fixed

- Allow doctests to use external crates by referencing a `Cargo.toml`

## mdBook 0.4.43
[v0.4.42...v0.4.43](https://github.com/rust-lang/mdBook/compare/v0.4.42...v0.4.43)

Expand Down
81 changes: 80 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
[workspace]
members = [".", "examples/remove-emphasis/mdbook-remove-emphasis"]
members = [".", "examples/remove-emphasis/mdbook-remove-emphasis", "guide"]

[workspace.dependencies]
anyhow = "1.0.71"
clap = { version = "4.3.12", features = ["cargo", "wrap_help"] }
mdbook = { path = "." }
pulldown-cmark = { version = "0.10.0", default-features = false, features = [
"html",
] } # Do not update, part of the public api.
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
semver = "1.0.17"


[package]
name = "mdbook"
version = "0.4.43"
authors = [
"Mathieu David <[email protected]>",
"Michael-F-Bryan <[email protected]>",
"Matt Ickstadt <[email protected]>"
"Matt Ickstadt <[email protected]>",
]
documentation = "https://rust-lang.github.io/mdBook/index.html"
edition = "2021"
Expand All @@ -20,23 +32,24 @@ description = "Creates a book from markdown files"
rust-version = "1.74"

[dependencies]
anyhow = "1.0.71"
anyhow.workspace = true
chrono = { version = "0.4.24", default-features = false, features = ["clock"] }
clap = { version = "4.3.12", features = ["cargo", "wrap_help"] }
clap.workspace = true
clap_complete = "4.3.2"
cargo-manifest = "0.17.0"
once_cell = "1.17.1"
env_logger = "0.11.1"
handlebars = "6.0"
log = "0.4.17"
memchr = "2.5.0"
opener = "0.7.0"
pulldown-cmark = { version = "0.10.0", default-features = false, features = ["html"] } # Do not update, part of the public api.
pulldown-cmark.workspace = true
regex = "1.8.1"
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
serde.workspace = true
serde_json.workspace = true
shlex = "1.3.0"
tempfile = "3.4.0"
toml = "0.5.11" # Do not update, see https://github.com/rust-lang/mdBook/issues/2037
toml = "0.5.11" # Do not update, see https://github.com/rust-lang/mdBook/issues/2037
topological-sort = "0.2.2"

# Watch feature
Expand All @@ -59,7 +72,7 @@ ammonia = { version = "4.0.0", optional = true }
assert_cmd = "2.0.11"
predicates = "3.0.3"
select = "0.6.0"
semver = "1.0.17"
semver.workspace = true
pretty_assertions = "1.3.0"
walkdir = "2.3.3"

Expand Down
14 changes: 14 additions & 0 deletions guide/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "mdbook-book-code-samples"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow.workspace = true
clap.workspace = true
mdbook.workspace = true
pulldown-cmark.workspace = true
pulldown-cmark-to-cmark = "18.0.0"
serde.workspace = true
serde_json.workspace = true
semver.workspace = true
3 changes: 2 additions & 1 deletion guide/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ authors = ["Mathieu David", "Michael-F-Bryan"]
language = "en"

[rust]
edition = "2018"
## not needed, and will cause an error, if using Cargo.toml: edition = "2021"
manifest = "Cargo.toml"

[output.html]
smart-punctuation = true
Expand Down
61 changes: 24 additions & 37 deletions guide/src/cli/test.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
# The test command

When writing a book, you sometimes need to automate some tests. For example,
When writing a book, you may want to provide some code samples,
and it's important that these be kept accurate as your software API evolves.
For example,
[The Rust Programming Book](https://doc.rust-lang.org/stable/book/) uses a lot
of code examples that could get outdated. Therefore it is very important for
them to be able to automatically test these code examples.
of code samples that could become outdated as the language evolves.

mdBook supports a `test` command that will run all available tests in a book. At
the moment, only Rust tests are supported.
MdBook supports a `test` command which runs code samples in your book as doc tests to verify they
will compile, and, optionally, run correctly.
For details on how to specify the test to be done and outcome to be expected, see [Code Blocks](/format/mdbook.md#code-blocks).

#### Disable tests on a code block

rustdoc doesn't test code blocks which contain the `ignore` attribute:

```rust,ignore
fn main() {}
```

rustdoc also doesn't test code blocks which specify a language other than Rust:

```markdown
**Foo**: _bar_
```

rustdoc *does* test code blocks which have no language specified:

```
This is going to cause an error!
```
At the moment, mdBook only supports doc *tests* written in Rust, although code samples can be written and *displayed* in many programming languages.

#### Specify a directory

Expand All @@ -37,7 +21,22 @@ instead of the current working directory.
mdbook test path/to/book
```

#### `--library-path`
#### `--dest-dir`

The `--dest-dir` (`-d`) option allows you to change the output directory for the
book. Relative paths are interpreted relative to the book's root directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml`, or to `./book`.

#### `--chapter`

The `--chapter` (`-c`) option allows you to test a specific chapter of the
book using the chapter name or the relative path to the chapter.

#### `--library-path` `[`deprecated`]`

***Note*** This argument is deprecated. Since Rust edition 2018, the compiler needs an explicit `--extern` argument for each external crate used in a doc test, it no longer simply scans the library path for likely-looking crates.
New projects should list external crates as dependencies in a **Cargo.toml** file and reference that file in your ***book.toml***, as described in [rust configuration](/format/configuration/general.html#rust-options).

The `--library-path` (`-L`) option allows you to add directories to the library
search path used by `rustdoc` when it builds and tests the examples. Multiple
Expand All @@ -53,15 +52,3 @@ mdbook test my-book -L target/debug/deps/

See the `rustdoc` command-line [documentation](https://doc.rust-lang.org/rustdoc/command-line-arguments.html#-l--library-path-where-to-look-for-dependencies)
for more information.

#### `--dest-dir`

The `--dest-dir` (`-d`) option allows you to change the output directory for the
book. Relative paths are interpreted relative to the book's root directory. If
not specified it will default to the value of the `build.build-dir` key in
`book.toml`, or to `./book`.

#### `--chapter`

The `--chapter` (`-c`) option allows you to test a specific chapter of the
book using the chapter name or the relative path to the chapter.
25 changes: 14 additions & 11 deletions guide/src/for_developers/backends.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ a [`RenderContext::from_json()`] constructor which will load a `RenderContext`.

This is all the boilerplate necessary for our backend to load the book.

```rust
```rust,should_panic
# // this sample panics because it can't open stdin
// src/main.rs
extern crate mdbook;

use std::io;
use mdbook::renderer::RenderContext;
Expand All @@ -55,14 +55,18 @@ fn main() {

## Inspecting the Book

Now our backend has a copy of the book, lets count how many words are in each
Now our backend has a copy of the book, let's count how many words are in each
chapter!

Because the `RenderContext` contains a [`Book`] field (`book`), and a `Book` has
the [`Book::iter()`] method for iterating over all items in a `Book`, this step
turns out to be just as easy as the first.

```rust
```rust,should_panic
# // this sample panics because it can't open stdin
use std::io;
use mdbook::renderer::RenderContext;
use mdbook::book::{BookItem, Chapter};

fn main() {
let mut stdin = io::stdin();
Expand Down Expand Up @@ -174,26 +178,25 @@ deserializing to some arbitrary type `T`.
To implement this, we'll create our own serializable `WordcountConfig` struct
which will encapsulate all configuration for this backend.

First add `serde` and `serde_derive` to your `Cargo.toml`,
First add `serde` to your `Cargo.toml`,

```
$ cargo add serde serde_derive
```shell
$ cargo add serde
```

And then you can create the config struct,

```rust
extern crate serde;
#[macro_use]
extern crate serde_derive;
use serde::{Serialize, Deserialize};

...
fn main() {

#[derive(Debug, Default, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct WordcountConfig {
pub ignores: Vec<String>,
}
}
```

Now we just need to deserialize the `WordcountConfig` from our `RenderContext`
Expand Down
Loading
Loading