|
| 1 | +# Rustdoc doctest nested `include_str!` 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 at <https://github.com/rust-lang/rust/pull/132210>. |
| 5 | + |
| 6 | +## Summary |
| 7 | + |
| 8 | +When a doctest is included with `include_str!`, if that doctest itself |
| 9 | +also uses `include!` / `include_str!` / `include_bytes!`, the path is |
| 10 | +resolved relative to the Markdown file, rather than the Rust |
| 11 | +source file. |
| 12 | + |
| 13 | +## Details |
| 14 | + |
| 15 | +Prior to the 2024 edition, adding documentation with |
| 16 | +`#[doc=include_str!("path/file.md")]` didn't carry span information |
| 17 | +into the doctests. As a result, if the Markdown file is in a different |
| 18 | +directory than the source, any `include!`ed paths need to be relative |
| 19 | +to the Rust file. |
| 20 | + |
| 21 | +For example, consider a library crate with these files: |
| 22 | + |
| 23 | +* Cargo.toml |
| 24 | +* README.md |
| 25 | +* src/ |
| 26 | + * lib.rs |
| 27 | +* examples/ |
| 28 | + * data.bin |
| 29 | + |
| 30 | +Let `lib.rs` contain this: |
| 31 | + |
| 32 | +```rust |
| 33 | +#![doc=include_str!("../README.md")] |
| 34 | +``` |
| 35 | + |
| 36 | +And assume this `README.md` file: |
| 37 | + |
| 38 | +````markdown |
| 39 | +``` |
| 40 | +let _ = include_bytes!("../examples/data.bin"); |
| 41 | +// ^^^ notice this |
| 42 | +``` |
| 43 | +```` |
| 44 | + |
| 45 | +Prior to the 2024 edition, the path in `README.md` needed to be |
| 46 | +relative to the `lib.rs` file. In 2024 and later, it is now relative |
| 47 | +to `README.md` iself. |
| 48 | + |
| 49 | +## Migration |
| 50 | + |
| 51 | +After migrating, you'll see the following error: |
| 52 | + |
| 53 | +```text |
| 54 | +error: couldn't read `../examples/data.bin`: No such file or directory (os error 2) |
| 55 | + --> src/../README.md:2:24 |
| 56 | + | |
| 57 | +2 | let _ = include_bytes!("../examples/data.bin"); |
| 58 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 59 | + = note: this error originates in the macro `include_bytes` (in Nightly builds, run with -Z macro-backtrace for more info) |
| 60 | +help: there is a file with the same name in a different directory |
| 61 | + | |
| 62 | +2 | let _ = include_bytes!("examples/data.bin"); |
| 63 | + | ~~~~~~~~~~~~~~~~~~~ |
| 64 | +``` |
0 commit comments