Skip to content

Default to --cap-lints=warn #12

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

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,20 @@ impl SysrootBuilder {
}

/// Appends the given flag.
///
/// If no `--cap-lints` argument is configured, we will add `--cap-lints=warn`.
/// This emulates the usual behavior of Cargo: Lints are normally capped when building
/// dependencies, except that they are not capped when building path dependencies, except that
/// path dependencies are still capped if they are part of `-Zbuild-std`.
pub fn rustflag(mut self, rustflag: impl Into<OsString>) -> Self {
self.rustflags.push(rustflag.into());
self
}

/// Appends the given flags.
///
/// If no `--cap-lints` argument is configured, we will add `--cap-lints=warn`. See
/// [`SysrootBuilder::rustflag`] for more explanation.
pub fn rustflags(mut self, rustflags: impl IntoIterator<Item = impl Into<OsString>>) -> Self {
self.rustflags.extend(rustflags.into_iter().map(Into::into));
self
Expand Down Expand Up @@ -326,6 +334,25 @@ path = "lib.rs"
None => rustc_version::version_meta()?,
};

// The whole point of this crate is to build the standard library in nonstandard
// configurations, which may trip lints due to untested combinations of cfgs.
// Cargo applies --cap-lints=allow or --cap-lints=warn when handling -Zbuild-std, which we
// of course are not using:
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/mod.rs#L899
// https://github.com/rust-lang/cargo/blob/2ce45605d9db521b5fd6c1211ce8de6055fdb24e/src/cargo/core/compiler/unit.rs#L102-L109
// All the standard library crates are path dependencies, and they also sometimes pull in
// separately-maintained crates like backtrace by treating their crate roots as module
// roots. If we do not cap lints, we can get lint failures outside core or std.
// We cannot set --cap-lints=allow because Cargo needs to parse warnings to understand the
// output of --print=file-names for crate-types that the target does not support.
if !self.rustflags.iter().any(|flag| {
// FIXME: OsStr::as_encoded_bytes is cleaner here
flag.to_str()
.map_or(false, |f| f.starts_with("--cap-lints"))
}) {
self.rustflags.push("--cap-lints=warn".into());
}

// Check if we even need to do anything.
let cur_hash = self.sysroot_compute_hash(src_dir, &rustc_version);
if self.sysroot_read_hash() == Some(cur_hash) {
Expand Down