Skip to content

Commit 81d925c

Browse files
authored
Avoid builds-without-the-bytes bug on Windows (#2919)
After #2826 was merged, I started seeing flaky builds on Windows related to build script executables (#2891 (comment)). This appears to be related to bazelbuild/bazel#21747 so to avoid the issue, this change ensures that Windows build script executables are copied instead of symlinked.
1 parent 884a7a2 commit 81d925c

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

cargo/private/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load("//rust:defs.bzl", "rust_binary")
3+
4+
rust_binary(
5+
name = "copy_file",
6+
srcs = ["copy_file.rs"],
7+
edition = "2021",
8+
visibility = ["//visibility:public"],
9+
)
210

311
bzl_library(
412
name = "bzl_lib",

cargo/private/cargo_build_script.bzl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,26 @@ def _cargo_build_script_runfiles_impl(ctx):
4040

4141
is_windows = script.extension == "exe"
4242
exe = ctx.actions.declare_file("{}{}".format(ctx.label.name, ".exe" if is_windows else ""))
43-
ctx.actions.symlink(
44-
output = exe,
45-
target_file = script,
46-
is_executable = True,
47-
)
43+
44+
# Avoid the following issue on Windows when using builds-without-the-bytes.
45+
# https://github.com/bazelbuild/bazel/issues/21747
46+
if is_windows:
47+
args = ctx.actions.args()
48+
args.add(script)
49+
args.add(exe)
50+
51+
ctx.actions.run(
52+
executable = ctx.executable._copy_file,
53+
arguments = [args],
54+
inputs = [script],
55+
outputs = [exe],
56+
)
57+
else:
58+
ctx.actions.symlink(
59+
output = exe,
60+
target_file = script,
61+
is_executable = True,
62+
)
4863

4964
# Tools are ommitted here because they should be within the `script`
5065
# attribute's runfiles.
@@ -95,6 +110,11 @@ https://github.com/bazelbuild/bazel/issues/15486
95110
allow_files = True,
96111
cfg = "exec",
97112
),
113+
"_copy_file": attr.label(
114+
cfg = "exec",
115+
executable = True,
116+
default = Label("//cargo/private:copy_file"),
117+
),
98118
},
99119
executable = True,
100120
)

cargo/private/copy_file.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! A tool for copying files and avoiding
2+
//! https://github.com/bazelbuild/bazel/issues/21747
3+
4+
use std::env;
5+
use std::fs;
6+
use std::path::PathBuf;
7+
8+
fn main() {
9+
let src = PathBuf::from(std::env::args().nth(1).expect("No source file provided"));
10+
let dest = PathBuf::from(env::args().nth(2).expect("No destination provided"));
11+
12+
fs::copy(&src, &dest).unwrap_or_else(|e| {
13+
panic!(
14+
"Failed to copy file `{} -> {}`\n{:?}",
15+
src.display(),
16+
dest.display(),
17+
e
18+
)
19+
});
20+
}

0 commit comments

Comments
 (0)