bazelbuild / bazelbuild/rules_rust
GCC cannot find linker when rustc invoked with
- Dominant language
- Starlark
- Stars
- 843
- Forks
- 651
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 15
Description
Hi,
We have a bazel setup with a `cc_toolchain` configured to use gold via `--fuse-ld=gold`. When rules rust runs rustc it forwards the linker flags configured in the `cc_toolchain` to rustc with `--codegen 'link-args= -fuse-ld=gold'`.
However, the environment variables are such that `PATH` is not set and therefore gcc cannot find the gold executable (which is in `/usr/bin/gold`). This means I am seeing errors looking like:
```
error: linking with `/usr/bin/gcc` failed: exit status: 1 | = note: "/usr/bin/gcc" "-fuse-ld=gold"
= note: collect2: fatal error: cannot find 'ld'
compilation terminated.
error: aborting due to previous error
```
(`cannot find 'ld'` is just bad error reporting from gcc. It means that gcc cannot find `gold`).
---
The only way I found to fix this is to patch rules rust like so:
```patch
diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl
index 9249ff8..f415b61 100644
--- a/rust/private/rustc.bzl
+++ b/rust/private/rustc.bzl
@@ -676,6 +676,7 @@ def construct_arguments(
# Set the SYSROOT to the directory of the rust_std files passed to the toolchain
env["SYSROOT"] = paths.dirname(toolchain.rust_std.files.to_list()[0].short_path)
+ env["PATH"] = "/usr/bin"
# extra_rustc_flags apply to the target configuration, not the exec configuration.
if hasattr(ctx.attr, "_extra_rustc_flags") and not is_exec_configuration(ctx):
```
---
I was wondering if there was a way to configure the PATH env variable used when running rustc? Possibly I am missing something and this is already doable without patching rules_rust, if so I am sorry!
Contributor guide
Assessment
This issue has not been assessed yet.