-
Notifications
You must be signed in to change notification settings - Fork 13k
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
[rustdoc] Add --extract-doctests
command-line flag
#134531
base: master
Are you sure you want to change the base?
Conversation
That was quick, thanks a lot! @rustbot label A-rust-for-linux |
This comment has been minimized.
This comment has been minimized.
ff979fd
to
0b006d4
Compare
This comment has been minimized.
This comment has been minimized.
0b006d4
to
486acda
Compare
This comment has been minimized.
This comment has been minimized.
486acda
to
67a0fef
Compare
This comment has been minimized.
This comment has been minimized.
This PR modifies cc @jieyouxu |
Using this flag looks like this: | ||
|
||
```bash | ||
$ rustdoc -Zunstable-options --extract-doctests src/lib.rs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a CLI option and not an output format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I didn't think about it, it's SO MUCH better!
* File where they are located. | ||
* Line where they are located. | ||
* Codeblock attributes (more information about this [here](./write-documentation/documentation-tests.html#attributes)). | ||
|
||
The output format is JSON. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not enough documentation, even for an unstable feature. You need to say what the actual keys are, and what they do (particularly the subkeys of langstr
, which are not self-explanatory). Please include a prettified example JSON document with all of the format features used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too sure if we want everything to be documented as docblock attributes list might get longer. Well, I'll list the current one and we can think about it later.
I don't think the approach taken here (or manually implementing It means some of the internal structs are actually part of the public API, but the exact relation is unclear in source code. I think the right way to structure this is to do what
Some other things:
|
//@ compile-flags:-Z unstable-options --extract-doctests | ||
//@ normalize-stdout-test: "tests/rustdoc-ui" -> "$$DIR" | ||
//@ check-pass | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably want a test for how this interacts with:
- Implicit/explicit
fn main()
- Hidding lines with
/// # use some::path;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True although currently it's the code as defined in the documentation and no changes on rustdoc side (which I think matches better what rust for linux wants).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should provide another field with "rustdoc computed code" where hidden lines and main
function wrapping are added by rustdoc. Actually that sounds like a very good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True although currently it's the code as defined in the documentation and no changes on rustdoc side (which I think matches better what rust for linux wants).
Currently we use both hidden lines and the ?
support, i.e. the # Ok::<..., ...>(...)
syntax (which implies the fn main()
, from what I understand).
Some of that post-processing may be easy to do by users, like the hidden lines I assume, but if you walk the source or IR or similar to figure out details (like the crate attributes that you move out of main()
), then it may be harder for end users to replicate that properly without a hack. Perhaps it may be possible to export what rustdoc
figured about the test, so that users can replicate the post-processing on their side, but customized to their environment.
Having the rustdoc
computed code sounds fine since it may be enough for some use cases, but e.g. we currently need to convert on the fly the .unwrap()
in the ?
-using tests into a custom assert!
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, for instance, if rustdoc
tells us "this test uses ?
", "these are the crate attributes I would have moved", etc., then users may be able to easily and reliably construct their own wrappers and e.g. do a check instead of an .unwrap()
.
Of course, for things like crate attributes, it may be best to remove them so that the end user can re-add them where needed, so it wouldn't be the completely "unaltered" source code. But it could be an "adapted" version. Hidden lines should probably be normal lines in that adapted version.
Perhaps it makes sense to provide all those versions, i.e. the completely unaltered one for those that may want to do something complex or to render the text somewhere, the "adapted" version for customized test environments and the rustdoc
computed code for those that can use directly that. In the kernel we would use the "adapted" one, only, I think.
I think you're right, I took the easiest way which has the advantage to always keep the output up-to-date since it uses the same types for the code generation. I'll split it out.
I'm not convinced that adding an option to output to a file is needed. I have these two cases in mind:
Do you think another case I didn't think about maybe?
Good catch, gonna add it, thanks! |
I think the nicest way to get this is to unpack the “internal” types exhaustively in the internal->public conversion. This way you’re forced to think about the public repr when internals change, but they’re still seperate and easy to track.
I think the big one is build-systems, which are much more used to chaining commands together via files than stdout. |
☔ The latest upstream changes (presumably #134590) made this pull request unmergeable. Please resolve the merge conflicts. |
Part of #134529.
It was discussed with the Rust-for-Linux project recently that they needed a way to extract doctests so they can modify them and then run them more easily (look for "a way to extract doctests" here).
For now, I output most of
ScrapedDoctest
fields in JSON format withserde_json
. So it outputs the following information:cc @ojeda
r? @notriddle