bazelbuild / rules_rust

Rust rules for Bazel
https://bazelbuild.github.io/rules_rust/
Apache License 2.0
666 stars 430 forks source link

cargo build script runner fails on windows #2293

Closed rbtcollins closed 1 month ago

rbtcollins commented 11 months ago

I don't know how broadly yet, so I guess my next step is to figure out how to use a git version of rules_rust.

Here is what I know so far.

bazel build ... is trying to run build scripts for dependencies of prost for a rust_prost_library, and failing at https://github.com/bazelbuild/rules_rust/blob/b96e37ecf435d032435ad66f547a6599a16467d2/cargo/cargo_build_script_runner/lib.rs#L127-L128 with Os { code: 267, kind: NotADirectory, message: "The directory name is invalid." }

The build script runner code doesn't annotate the panic with any context such as directory, binary, etc, and as its a panic we just get the unwind stack, which in an optimised binary can be quite opaque - see below.

I couldn't see any logging or tracing telemetry in the script either, so debugging routes aren't immediately obvious.

$ bazel build --action_env=RUST_BACKTRACE=full //third-party/plugin-proto:compiler_plugin_rust_proto
INFO: Invocation ID: 8f611dc6-e3b3-4a5f-8577-facd8257401f
INFO: Analyzed target //third-party/plugin-proto:compiler_plugin_rust_proto (0 packages loaded, 0 targets configured).     
INFO: Found 1 target...                                                                                                    
ERROR: C:/bzl/external/crate_index__winapi-0.3.9/BUILD.bazel:94:19: Running Cargo build script winapi failed: (Exit 101): cargo_build_script_runner.exe failed: error executing command (from target @crate_index__winapi-0.3.9//:winapi_build_script) bazel-out\x64_windows-opt-exec-2B5CBBC6\bin\external\rules_rust\cargo\cargo_build_script_runner\cargo_build_script_runner.exe ... (remaining 11 arguments skipped)
thread 'main' panicked at external/rules_rust/cargo/cargo_build_script_runner/lib.rs:127:41:
Unable to start binary: Os { code: 267, kind: NotADirectory, message: "The directory name is invalid." }
stack backtrace:
   0:     0x7ff6614f893a - std::sys_common::backtrace::_print::impl$0::fmt
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\sys_common\backtrace.rs:44
   1:     0x7ff661513c8b - core::fmt::rt::Argument::fmt
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\fmt\rt.rs:138
   2:     0x7ff661513c8b - core::fmt::write
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\fmt\mod.rs:1114
   3:     0x7ff6614f4f01 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\io\mod.rs:1763
   4:     0x7ff6614f86ba - std::sys_common::backtrace::_print
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\sys_common\backtrace.rs:47
   5:     0x7ff6614f86ba - std::sys_common::backtrace::print
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\sys_common\backtrace.rs:34
   6:     0x7ff6614fb82a - std::panicking::default_hook::closure$1
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:272
   7:     0x7ff6614fb498 - std::panicking::default_hook
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:292
   8:     0x7ff6614fbed8 - std::panicking::rust_panic_with_hook
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:731
   9:     0x7ff6614fbdbd - std::panicking::begin_panic_handler::closure$0
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:609
  10:     0x7ff6614f95f9 - std::sys_common::backtrace::__rust_end_short_backtrace<std::panicking::begin_panic_handler::closure_env$0,never$>
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\sys_common\backtrace.rs:170
  11:     0x7ff6614fbad0 - std::panicking::begin_panic_handler
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:597
  12:     0x7ff661519477 - core::panicking::panic_fmt
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\panicking.rs:72
  13:     0x7ff6615198b3 - core::result::unwrap_failed
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\result.rs:1652
  14:     0x7ff6614e4689 - cargo_build_script_output_parser::BuildScriptOutput::outputs_from_command::hbf4070ca21c2b37e    
  15:     0x7ff6614d64f7 - <alloc::string::String as core::fmt::Display>::fmt::h64b17fc9334bfb13
  16:     0x7ff6614dd9c1 - <alloc::string::String as core::fmt::Display>::fmt::h64b17fc9334bfb13
  17:     0x7ff6614e1ad6 - alloc::sync::Arc<T,A>::drop_slow::h6f03314d4e990a47
  18:     0x7ff6614e1aec - std::rt::lang_start::{{closure}}::hd2ca0a94423ebe1b
  19:     0x7ff6614f02f8 - std::rt::lang_start_internal::closure$2
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\rt.rs:148
  20:     0x7ff6614f02f8 - std::panicking::try::do_call
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:504
  21:     0x7ff6614f02f8 - std::panicking::try
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:468
  22:     0x7ff6614f02f8 - std::panic::catch_unwind
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panic.rs:142
  23:     0x7ff6614f02f8 - std::rt::lang_start_internal
                               at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\rt.rs:148
  24:     0x7ff6614dda9c - main
  25:     0x7ff6615178f0 - invoke_main
                               at d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  26:     0x7ff6615178f0 - __scrt_common_main_seh
                               at d:\a01\_work\20\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  27:     0x7fff390c257d - BaseThreadInitThunk
  28:     0x7fff3a04aa58 - RtlUserThreadStart
Target //third-party/plugin-proto:compiler_plugin_rust_proto failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.588s, Critical Path: 0.36s
INFO: 65 processes: 65 internal.
FAILED: Build did NOT complete successfully

Details that might or might not be relevant.

In ~/.bazelrc:

common --disk_cache=~/.cache/bazel
startup --output_base=c:/bzl

In .bazelrc:

startup --noautodetect_server_javabase

In third-party/plugin-proto/BUILD.bazel:

load("@rules_rust//proto/prost:defs.bzl", "rust_prost_library")

rust_prost_library(
    name = "compiler_plugin_rust_proto",
    proto = "@com_google_protobuf//:compiler_plugin_proto",
)

In third-party/plugin-proto/deps.bzl:

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

def deps():
    http_file(
        name = "plugin_proto",
        sha256 = "c2921a11ae4e84214b268fbbe8ea37aaf6c1dc41a3c321b52f871f8dede4c03e",
        url = "https://github.com/protocolbuffers/protobuf/blob/a2f92689dac8a7dbea584919c7de52d6a28d66d1/src/google/protobuf/compiler/plugin.proto",
        downloaded_file_path = "plugin.proto",
    )
    git_repository(name = "com_google_protobuf", remote = "https://github.com/protocolbuffers/protobuf")
criemen commented 8 months ago

I ran into this as well, this is a (bad) error message indicating that you need to specify

startup --windows_enable_symlinks
build --enable_runfiles

in your .bazelrc, so that the runfiles directory is properly filled. Otherwise, the directory cargo_build_script wants to build in doesn't exist, and you get this error.

UebelAndre commented 1 month ago

I ran into this as well, this is a (bad) error message indicating that you need to specify

startup --windows_enable_symlinks
build --enable_runfiles

in your .bazelrc, so that the runfiles directory is properly filled. Otherwise, the directory cargo_build_script wants to build in doesn't exist, and you get this error.

I believe this is indeed the root cause so I'll close this in favor of https://github.com/bazelbuild/rules_rust/issues/1156. If that's not the case though feel free to reopen!