|
| 1 | +# Rustdoc nested `include!` change |
| 2 | + |
| 3 | +🚧 The 2024 Edition has not yet been released and hence this section is still "under construction". |
| 4 | +More information may be found in the tracking issue at <https://github.com/rust-lang/rust/issues/132230>. |
| 5 | + |
| 6 | +## Summary |
| 7 | + |
| 8 | +When a doctest is included with `include_str!`, if that doctest itself also uses `include!`, `include_str!`, or `include_bytes!`, the path is resolved relative to the Markdown file, rather than to the Rust source file. |
| 9 | + |
| 10 | +## Details |
| 11 | + |
| 12 | +Prior to the 2024 edition, adding documentation with `#[doc=include_str!("path/file.md")]` didn't carry span information into any doctests in that file. As a result, if the Markdown file was in a different directory than the source, any paths included had to be specified relative to the source file. |
| 13 | + |
| 14 | +For example, consider a library crate with these files: |
| 15 | + |
| 16 | +- `Cargo.toml` |
| 17 | +- `README.md` |
| 18 | +- `src/` |
| 19 | + - `lib.rs` |
| 20 | +- `examples/` |
| 21 | + - `data.bin` |
| 22 | + |
| 23 | +Let's say that `lib.rs` contains this: |
| 24 | + |
| 25 | +```rust,ignore |
| 26 | +#![doc=include_str!("../README.md")] |
| 27 | +``` |
| 28 | + |
| 29 | +And assume this `README.md` file: |
| 30 | + |
| 31 | +````markdown |
| 32 | +``` |
| 33 | +let _ = include_bytes!("../examples/data.bin"); |
| 34 | +// ^^^ notice this |
| 35 | +``` |
| 36 | +```` |
| 37 | + |
| 38 | +Prior to the 2024 edition, the path in `README.md` needed to be relative to the `lib.rs` file. In 2024 and later, it is now relative to `README.md` itself, so we would update `README.md` to: |
| 39 | + |
| 40 | +````markdown |
| 41 | +``` |
| 42 | +let _ = include_bytes!("examples/data.bin"); |
| 43 | +``` |
| 44 | +```` |
| 45 | + |
| 46 | +## Migration |
| 47 | + |
| 48 | +There is no automatic migration to convert the paths in affected doctests. If one of your doctests is affected, you'll see an error like this after migrating to the new edition when building your tests: |
| 49 | + |
| 50 | +```text |
| 51 | +error: couldn't read `../examples/data.bin`: No such file or directory (os error 2) |
| 52 | + --> src/../README.md:2:24 |
| 53 | + | |
| 54 | +2 | let _ = include_bytes!("../examples/data.bin"); |
| 55 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 56 | + = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) |
| 57 | +help: there is a file with the same name in a different directory |
| 58 | + | |
| 59 | +2 | let _ = include_bytes!("examples/data.bin"); |
| 60 | + | ~~~~~~~~~~~~~~~~~~~ |
| 61 | +``` |
| 62 | + |
| 63 | +To migrate your doctests to Rust 2024, update any affected paths to be relative to the file containing the doctests. |
0 commit comments