From 24d4b9150b495cdf2c2e10e403aecf81fa0b16a7 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 16 Apr 2021 11:48:44 -0400 Subject: [PATCH 1/5] Don't give a hard error on RUSTC_BOOTSTRAP=crate_name --- src/cargo/core/compiler/custom_build.rs | 10 +++++++++- tests/testsuite/build_script_env.rs | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 55f628f66ed..4ea780c5477 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -587,7 +587,15 @@ impl BuildOutput { // to set RUSTC_BOOTSTRAP. // If this is a nightly build, setting RUSTC_BOOTSTRAP wouldn't affect the // behavior, so still only give a warning. - if nightly_features_allowed { + // NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we + // want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning + // (so that `RUSTC_BOOTSTRAP=pkg_name` will work) + let rustc_bootstrap_allows = |name: &str| { + std::env::var("RUSTC_BOOTSTRAP").map_or(false, |var| { + var.split(',').any(|s| s == name) + }) + }; + if nightly_features_allowed || rustc_bootstrap_allows(&*pkg_name) { warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\ note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.", val, whence diff --git a/tests/testsuite/build_script_env.rs b/tests/testsuite/build_script_env.rs index f03a3024a5a..01dc6452ee4 100644 --- a/tests/testsuite/build_script_env.rs +++ b/tests/testsuite/build_script_env.rs @@ -129,4 +129,16 @@ fn rustc_bootstrap() { .masquerade_as_nightly_cargo() .with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .run(); + // RUSTC_BOOTSTRAP set to the name of the crate + p.cargo("build") + .env("RUSTC_BOOTSTRAP", "foo") + .with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]") + .run(); + // RUSTC_BOOTSTRAP set to some random value + p.cargo("build") + .env("RUSTC_BOOTSTRAP", "bar") + .with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]") + .with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=foo` [..]") + .with_status(101) + .run(); } From 156254c99f47288010b16d46d54f3f02f1487259 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 16 Apr 2021 12:24:20 -0400 Subject: [PATCH 2/5] Use the crate name instead of the package name This doesn't work because it uses the name of the build script, which is always build_script_build. I'm not sure what to change it to - the name of the library crate could be different than the name of the package, and there could be multiple different crates being compiled in the same package. --- src/cargo/core/compiler/custom_build.rs | 34 ++++++++++++------------- tests/testsuite/build_script_env.rs | 12 ++++++--- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 4ea780c5477..9d45ab41fc6 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -4,7 +4,6 @@ use crate::core::compiler::context::Metadata; use crate::core::compiler::job_queue::JobState; use crate::core::{profiles::ProfileRoot, PackageId}; use crate::util::errors::{CargoResult, CargoResultExt}; -use crate::util::interning::InternedString; use crate::util::machine_message::{self, Message}; use crate::util::{self, internal, paths, profile}; use cargo_platform::Cfg; @@ -268,7 +267,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { } }) .collect::>(); - let pkg_name = unit.pkg.name(); + let crate_name = unit.target.crate_name(); let pkg_descr = unit.pkg.to_string(); let build_script_outputs = Arc::clone(&cx.build_script_outputs); let id = unit.pkg.package_id(); @@ -278,7 +277,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { let host_target_root = cx.files().host_dest().to_path_buf(); let all = ( id, - pkg_name, + crate_name.clone(), pkg_descr.clone(), Arc::clone(&build_script_outputs), output_file.clone(), @@ -398,7 +397,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?; let parsed_output = BuildOutput::parse( &output.stdout, - pkg_name, + crate_name, &pkg_descr, &script_out_dir, &script_out_dir, @@ -420,12 +419,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { // itself to run when we actually end up just discarding what we calculated // above. let fresh = Work::new(move |state| { - let (id, pkg_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all; + let (id, crate_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all; let output = match prev_output { Some(output) => output, None => BuildOutput::parse_file( &output_file, - pkg_name, + crate_name, &pkg_descr, &prev_script_out_dir, &script_out_dir, @@ -477,7 +476,7 @@ fn insert_warnings_in_build_outputs( impl BuildOutput { pub fn parse_file( path: &Path, - pkg_name: InternedString, + crate_name: String, pkg_descr: &str, script_out_dir_when_generated: &Path, script_out_dir: &Path, @@ -487,7 +486,7 @@ impl BuildOutput { let contents = paths::read_bytes(path)?; BuildOutput::parse( &contents, - pkg_name, + crate_name, pkg_descr, script_out_dir_when_generated, script_out_dir, @@ -497,10 +496,12 @@ impl BuildOutput { } // Parses the output of a script. - // The `pkg_name` is used for error messages. + // The `pkg_descr` is used for error messages. + // The `crate_name` is used for determining if RUSTC_BOOTSTRAP should be allowed. pub fn parse( input: &[u8], - pkg_name: InternedString, + // Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error. + crate_name: String, pkg_descr: &str, script_out_dir_when_generated: &Path, script_out_dir: &Path, @@ -589,13 +590,12 @@ impl BuildOutput { // behavior, so still only give a warning. // NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we // want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning - // (so that `RUSTC_BOOTSTRAP=pkg_name` will work) + // (so that `RUSTC_BOOTSTRAP=crate_name` will work) let rustc_bootstrap_allows = |name: &str| { - std::env::var("RUSTC_BOOTSTRAP").map_or(false, |var| { - var.split(',').any(|s| s == name) - }) + std::env::var("RUSTC_BOOTSTRAP") + .map_or(false, |var| var.split(',').any(|s| s == name)) }; - if nightly_features_allowed || rustc_bootstrap_allows(&*pkg_name) { + if nightly_features_allowed || rustc_bootstrap_allows(&*crate_name) { warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\ note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.", val, whence @@ -608,7 +608,7 @@ impl BuildOutput { help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.", val, whence, - pkg_name, + crate_name, ); } } else { @@ -865,7 +865,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option Date: Sat, 17 Apr 2021 11:31:48 -0400 Subject: [PATCH 3/5] Use the library name to decide whether the override should be allowed from the top-level. If there's no library, give a hard error unless features are unconditionally allowed with RUSTC_BOOTSTRAP=1. --- src/cargo/core/compiler/custom_build.rs | 46 +++++++++++++++++-------- tests/testsuite/build_script_env.rs | 43 +++++++++++++++++------ 2 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 9d45ab41fc6..86f00a84dda 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -267,7 +267,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { } }) .collect::>(); - let crate_name = unit.target.crate_name(); + let library_name = unit + .pkg + .targets() + .iter() + .find(|t| t.is_lib()) + .map(|t| t.crate_name()); let pkg_descr = unit.pkg.to_string(); let build_script_outputs = Arc::clone(&cx.build_script_outputs); let id = unit.pkg.package_id(); @@ -277,7 +282,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { let host_target_root = cx.files().host_dest().to_path_buf(); let all = ( id, - crate_name.clone(), + library_name.clone(), pkg_descr.clone(), Arc::clone(&build_script_outputs), output_file.clone(), @@ -397,7 +402,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { paths::write(&root_output_file, util::path2bytes(&script_out_dir)?)?; let parsed_output = BuildOutput::parse( &output.stdout, - crate_name, + library_name, &pkg_descr, &script_out_dir, &script_out_dir, @@ -419,12 +424,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { // itself to run when we actually end up just discarding what we calculated // above. let fresh = Work::new(move |state| { - let (id, crate_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all; + let (id, library_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all; let output = match prev_output { Some(output) => output, None => BuildOutput::parse_file( &output_file, - crate_name, + library_name, &pkg_descr, &prev_script_out_dir, &script_out_dir, @@ -476,7 +481,7 @@ fn insert_warnings_in_build_outputs( impl BuildOutput { pub fn parse_file( path: &Path, - crate_name: String, + library_name: Option, pkg_descr: &str, script_out_dir_when_generated: &Path, script_out_dir: &Path, @@ -486,7 +491,7 @@ impl BuildOutput { let contents = paths::read_bytes(path)?; BuildOutput::parse( &contents, - crate_name, + library_name, pkg_descr, script_out_dir_when_generated, script_out_dir, @@ -497,11 +502,11 @@ impl BuildOutput { // Parses the output of a script. // The `pkg_descr` is used for error messages. - // The `crate_name` is used for determining if RUSTC_BOOTSTRAP should be allowed. + // The `library_name` is used for determining if RUSTC_BOOTSTRAP should be allowed. pub fn parse( input: &[u8], // Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error. - crate_name: String, + library_name: Option, pkg_descr: &str, script_out_dir_when_generated: &Path, script_out_dir: &Path, @@ -590,12 +595,21 @@ impl BuildOutput { // behavior, so still only give a warning. // NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we // want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning - // (so that `RUSTC_BOOTSTRAP=crate_name` will work) - let rustc_bootstrap_allows = |name: &str| { + // (so that `RUSTC_BOOTSTRAP=library_name` will work) + let rustc_bootstrap_allows = |name: Option<&str>| { + let name = match name { + // as of 2021, no binaries on crates.io use RUSTC_BOOTSTRAP, so + // fine-grained opt-outs aren't needed. end-users can always use + // RUSTC_BOOTSTRAP=1 from the top-level if it's really a problem. + None => return false, + Some(n) => n, + }; std::env::var("RUSTC_BOOTSTRAP") .map_or(false, |var| var.split(',').any(|s| s == name)) }; - if nightly_features_allowed || rustc_bootstrap_allows(&*crate_name) { + if nightly_features_allowed + || rustc_bootstrap_allows(library_name.as_deref()) + { warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\ note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.", val, whence @@ -608,7 +622,7 @@ impl BuildOutput { help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.", val, whence, - crate_name, + library_name.as_deref().unwrap_or("1"), ); } } else { @@ -865,7 +879,11 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option Date: Sat, 17 Apr 2021 11:33:51 -0400 Subject: [PATCH 4/5] minor cleanup --- src/cargo/core/compiler/custom_build.rs | 13 ++----------- src/cargo/core/package.rs | 4 ++++ 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/cargo/core/compiler/custom_build.rs b/src/cargo/core/compiler/custom_build.rs index 86f00a84dda..e6d284123d0 100644 --- a/src/cargo/core/compiler/custom_build.rs +++ b/src/cargo/core/compiler/custom_build.rs @@ -267,12 +267,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult { } }) .collect::>(); - let library_name = unit - .pkg - .targets() - .iter() - .find(|t| t.is_lib()) - .map(|t| t.crate_name()); + let library_name = unit.pkg.library().map(|t| t.crate_name()); let pkg_descr = unit.pkg.to_string(); let build_script_outputs = Arc::clone(&cx.build_script_outputs); let id = unit.pkg.package_id(); @@ -879,11 +874,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option &[Target] { self.manifest().targets() } + /// Gets the library crate for this package, if it exists. + pub fn library(&self) -> Option<&Target> { + self.targets().iter().find(|t| t.is_lib()) + } /// Gets the current package version. pub fn version(&self) -> &Version { self.package_id().version() From 2dbeda8b82576bbce4cdefe6fbe00ae4d8f7096c Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 17 Apr 2021 11:59:41 -0400 Subject: [PATCH 5/5] Fix tests --- tests/testsuite/build_script_env.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tests/testsuite/build_script_env.rs b/tests/testsuite/build_script_env.rs index d4cb7ca2d7f..17c729bc9c1 100644 --- a/tests/testsuite/build_script_env.rs +++ b/tests/testsuite/build_script_env.rs @@ -128,9 +128,12 @@ fn rustc_bootstrap() { ) .with_status(101) .run(); - // RUSTC_BOOTSTRAP unset on nightly should warn + // nightly should warn whether or not RUSTC_BOOTSTRAP is set p.cargo("build") .masquerade_as_nightly_cargo() + // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc + // (this matters when tests are being run with a beta or stable cargo) + .env("RUSTC_BOOTSTRAP", "1") .with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .run(); // RUSTC_BOOTSTRAP set to the name of the library should warn @@ -151,21 +154,22 @@ fn rustc_bootstrap() { // Tests for binaries instead of libraries let p = project() .file("Cargo.toml", &basic_manifest("foo", "0.0.1")) - .file("src/main.rs", "#![feature(rustc_attrs)] fn main()") + .file("src/main.rs", "#![feature(rustc_attrs)] fn main() {}") .file("build.rs", build_rs) .build(); - // RUSTC_BOOTSTRAP unconditionally set when there's no library should warn + // nightly should warn when there's no library whether or not RUSTC_BOOTSTRAP is set p.cargo("build") .masquerade_as_nightly_cargo() + // NOTE: uses RUSTC_BOOTSTRAP so it will be propagated to rustc + // (this matters when tests are being run with a beta or stable cargo) + .env("RUSTC_BOOTSTRAP", "1") .with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]") .run(); // RUSTC_BOOTSTRAP conditionally set when there's no library should error (regardless of the value) p.cargo("build") .env("RUSTC_BOOTSTRAP", "foo") .with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]") - .with_stderr_does_not_contain( - "help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]", - ) + .with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=1` [..]") .with_status(101) .run(); }