Skip to content

Extern crates support - #3238

Open
pczarn wants to merge 11 commits into
rust-lang:mainfrom
pczarn:crates
Open

pczarn wants to merge 11 commits into
rust-lang:mainfrom
pczarn:crates

Conversation

@pczarn

@pczarn pczarn commented Oct 18, 2026 •

Copy link
Copy Markdown

Fixes #706
Fixes #394

Supersedes #2503
Supersedes #3137

Allows doc snippets to freely depend on external crates.

The key magical incantation is using cargo with the right arguments. Those are message-format set to json, as well as the RUSTDOC command set to echo, which skips the compile process and outputs two sets of information about the compile process to our captured stdout.

Hopefully this is not too much of a hack, and saves a couple hundred lines of effort.

no-ai-icon-02(1) no-ai-icon-04

@rustbot rustbot added the S-waiting-on-review Status: waiting on a review label Oct 18, 2026
.current_dir(&self.root)
.arg("test")
.arg("--doc")
.args(["--message-format=json", "--config", "build.rustdoc='echo'"]);

@pczarn pczarn Oct 18, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key magical incantation is using cargo with the right arguments. Those are message-format set to json, as well as the RUSTDOC command set to echo, which outputs two sets of information about the compile process to our captured stdout.

View changes since the review

#[non_exhaustive]
pub struct RustConfig {
/// Rust edition used in playground
/// DEPRECATED

@GuillaumeGomez GuillaumeGomez Oct 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pczarn pczarn Oct 19, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this has some consequences for good and bad

Good: philosophy of TIOWTDI: There Is One Way To Do It. Everyone places it in the same file.

Bad: you need an extra file two extra files just to declare the edition.

let chapters = book
.iter()
.filter_map(get_chapter)
.map(|(chapter, path)| (path.clone(), chapter.name.clone(), chapter.content.clone()))

@GuillaumeGomez GuillaumeGomez Oct 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I missed something, I don't think you need to clone anything here.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.map(|(chapter, path)| (path.clone(), chapter.name.clone(), chapter.content.clone()))
book.items.into_iter().filter_map(get_chapter).map(|(chapter, path)| (path, chapter.name, chapter.content))

Like that?

Because the items field is public.

Doesn't make that much of a difference, because this piece is not performance-critical. Thanks,

let output = cargo.output().ok()?;

if !output.status.success() {
// failed = true;

@GuillaumeGomez GuillaumeGomez Oct 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuillaumeGomez That leftover shouldn't be there. I am removing it, because this line would be for tests, not cargo.

@GuillaumeGomez GuillaumeGomez left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are quite a lot of changes which seem unrelated to what you're trying to achieve. As far as I can see, only the cargo command update is needed, right?

View changes since this review

@rustbot rustbot added S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author. and removed S-waiting-on-review Status: waiting on a review labels Oct 19, 2026
@rustbot

rustbot commented Oct 19, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@pczarn

pczarn commented Oct 19, 2026 •

Copy link
Copy Markdown
Author

@GuillaumeGomez Thanks for the review.

There is indeed a bunch of changes, which are all necessary because they allow us to keep old deprecated or remaining functionality while adding the new functionality through Cargo manifests. Some parts are there just to massage the arguments into correctness.

Half of the changes just kind of are for testing, documentation etc. So they may seem unrelated while they are all necessary.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

@GuillaumeGomez Some of this code is there to, for example, permit you to jump to error location in your editor. This works perfectly well unless you include program content inline. I checked. Which is also why there is some extra code.

@GuillaumeGomez

Copy link
Copy Markdown
Member

@GuillaumeGomez Some of this code is there to, for example, permit you to jump to error location in your editor. This works perfectly well unless you include program content inline. I checked. Which is also why there is some extra code.

Except it's not what this feature is about. Remove it.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

@GuillaumeGomez I don't see why an improvement should be removed if all it takes is a few lines. It should be okay to refactor or make small functional changes to the same method you are currently working on.

@GuillaumeGomez

Copy link
Copy Markdown
Member

No, please keep your PRs focused on one topic. If you have other improvements in mind you are welcome to open separate PRs.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

@GuillaumeGomez Okay, sure, thanks. What I am thinking this code will break in the future in case someone changes the arguments from cargo to rustdoc, so it needs some way to ensure the cargo project does not break this. What I am saying there is already one instance of whitespace in the arguments and we need some way to ensure no further whitespace appears within command line arguments, or that they are successfully removed.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

This one is --check-cfg 'cfg(feature, values())' so echo removes the single quotes and we have a space in between feature, and values

@GuillaumeGomez

Copy link
Copy Markdown
Member

Then it's not the right approach. You can't really guess what the correct way to parse is unless you reimplement the binary CLI.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

@GuillaumeGomez Mmmmmm, yes. I think I can fix this. With a few tiny changes to cargo. When new cargo version is released, the code would work on top of it, and otherwise keep the echo.

@pczarn

pczarn commented Oct 19, 2026

Copy link
Copy Markdown
Author

@GuillaumeGomez I just don't want to add hundreds of lines of code here. This change is a net addition of just 230 lines.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: The marked PR is awaiting some action (such as code changes) from the PR author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to reference 3rd-party crates mdbook test extern crate functionality

3 participants