Skip to content

Commit 8523b25

Browse files
Auto merge of #149377 - jieyouxu:git-hash-availability, r=<try>
Do not trust bootstrap about git hash availability try-job: aarch64-apple try-job: test-various
2 parents 7b9905e + 39e27c1 commit 8523b25

File tree

6 files changed

+50
-27
lines changed

6 files changed

+50
-27
lines changed

src/bootstrap/src/core/build_steps/test.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,10 +2346,6 @@ Please disable assertions with `rust.debug-assertions = false`.
23462346

23472347
cmd.arg("--channel").arg(&builder.config.channel);
23482348

2349-
if !builder.config.omit_git_hash {
2350-
cmd.arg("--git-hash");
2351-
}
2352-
23532349
let git_config = builder.config.git_config();
23542350
cmd.arg("--nightly-branch").arg(git_config.nightly_branch);
23552351
cmd.arg("--git-merge-commit-email").arg(git_config.git_merge_commit_email);

src/ci/run.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ fi
110110
export RUST_RELEASE_CHANNEL=$(releaseChannel)
111111
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --release-channel=$RUST_RELEASE_CHANNEL"
112112

113+
# Always set `RUSTC_TEST_GIT_HASH` to run
114+
# `tests/run-make/version-verbose-commit-hash` under CI conditions (for jobs
115+
# that run tests).
116+
export RUSTC_TEST_GIT_HASH=1
117+
113118
if [ "$DEPLOY$DEPLOY_ALT" = "1" ]; then
114119
if [[ "$CI_JOB_NAME" == *ohos* ]]; then
115120
RUST_CONFIGURE_ARGS="$RUST_CONFIGURE_ARGS --disable-llvm-static-stdcpp"

src/doc/rustc-dev-guide/src/tests/misc.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,21 @@ fn main() {
3838
.run();
3939
}
4040
```
41+
42+
## `RUSTC_TEST_GIT_HASH` env var
43+
44+
> Context:
45+
>
46+
> - <https://github.com/rust-lang/rust/issues/132875>
47+
> - <https://github.com/rust-lang/rust/issues/132845>
48+
49+
`RUSTC_TEST_GIT_HASH` is a special env var used intentionally to bypass
50+
bootstrap to make sure that the built rustc and rustdoc binaries correctly
51+
report git hash information. We can't rely on information reported by bootstrap
52+
regarding git hash availability because the bootstrap logic itself can be wrong
53+
(see linked issues above).
54+
55+
- This env var must be set in CI.
56+
- To run `tests/run-make/version-verbose-commit-hash` locally, set
57+
`RUSTC_TEST_GIT_HASH` to any non-empty value. Otherwise, this test will be
58+
skipped.

src/tools/compiletest/src/common.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,10 +632,9 @@ pub struct Config {
632632
pub channel: String,
633633

634634
/// Whether adding git commit information such as the commit hash has been enabled for building.
635-
///
636-
/// FIXME: `compiletest` cannot trust `bootstrap` for this information, because `bootstrap` can
637-
/// have bugs and had bugs on that logic. We need to figure out how to obtain this e.g. directly
638-
/// from CI or via git locally.
635+
/// Note that this information directly comes from an env var `RUSTC_TEST_GIT_HASH` set by
636+
/// CI (or in the local environment), since we must assume bootstrap might report incorrect
637+
/// git hash availability due to bugs, and thus we need to bypass bootstrap.
639638
pub git_hash: bool,
640639

641640
/// The default Rust edition.

src/tools/compiletest/src/directives/tests.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ struct ConfigBuilder {
9999
stage: Option<u32>,
100100
stage_id: Option<String>,
101101
llvm_version: Option<String>,
102-
git_hash: bool,
103102
system_llvm: bool,
104103
profiler_runtime: bool,
105104
rustc_debug_assertions: bool,
@@ -147,11 +146,6 @@ impl ConfigBuilder {
147146
self
148147
}
149148

150-
fn git_hash(&mut self, b: bool) -> &mut Self {
151-
self.git_hash = b;
152-
self
153-
}
154-
155149
fn system_llvm(&mut self, s: bool) -> &mut Self {
156150
self.system_llvm = s;
157151
self
@@ -218,9 +212,6 @@ impl ConfigBuilder {
218212
args.push(llvm_version.clone());
219213
}
220214

221-
if self.git_hash {
222-
args.push("--git-hash".to_owned());
223-
}
224215
if self.system_llvm {
225216
args.push("--system-llvm".to_owned());
226217
}
@@ -422,11 +413,13 @@ fn debugger() {
422413

423414
#[test]
424415
fn git_hash() {
425-
let config: Config = cfg().git_hash(false).build();
426-
assert!(check_ignore(&config, "//@ needs-git-hash"));
427-
428-
let config: Config = cfg().git_hash(true).build();
416+
unsafe { std::env::set_var("RUSTC_TEST_GIT_HASH", "1") };
417+
let config = cfg().build();
429418
assert!(!check_ignore(&config, "//@ needs-git-hash"));
419+
420+
unsafe { std::env::remove_var("RUSTC_TEST_GIT_HASH") };
421+
let config = cfg().build();
422+
assert!(check_ignore(&config, "//@ needs-git-hash"));
430423
}
431424

432425
#[test]

src/tools/compiletest/src/lib.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ use crate::executor::{CollectedTest, ColorConfig};
5151
/// some code here that inspects environment variables or even runs executables
5252
/// (e.g. when discovering debugger versions).
5353
fn parse_config(args: Vec<String>) -> Config {
54+
// FIXME(jieyouxu): this says "parse_config", but in reality also in some cases read from
55+
// env vars!
56+
5457
let mut opts = Options::new();
5558
opts.reqopt("", "compile-lib-path", "path to host shared libraries", "PATH")
5659
.reqopt("", "run-lib-path", "path to target shared libraries", "PATH")
@@ -180,11 +183,6 @@ fn parse_config(args: Vec<String>) -> Config {
180183
.optflag("", "profiler-runtime", "is the profiler runtime enabled for this target")
181184
.optflag("h", "help", "show this message")
182185
.reqopt("", "channel", "current Rust channel", "CHANNEL")
183-
.optflag(
184-
"",
185-
"git-hash",
186-
"run tests which rely on commit version being compiled into the binaries",
187-
)
188186
.optopt("", "edition", "default Rust edition", "EDITION")
189187
.reqopt("", "nightly-branch", "name of the git branch for nightly", "BRANCH")
190188
.reqopt(
@@ -370,6 +368,19 @@ fn parse_config(args: Vec<String>) -> Config {
370368
let build_test_suite_root = opt_path(matches, "build-test-suite-root");
371369
assert!(build_test_suite_root.starts_with(&build_root));
372370

371+
// NOTE: we cannot trust bootstrap for git hash availability, therefore we use an env var to
372+
// communicate directly between compiletest and CI/local environment, bypassing bootstrap.
373+
let git_hash = if build_helper::ci::CiEnv::is_ci() {
374+
// In CI environment, we expect that the env var must be set to prevent it from being forgotten.
375+
let _ = env::var_os("RUSTC_TEST_GIT_HASH")
376+
.expect("`RUSTC_TEST_GIT_HASH` must be set under CI environments");
377+
true
378+
} else {
379+
// But locally, we don't want to do so -- only run the test that requires git hash
380+
// availability if the user explicitly sets the env var.
381+
env::var_os("RUSTC_TEST_GIT_HASH").is_some()
382+
};
383+
373384
Config {
374385
bless: matches.opt_present("bless"),
375386
fail_fast: matches.opt_present("fail-fast")
@@ -455,9 +466,10 @@ fn parse_config(args: Vec<String>) -> Config {
455466
has_html_tidy,
456467
has_enzyme,
457468
channel: matches.opt_str("channel").unwrap(),
458-
git_hash: matches.opt_present("git-hash"),
459469
edition: matches.opt_str("edition").as_deref().map(parse_edition),
460470

471+
git_hash,
472+
461473
cc: matches.opt_str("cc").unwrap(),
462474
cxx: matches.opt_str("cxx").unwrap(),
463475
cflags: matches.opt_str("cflags").unwrap(),

0 commit comments

Comments
 (0)