Skip to content

Commit d84cc49

Browse files
authored
Add patches making rust analyzer crates deterministic (#3929)
Kind of related to #3547 but not directly a `-sys` crate. Regardless, these crates are non deterministic and these patches make them deterministic.
1 parent b1f5672 commit d84cc49

6 files changed

Lines changed: 76 additions & 0 deletions

File tree

tools/rust_analyzer/3rdparty/BUILD.bazel

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ load("//crate_universe:defs.bzl", "crate", "crates_vendor")
33

44
crates_vendor(
55
name = "crates_vendor",
6+
annotations = {
7+
"io-lifetimes": [
8+
crate.annotation(
9+
patches = ["@rules_rust//tools/rust_analyzer/3rdparty/patches:io-lifetimes-determinism.patch"],
10+
),
11+
],
12+
"rustix": [
13+
crate.annotation(
14+
patches = ["@rules_rust//tools/rust_analyzer/3rdparty/patches:rustix-determinism.patch"],
15+
),
16+
],
17+
},
618
cargo_lockfile = "Cargo.Bazel.lock",
719
mode = "remote",
820
packages = {

tools/rust_analyzer/3rdparty/crates/defs.bzl

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(glob(include = ["*.patch"]))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Crate Patches
2+
3+
Patches in this folder are used for patching crates in `../crates`, e.g. to fix non deterministic behavior.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
diff --git build.rs build.rs
2+
index 2fc2a38..5a01d04 100644
3+
--- build.rs
4+
+++ build.rs
5+
@@ -43,7 +43,6 @@ fn has_feature(feature: &str) -> bool {
6+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
7+
use std::process::Stdio;
8+
9+
- let out_dir = var("OUT_DIR").unwrap();
10+
let rustc = var("RUSTC").unwrap();
11+
let target = var("TARGET").unwrap();
12+
13+
@@ -68,7 +67,7 @@ fn can_compile<T: AsRef<str>>(test: T) -> bool {
14+
.arg("--target")
15+
.arg(target)
16+
.arg("--out-dir")
17+
- .arg(out_dir); // Put the output somewhere inconsequential.
18+
+ .arg("/dev/null"); // Put the output somewhere inconsequential to avoid non-determinism.
19+
20+
// If Cargo wants to set RUSTFLAGS, use that.
21+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
diff --git build.rs build.rs
2+
index 320b3052..611bd280 100644
3+
--- build.rs
4+
+++ build.rs
5+
@@ -86,7 +86,10 @@ fn main() {
6+
// x86 our asm support requires naked functions. On PowerPC and MIPS,
7+
// Rust's inline asm is considered experimental, so only use it if
8+
// `--cfg=rustix_use_experimental_asm` is given.
9+
- if (feature_rustc_dep_of_std || vendor == "mustang" || can_compile("use std::arch::asm;"))
10+
+ // Note: On x86_64 linux, assume asm is available (any rustc >= 1.59 supports it)
11+
+ // to avoid non-deterministic probe compilation.
12+
+ let has_asm = arch == "x86_64" || feature_rustc_dep_of_std || vendor == "mustang" || can_compile("use std::arch::asm;");
13+
+ if has_asm
14+
&& (arch != "x86" || has_feature("naked_functions"))
15+
&& ((arch != "powerpc64" && arch != "mips" && arch != "mips64")
16+
|| rustix_use_experimental_asm)
17+
@@ -242,7 +245,6 @@ fn has_feature(feature: &str) -> bool {
18+
fn can_compile<T: AsRef<str>>(test: T) -> bool {
19+
use std::process::Stdio;
20+
21+
- let out_dir = var("OUT_DIR").unwrap();
22+
let rustc = var("RUSTC").unwrap();
23+
let target = var("TARGET").unwrap();
24+
25+
@@ -267,7 +269,7 @@ fn can_compile<T: AsRef<str>>(test: T) -> bool {
26+
.arg("--target")
27+
.arg(target)
28+
.arg("--out-dir")
29+
- .arg(out_dir); // Put the output somewhere inconsequential.
30+
+ .arg("/dev/null"); // Put the output somewhere inconsequential.
31+
32+
// If Cargo wants to set RUSTFLAGS, use that.
33+
if let Ok(rustflags) = var("CARGO_ENCODED_RUSTFLAGS") {

0 commit comments

Comments
 (0)