Skip to content

Commit f9db2f4

Browse files
jshajyn514
authored andcommitted
Update rustdoc internals
Use current paths when discussing source files. Update cheat sheet section with download-rustc. Add "use cases" section.
1 parent b6cf9a7 commit f9db2f4

File tree

1 file changed

+121
-10
lines changed

1 file changed

+121
-10
lines changed

src/rustdoc.md

+121-10
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,141 @@ does is call the `main()` that's in this crate's `lib.rs`, though.)
3636

3737
## Cheat sheet
3838

39+
* Run `./x.py setup tools` before getting started. This will configure `x.py`
40+
with nice settings for developing rustdoc and other tools, including
41+
downloading a copy of rustc rather than building it.
3942
* Use `./x.py build` to make a usable
4043
rustdoc you can run on other projects.
4144
* Add `library/test` to be able to use `rustdoc --test`.
42-
* If you've used `rustup toolchain link local /path/to/build/$TARGET/stage1`
43-
previously, then after the previous build command, `cargo +local doc` will
44-
Just Work.
45+
* Run `rustup toolchain link stage2 build/$TARGET/stage2` to add a
46+
custom toolchain called `stage2` to your rustup environment. After
47+
running that, `cargo +stage2 doc` in any directory will build with
48+
your locally-compiled rustdoc.
4549
* Use `./x.py doc library/std` to use this rustdoc to generate the
4650
standard library docs.
47-
* The completed docs will be available in `build/$TARGET/doc/std`, though the
48-
bundle is meant to be used as though you would copy out the `doc` folder to
49-
a web server, since that's where the CSS/JS and landing page are.
51+
* The completed docs will be available in `build/$TARGET/doc/std`.
52+
* If you want to copy those docs to a webserver, copy all of
53+
`build/$TARGET/doc`, since that's where the CSS, JS, fonts, and landing
54+
page are.
5055
* Use `./x.py test src/test/rustdoc*` to run the tests using a stage1 rustdoc.
5156
* See [Rustdoc internals] for more information about tests.
52-
* Most of the HTML printing code is in `html/format.rs` and `html/render.rs`.
57+
58+
## Code structure
59+
60+
* All paths in this section are relative to `src/librustdoc` in the rust-lang/rust repository.
61+
* Most of the HTML printing code is in `html/format.rs` and `html/render/mod.rs`.
5362
It's in a bunch of `fmt::Display` implementations and supplementary
5463
functions.
5564
* The types that got `Display` impls above are defined in `clean/mod.rs`, right
5665
next to the custom `Clean` trait used to process them out of the rustc HIR.
57-
* The bits specific to using rustdoc as a test harness are in `test.rs`.
66+
* The bits specific to using rustdoc as a test harness are in
67+
`doctest.rs`.
5868
* The Markdown renderer is loaded up in `html/markdown.rs`, including functions
5969
for extracting doctests from a given block of Markdown.
60-
* The tests on rustdoc *output* are located in `src/test/rustdoc`, where
70+
* The tests on the structure of rustdoc HTML output are located in `src/test/rustdoc`, where
6171
they're handled by the test runner of rustbuild and the supplementary script
6272
`src/etc/htmldocck.py`.
63-
* Tests on search index generation are located in `src/test/rustdoc-js`, as a
73+
74+
## Tests
75+
76+
* All paths in this section are relative to `src/test` in the rust-lang/rust repository.
77+
* Tests on search index generation are located in `rustdoc-js`, as a
6478
series of JavaScript files that encode queries on the standard library search
6579
index and expected results.
80+
* Tests on the "UI" of rustdoc (the terminal output it produces when run) are in
81+
`rustdoc-ui`
82+
* Tests on the "GUI" of rustdoc (the HTML, JS, and CSS as rendered in a browser)
83+
are in `rustdoc-gui`. These use a [NodeJS tool called
84+
browser-UI-test](https://github.com/GuillaumeGomez/browser-UI-test/) that uses
85+
puppeteer to run tests in a headless browser and check rendering and
86+
interactivity.
87+
88+
## Constraints
89+
90+
We try to make rustdoc work reasonably well with JavaScript disabled, and when
91+
browsing local files. We support
92+
[these browsers](https://rust-lang.github.io/rfcs/1985-tiered-browser-support.html#supported-browsers).
93+
94+
Supporting local files (`file:///` URLs) brings some surprising restrictions.
95+
Certain browser features that require secure origins, like `localStorage` and
96+
Service Workers, don't work reliably. We can still use such features but we
97+
should make sure pages are still usable without them.
98+
99+
## Multiple runs, same output directory
100+
101+
Rustdoc can be run multiple times for varying inputs, with its output set to the
102+
same directory. That's how cargo produces documentation for dependencies of the
103+
current crate. It can also be done manually if a user wants a big
104+
documentation bundle with all of the docs they care about.
105+
106+
HTML is generated independently for each crate, but there is some cross-crate
107+
information that we update as we add crates to the output directory:
108+
109+
- `crates<SUFFIX>.js` holds a list of all crates in the output directory.
110+
- `search-index<SUFFIX>.js` holds a list of all searchable items.
111+
- For each trait, there is a file under `implementors/.../trait.TraitName.js`
112+
containing a list of implementors of that trait. The implementors may be in
113+
different crates than the trait, and the JS file is updated as we discover
114+
new ones.
115+
116+
## Use cases
117+
118+
There are a few major use cases for rustdoc that you should keep in mind when
119+
working on it:
120+
121+
### Standard library docs
122+
123+
These are published at <https://doc.rust-lang.org/std> as part of the Rust release
124+
process. Stable releases are also uploaded to specific versioned URLs like
125+
<https://doc.rust-lang.org/1.57.0/std/>. Beta and nightly docs are published to
126+
<https://doc.rust-lang.org/beta/std/> and <https://doc.rust-lang.org/nightly/std/>.
127+
The docs are uploaded with the [promote-release
128+
tool](https://github.com/rust-lang/promote-release) and served from S3 with
129+
CloudFront.
130+
131+
The standard library docs contain five crates: alloc, core, proc_macro, std, and
132+
test.
133+
134+
### docs.rs
135+
136+
When crates are published to crates.io, docs.rs automatically builds
137+
and publishes their documentation, for instance at
138+
<https://docs.rs/serde/latest/serde/>. It always builds with the current nightly
139+
rustdoc, so any changes you land in rustdoc are "insta-stable" in that they will
140+
have an immediate public effect on docs.rs. Old documentation is not rebuilt, so
141+
you will see some variation in UI when browsing old releases in docs.rs. Crate
142+
authors can request rebuilds, which will be run with the latest rustdoc.
143+
144+
Docs.rs performs some transformations on rustdoc's output in order to save
145+
storage and display a navigation bar at the top. In particular, certain static
146+
files (like main.js and rustdoc.css may be shared across multiple invocations
147+
of the same version of rustdoc. Others, like crates.js and sidebar-items.js, are
148+
different for different invocations. Still others, like fonts, will never
149+
change. These categories are distinguished using the `SharedResource` enum in
150+
`src/librustdoc/html/render/write_shared.rs`
151+
152+
Documentation on docs.rs is always generated for a single crate at a time, so
153+
the search and sidebar functionality don't include dependencies of the current
154+
crate.
155+
156+
### Locally generated docs
157+
158+
Crate authors can run `cargo doc --open` in crates they have checked
159+
out locally to see the docs. This is useful to check that the docs they
160+
are writing are useful and display correctly. It can also be useful for
161+
people to view documentation on crates they aren't authors of, but want to
162+
use. In both cases, people may use `--document-private-items` Cargo flag to
163+
see private methods, fields, and so on, which are normally not displayed.
164+
165+
By default `cargo doc` will generate documentation for a crate and all of its
166+
dependencies. That can result in a very large documentation bundle, with a large
167+
(and slow) search corpus. The Cargo flag `--no-deps` inhibits that behavior and
168+
generates docs for just the crate.
169+
170+
### Self-hosted project docs
171+
172+
Some projects like to host their own documentation. For example:
173+
<https://docs.serde.rs/>. This is easy to do by locally generating docs, and
174+
simply copying them to a web server. Rustdoc's HTML output can be extensively
175+
customized by flags. Users can add a theme, set the default theme, and inject
176+
arbitrary HTML. See `rustdoc --help` for details.

0 commit comments

Comments
 (0)