Skip to content
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

Improve strategy for selecting targets to be scraped for examples #11430

Merged
merged 2 commits into from
Nov 29, 2022

Conversation

willcrichton
Copy link
Contributor

What does this PR try to resolve?

After #10343, we have identified a clear set of conditions for whether a particular target should be considered by -Zrustdoc-scrape-examples. These conditions are described more clearly in #11425.

However, after some testing with complex Cargo workspaces (e.g. wasmtime), I realized that the current approach of modifying the CompileFilter did not correctly implement this new specification. For example, a target with doc = false should not be scraped by default since it is not a documented unit, but the current approach would potentially include such a target for scraping.

This PR provides a new approach which I believe correctly implements the specification:

  1. generate_targets is called with the same parameters except the mode which becomes CompileMode::Docscrape instead of CompileMode::Doc. filter_default_targets generates the same targets for Docscrape as for Doc.
  2. Inside generate_targets, an initial set of Proposals are created. This set of proposals is extended with further proposals based on targets identified as doc-scrape-examples = true, or Example targets where possible.

This PR subsumes #11423, and also fixes #10571.

How should we test and review this PR?

I have added another test docscrape::only_scrape_documented_targets to verify that only documented or explicitly-enabled targets are included for scraping.

r? @weihanglo

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 27, 2022
Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Make sense. Thank you!

@@ -597,6 +591,7 @@ fn generate_targets(
package_set: &PackageSet<'_>,
profiles: &Profiles,
interner: &UnitInterner,
has_dev_units: HasDevUnits,
Copy link
Member

Choose a reason for hiding this comment

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

On no. The thirteenth parameter 😂

@@ -828,6 +823,52 @@ fn generate_targets(
}
}

if mode.is_doc_scrape() {
Copy link
Member

Choose a reason for hiding this comment

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

Not a blocker. Do you feel it better if we extract this piece of code to a separate function doing post-process after calling generate_targets? Doing so avoids adding more parameters on generate_targets.

Don't blindly agree with me, though 😆.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that this code needs to be in the middle of generate_targets --- it post-processes the proposals but not the units.

In my proposed refactor, I'm going to separate out generate_targets into TargetGenerator::make_proposals and TargetGenerator::proposals_to_units. Then I can cleanly insert the doc-scrape-specific code after make_proposals as post-processing.

@willcrichton
Copy link
Contributor Author

@weihanglo I share your concern that generate_targets is getting really complicated. I actually started to refactor it, but decided to wait.

If it's ok, I would like to merge this PR as-is to get the functionality into Cargo. After this is merged, I will file a second PR that refactors generate_targets into a new file cargo_compile/target_generator.rs that has a struct TargetGenerator with the mega-method factored into different documentable pieces.

Copy link
Member

@weihanglo weihanglo left a comment

Choose a reason for hiding this comment

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

Thank you for taking care of these edge cases I was not aware of!

It's fine to refactor later. Really looking forward to seeing a well-organized unit generator!

@weihanglo
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Nov 29, 2022

📌 Commit 968caae has been approved by weihanglo

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 29, 2022
@bors
Copy link
Contributor

bors commented Nov 29, 2022

⌛ Testing commit 968caae with merge d28c9b8...

@bors
Copy link
Contributor

bors commented Nov 29, 2022

☀️ Test successful - checks-actions
Approved by: weihanglo
Pushing d28c9b8 to master...

@bors bors merged commit d28c9b8 into rust-lang:master Nov 29, 2022
bors added a commit that referenced this pull request Dec 2, 2022
…anglo

Refactor generate_targets into separate module

### What does this PR try to resolve?

The `generate_targets` function is fairly complicated with an absurd number of parameters. This PR refactors the function into a `TargetGenerator` struct that represents the state of the generator, and reduces the amount of parameter-passing between the relevant functions. Additionally, the `generate_targets` function has been refactored into two smaller functions `create_proposals` and `proposals_to_units`. The docscrape-specific functionality from #11430 has been pulled out into a separate function, as promised.

### How should we test and review this PR?

This PR does not change any functionality, so no new tests are added. It should be reviewed for code style.

r? `@weihanglo`
weihanglo added a commit to weihanglo/rust that referenced this pull request Dec 3, 2022
9 commits in e027c4b5d25af2119b1956fac42863b9b3242744..f6e737b1e3386adb89333bf06a01f68a91ac5306
2022-11-25 19:44:46 +0000 to 2022-12-02 20:21:24 +0000
- Refactor generate_targets into separate module (rust-lang/cargo#11445)
- Improve file found in multiple build targets warning (rust-lang/cargo#11299)
- Error when precise without -p flag (rust-lang/cargo#11349)
- Improve strategy for selecting targets to be scraped for examples (rust-lang/cargo#11430)
- Aware of compression ratio for unpack size limit (rust-lang/cargo#11337)
- Add test for rustdoc-map generation when using sparse registries (rust-lang/cargo#11403)
- Add error message when `cargo fix` on an empty repo (rust-lang/cargo#11400)
- Store the sparse+ prefix in the SourceId for sparse registries (rust-lang/cargo#11387)
- Update documentation for -Zrustdoc-scrape-examples in the Cargo Book (rust-lang/cargo#11425)
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 3, 2022
Update cargo

9 commits in e027c4b5d25af2119b1956fac42863b9b3242744..f6e737b1e3386adb89333bf06a01f68a91ac5306
2022-11-25 19:44:46 +0000 to 2022-12-02 20:21:24 +0000
- Refactor generate_targets into separate module (rust-lang/cargo#11445)
- Improve file found in multiple build targets warning (rust-lang/cargo#11299)
- Error when precise without -p flag (rust-lang/cargo#11349)
- Improve strategy for selecting targets to be scraped for examples (rust-lang/cargo#11430)
- Aware of compression ratio for unpack size limit (rust-lang/cargo#11337)
- Add test for rustdoc-map generation when using sparse registries (rust-lang/cargo#11403)
- Add error message when `cargo fix` on an empty repo (rust-lang/cargo#11400)
- Store the sparse+ prefix in the SourceId for sparse registries (rust-lang/cargo#11387)
- Update documentation for -Zrustdoc-scrape-examples in the Cargo Book (rust-lang/cargo#11425)
@ehuss ehuss added this to the 1.67.0 milestone Dec 14, 2022
@weihanglo weihanglo added the Z-scrape-examples Nightly: rustdoc scrape examples label Sep 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. Z-scrape-examples Nightly: rustdoc scrape examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cargo panics when using -Z rustdoc-scrape-examples=examples on proc-macro crates
5 participants