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

Detect when build.target is configured, and allow overriding it from the command line #619

Merged
merged 7 commits into from
Jan 22, 2024

Conversation

Nemo157
Copy link
Contributor

@Nemo157 Nemo157 commented Dec 30, 2023

This allows checking the correct directory for the json files. There are two more correct approaches to this, but neither are usable currently:

  1. The most correct approach would be to read the compiler-artifact messages from cargo --message-format=json. Unfortunately as of the current nightly these don't work when using --output-format=json, they have an empty list of files.
  2. The next option would be to use --out-dir to tell cargo/rustdoc to place the json file in a known directory. As of the current nightly cargo doesn't support --out-dir with cargo doc, and attempting to pass it through to rustdoc directly results in an error because it is given more than once.

So instead, we query cargo to see if the user has configured build.target through either a config file or environment variables.

fixes #586, #579

Also, because it was easy after fixing the above I added a CLI flag to allow setting which target to build for.

fixes #579 more 😁

@Nemo157
Copy link
Contributor Author

Nemo157 commented Dec 30, 2023

For some reason the with_default test is failing for me locally even before any changes, because it's building html instead of json. I hope that's just something weird with my local setup.

Figured it out, I use -Zhost-config -Ztarget-applies-to-host which causes issues with RUSTDOCFLAGS without a --target set, all of these need disabling for the tests to actually work:

env -u CARGO_UNSTABLE_BUILD_STD -u CARGO_BUILD_TARGET -u CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST -u CARGO_UNSTABLE_HOST_CONFIG cargo test --test specified_target

@Nemo157 Nemo157 force-pushed the detect-build-target branch 3 times, most recently from d118094 to 56bfbbd Compare December 30, 2023 19:13
@obi1kenobi
Copy link
Owner

Figured it out, I use -Zhost-config -Ztarget-applies-to-host which causes issues with RUSTDOCFLAGS without a --target set, all of these need disabling for the tests to actually work:

env -u CARGO_UNSTABLE_BUILD_STD -u CARGO_BUILD_TARGET -u CARGO_UNSTABLE_TARGET_APPLIES_TO_HOST -u CARGO_UNSTABLE_HOST_CONFIG cargo test --test specified_target

Might you be willing to open a PR that adds a "troubleshooting" section to our CONTRIBUTING.md doc and describes this problem and its solution? I'd imagine it would be useful to other people in the future as well.

Copy link
Owner

@obi1kenobi obi1kenobi left a comment

Choose a reason for hiding this comment

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

Looks pretty good, thank you! A couple of nitpicks and minor suggestions, and I'd be happy to merge this.

src/main.rs Outdated
Comment on lines 260 to 262
/// Which target to build the crate for, to check platform-specific APIs.
#[arg(long)]
target: Option<String>,
Copy link
Owner

Choose a reason for hiding this comment

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

Thoughts on calling this field build_target instead (+updating all other internal names to build_target), then making an alias for --target to match cargo?

Cargo seems split: it calls it --target, build.target, and CARGO_BUILD_TARGET. I'm inclined to support --target to match it in the CLI, but I feel like internally and via the lib API build_target is going to be a less confusing name. For example, we don't want it to be confused with a custom target directory, which one day might also be another Option<String> field with target in the name.

Copy link
Owner

Choose a reason for hiding this comment

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

It might also be nice to add an example of a build target in the doc comment, say e.g. x86_64-unknown-linux-gnu

src/lib.rs Outdated
@@ -299,6 +301,11 @@ impl Check {
self
}

pub fn with_target(&mut self, target: String) -> &mut Self {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: this is a public function so let's document what it does, what's the default value if this function isn't used, etc. It's okay if it mirrors some of the CLI's documentation.

src/lib.rs Outdated
@@ -37,6 +37,7 @@ pub struct Check {
release_type: Option<ReleaseType>,
current_feature_config: rustdoc_gen::FeatureConfig,
baseline_feature_config: rustdoc_gen::FeatureConfig,
target: Option<String>,
Copy link
Owner

Choose a reason for hiding this comment

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

Probably a good idea to add a field-level doc comment here, describing what the field means and what the value None means.

This allows checking the correct directory for the json files. There are
two more correct approaches to this, but neither are usable currently:

 1. The most correct approach would be to read the `compiler-artifact`
    messages from `cargo --message-format=json`. Unfortunately as of the
    current nightly these don't work when using `--output-format=json`,
    they have an empty list of files.
 2. The next option would be to use `--out-dir` to tell cargo/rustdoc to
    place the json file in a known directory. As of the current nightly
    cargo doesn't support `--out-dir` with `cargo doc`, and attempting
    to pass it through to rustdoc directly results in an error because
    it is given more than once.

So instead, we query cargo to see if the user has configured
`build.target` through either a config file or environment variables.
@obi1kenobi
Copy link
Owner

Nicely done, love how carefully you put all this together! 🚀 Thank you so much!

If you might be interested in making more PRs and want some ideas, give me a ping anytime. I've written up some of my thinking as GitHub issues, but not everything is in there yet.

@obi1kenobi obi1kenobi merged commit e21509e into obi1kenobi:main Jan 22, 2024
33 checks passed
@obi1kenobi obi1kenobi added the C-enhancement Category: raise the bar on expectations label Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: raise the bar on expectations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failure to find json file when using build.target config Support semver-checking cross-compiled crates
2 participants