Skip to content

Commit 6d6f466

Browse files
committed
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.
1 parent 04fa2f2 commit 6d6f466

File tree

2 files changed

+26
-20
lines changed

2 files changed

+26
-20
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::core::compiler::context::Metadata;
44
use crate::core::compiler::job_queue::JobState;
55
use crate::core::{profiles::ProfileRoot, PackageId};
66
use crate::util::errors::CargoResult;
7-
use crate::util::interning::InternedString;
87
use crate::util::machine_message::{self, Message};
98
use crate::util::{internal, profile};
109
use anyhow::Context as _;
@@ -270,7 +269,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
270269
}
271270
})
272271
.collect::<Vec<_>>();
273-
let pkg_name = unit.pkg.name();
272+
let crate_name = unit.target.crate_name();
274273
let pkg_descr = unit.pkg.to_string();
275274
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
276275
let id = unit.pkg.package_id();
@@ -280,7 +279,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
280279
let host_target_root = cx.files().host_dest().to_path_buf();
281280
let all = (
282281
id,
283-
pkg_name,
282+
crate_name.clone(),
284283
pkg_descr.clone(),
285284
Arc::clone(&build_script_outputs),
286285
output_file.clone(),
@@ -400,7 +399,7 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
400399
paths::write(&root_output_file, paths::path2bytes(&script_out_dir)?)?;
401400
let parsed_output = BuildOutput::parse(
402401
&output.stdout,
403-
pkg_name,
402+
crate_name,
404403
&pkg_descr,
405404
&script_out_dir,
406405
&script_out_dir,
@@ -422,12 +421,12 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
422421
// itself to run when we actually end up just discarding what we calculated
423422
// above.
424423
let fresh = Work::new(move |state| {
425-
let (id, pkg_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
424+
let (id, crate_name, pkg_descr, build_script_outputs, output_file, script_out_dir) = all;
426425
let output = match prev_output {
427426
Some(output) => output,
428427
None => BuildOutput::parse_file(
429428
&output_file,
430-
pkg_name,
429+
crate_name,
431430
&pkg_descr,
432431
&prev_script_out_dir,
433432
&script_out_dir,
@@ -479,7 +478,7 @@ fn insert_warnings_in_build_outputs(
479478
impl BuildOutput {
480479
pub fn parse_file(
481480
path: &Path,
482-
pkg_name: InternedString,
481+
crate_name: String,
483482
pkg_descr: &str,
484483
script_out_dir_when_generated: &Path,
485484
script_out_dir: &Path,
@@ -489,7 +488,7 @@ impl BuildOutput {
489488
let contents = paths::read_bytes(path)?;
490489
BuildOutput::parse(
491490
&contents,
492-
pkg_name,
491+
crate_name,
493492
pkg_descr,
494493
script_out_dir_when_generated,
495494
script_out_dir,
@@ -499,10 +498,12 @@ impl BuildOutput {
499498
}
500499

501500
// Parses the output of a script.
502-
// The `pkg_name` is used for error messages.
501+
// The `pkg_descr` is used for error messages.
502+
// The `crate_name` is used for determining if RUSTC_BOOTSTRAP should be allowed.
503503
pub fn parse(
504504
input: &[u8],
505-
pkg_name: InternedString,
505+
// Takes String instead of InternedString so passing `unit.pkg.name()` will give a compile error.
506+
crate_name: String,
506507
pkg_descr: &str,
507508
script_out_dir_when_generated: &Path,
508509
script_out_dir: &Path,
@@ -591,13 +592,12 @@ impl BuildOutput {
591592
// behavior, so still only give a warning.
592593
// NOTE: cargo only allows nightly features on RUSTC_BOOTSTRAP=1, but we
593594
// want setting any value of RUSTC_BOOTSTRAP to downgrade this to a warning
594-
// (so that `RUSTC_BOOTSTRAP=pkg_name` will work)
595+
// (so that `RUSTC_BOOTSTRAP=crate_name` will work)
595596
let rustc_bootstrap_allows = |name: &str| {
596-
std::env::var("RUSTC_BOOTSTRAP").map_or(false, |var| {
597-
var.split(',').any(|s| s == name)
598-
})
597+
std::env::var("RUSTC_BOOTSTRAP")
598+
.map_or(false, |var| var.split(',').any(|s| s == name))
599599
};
600-
if nightly_features_allowed || rustc_bootstrap_allows(&*pkg_name) {
600+
if nightly_features_allowed || rustc_bootstrap_allows(&*crate_name) {
601601
warnings.push(format!("Cannot set `RUSTC_BOOTSTRAP={}` from {}.\n\
602602
note: Crates cannot set `RUSTC_BOOTSTRAP` themselves, as doing so would subvert the stability guarantees of Rust for your project.",
603603
val, whence
@@ -610,7 +610,7 @@ impl BuildOutput {
610610
help: If you're sure you want to do this in your project, set the environment variable `RUSTC_BOOTSTRAP={}` before running cargo instead.",
611611
val,
612612
whence,
613-
pkg_name,
613+
crate_name,
614614
);
615615
}
616616
} else {
@@ -867,7 +867,7 @@ fn prev_build_output(cx: &mut Context<'_, '_>, unit: &Unit) -> (Option<BuildOutp
867867
(
868868
BuildOutput::parse_file(
869869
&output_file,
870-
unit.pkg.name(),
870+
unit.target.crate_name(),
871871
&unit.pkg.to_string(),
872872
&prev_script_out_dir,
873873
&script_out_dir,

tests/testsuite/build_script_env.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Tests for build.rs rerun-if-env-changed and rustc-env
22
3+
use cargo_test_support::basic_manifest;
34
use cargo_test_support::project;
45
use cargo_test_support::sleep_ms;
56

@@ -110,6 +111,7 @@ fn rerun_if_env_or_file_changes() {
110111
#[cargo_test]
111112
fn rustc_bootstrap() {
112113
let p = project()
114+
.file("Cargo.toml", &basic_manifest("has-dashes", "0.0.1"))
113115
.file("src/main.rs", "fn main() {}")
114116
.file(
115117
"build.rs",
@@ -122,7 +124,9 @@ fn rustc_bootstrap() {
122124
.build();
123125
p.cargo("build")
124126
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
125-
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=foo` [..]")
127+
.with_stderr_contains(
128+
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
129+
)
126130
.with_status(101)
127131
.run();
128132
p.cargo("build")
@@ -131,14 +135,16 @@ fn rustc_bootstrap() {
131135
.run();
132136
// RUSTC_BOOTSTRAP set to the name of the crate
133137
p.cargo("build")
134-
.env("RUSTC_BOOTSTRAP", "foo")
138+
.env("RUSTC_BOOTSTRAP", "has_dashes")
135139
.with_stderr_contains("warning: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
136140
.run();
137141
// RUSTC_BOOTSTRAP set to some random value
138142
p.cargo("build")
139143
.env("RUSTC_BOOTSTRAP", "bar")
140144
.with_stderr_contains("error: Cannot set `RUSTC_BOOTSTRAP=1` [..]")
141-
.with_stderr_contains("help: [..] set the environment variable `RUSTC_BOOTSTRAP=foo` [..]")
145+
.with_stderr_contains(
146+
"help: [..] set the environment variable `RUSTC_BOOTSTRAP=has_dashes` [..]",
147+
)
142148
.with_status(101)
143149
.run();
144150
}

0 commit comments

Comments
 (0)