Support omitting automatic rpaths for packaged Linux rust_binary outputs

Open
#4,325 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
46/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
rust
Domain
build-system

Research direction

Read rustc_compile_action and get_linker_and_args to trace how automatic rpaths reach the linker, then inspect the planned configurability redesign in #4049. Add a regression test comparing default automatic paths with opted-in authored paths and checking refusal when dynamic Rust std is enabled; confirm the public option’s scope with the maintainers.

Written by the indexing model from the issue text.

Description

needs-triage

Linux distribution binaries need a way to omit Bazel's automatically computed runtime library paths while preserving explicitly authored linker flags.

A concrete packaging layout is bin/app plus lib/libdependency.so. We compile with static Rust std, copy the executable and its declared shared libraries into an OCI image, and want the sole runtime search path to be $ORIGIN/../lib. Paths into Bazel's _solib and runfiles trees do not exist in that image.

With rules_rust 0.74.0, rustc_compile_action computes rpaths from native dependencies and passes them into get_linker_and_args. Adding an explicit path through rustc_flags preserves the automatic paths too. Disabling the C++ runtime_library_search_directories feature is not a suitable general solution: the configured C++ link action can require that feature during analysis.

We currently use a small downstream correction: a boolean on rust_binary, default false, which leaves the C++ feature configuration intact but passes an empty rpath depset to the linker for an opted-in binary. It preserves user-authored linker flags and normal build-script/proc-macro linking. Our current implementation refuses non-Linux binaries and link_std_dylib=True, so it does not silently break dynamic Rust std loading.

For example, a possible public interface would be:

rust_binary(
    name = "app",
    srcs = ["main.rs"],
    deps = [":shared_native_dependency"],
    relocatable_binary = True,
    rustc_flags = ["-Clink-arg=-Wl,-rpath,$$ORIGIN/../lib"],
)

Validation of the downstream correction includes ARM64 ELF dynamic-section checks after copying out of the execroot, startup of every packaged executable in the exact runtime image, and a backend query that executes packaged V8. The default behavior remains unchanged. These checks were executed against the pinned 0.74.0 correction; I am not claiming execution against upstream main.

I checked main at bbeacc846555ffe6738ad79ea3013f2c255c8081 and the same automatic-rpath path remains. Before proposing a public attribute, as CONTRIBUTING.md requests, would you prefer this narrowly scoped option, an existing supported configuration that I missed, or a hook in the planned configurability redesign (#4049)? A regression test should compare default automatic paths with opted-in authored paths and verify refusal for dynamic Rust std.

Assisted-by: OpenAI Codex

Dominant language
Starlark
Stars
857
Forks
701
Avg merge
4d 7h
Merged PRs (30d)
46

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from bazelbuild/rules_rust

All issues in bazelbuild/rules_rust

Similar issues

More Build System issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.