Skip to content

Commit

Permalink
Avoid builds-without-the-bytes bug on Windows (#2919)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
UebelAndre authored Oct 5, 2024
1 parent 884a7a2 commit 81d925c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
8 changes: 8 additions & 0 deletions cargo/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//rust:defs.bzl", "rust_binary")

rust_binary(
name = "copy_file",
srcs = ["copy_file.rs"],
edition = "2021",
visibility = ["//visibility:public"],
)

bzl_library(
name = "bzl_lib",
Expand Down
30 changes: 25 additions & 5 deletions cargo/private/cargo_build_script.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,26 @@ def _cargo_build_script_runfiles_impl(ctx):

is_windows = script.extension == "exe"
exe = ctx.actions.declare_file("{}{}".format(ctx.label.name, ".exe" if is_windows else ""))
ctx.actions.symlink(
output = exe,
target_file = script,
is_executable = True,
)

# Avoid the following issue on Windows when using builds-without-the-bytes.
# https://github.com/bazelbuild/bazel/issues/21747
if is_windows:
args = ctx.actions.args()
args.add(script)
args.add(exe)

ctx.actions.run(
executable = ctx.executable._copy_file,
arguments = [args],
inputs = [script],
outputs = [exe],
)
else:
ctx.actions.symlink(
output = exe,
target_file = script,
is_executable = True,
)

# Tools are ommitted here because they should be within the `script`
# attribute's runfiles.
Expand Down Expand Up @@ -95,6 +110,11 @@ https://github.com/bazelbuild/bazel/issues/15486
allow_files = True,
cfg = "exec",
),
"_copy_file": attr.label(
cfg = "exec",
executable = True,
default = Label("//cargo/private:copy_file"),
),
},
executable = True,
)
Expand Down
20 changes: 20 additions & 0 deletions cargo/private/copy_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! A tool for copying files and avoiding
//! https://github.com/bazelbuild/bazel/issues/21747

use std::env;
use std::fs;
use std::path::PathBuf;

fn main() {
let src = PathBuf::from(std::env::args().nth(1).expect("No source file provided"));
let dest = PathBuf::from(env::args().nth(2).expect("No destination provided"));

fs::copy(&src, &dest).unwrap_or_else(|e| {
panic!(
"Failed to copy file `{} -> {}`\n{:?}",
src.display(),
dest.display(),
e
)
});
}

0 comments on commit 81d925c

Please sign in to comment.