rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

error: unable to run `strip`: No such file or directory (os error 2)

Open avdv opened this issue 2 years ago • 5 comments

Hi.

I am trying to build a simple rust_binary, but the build fails with the error:

ERROR: /private/var/tmp/_bazel_claudio/7ec48aaaaf38cb969348d6b03cb24398/external/rules_rust/util/process_wrapper/BUILD.bazel:31:36: Compiling Rust (without process_wrapper) bin process_wrapper (6 files) [for tool] failed: (Exit 1): process-wrapper failed: error executing command 
  (cd /private/var/tmp/_bazel_claudio/7ec48aaaaf38cb969348d6b03cb24398/sandbox/processwrapper-sandbox/5/execroot/__main__ && \
  exec env - \
    CARGO_CFG_TARGET_ARCH=x86_64 \
    CARGO_CFG_TARGET_OS=darwin \
    CARGO_CRATE_NAME=process_wrapper \
    CARGO_MANIFEST_DIR='${pwd}/external/rules_rust/util/process_wrapper' \
    CARGO_PKG_AUTHORS='' \
    CARGO_PKG_DESCRIPTION='' \
    CARGO_PKG_HOMEPAGE='' \
    CARGO_PKG_NAME=process_wrapper \
    CARGO_PKG_VERSION=0.0.0 \
    CARGO_PKG_VERSION_MAJOR=0 \
    CARGO_PKG_VERSION_MINOR=0 \
    CARGO_PKG_VERSION_PATCH=0 \
    CARGO_PKG_VERSION_PRE='' \
    PATH=/nix/store/0ly5lpwanagc5qpmnqrw8aqxb4ivw7kf-bash-5.2-p15/bin:/nix/store/vd0b59wk8x56i8kmwda8jl5h37wnixn2-coreutils-9.3/bin:/nix/store/2jp91fvgj8mjnpx2vjavc7sw5njbdv04-diffutils-3.10/bin:/nix/store/dl50w02kxffqn52rc1hlps01danyac5g-file-5.45/bin:/nix/store/cmh1dkqbzds5ilgmz9vc7jpavd8lflp3-findutils-4.9.0/bin:/nix/store/f27mhm2kpk37mpj88f77j0y2ncgb95a7-gawk-5.2.2/bin:/nix/store/ccnn45f8fjs7dcj53jgw8jmaa2dq9nrm-gnugrep-3.11/bin:/nix/store/l7hp06f9rfvx8w54vmi3vk1xvgpj34fc-patch-2.7.6/bin:/nix/store/lpzfs8kvdf7fs2j2fbc8a0r2zjjlygna-gnused-4.9/bin:/nix/store/9zmlp4a3y519q3hkflyik9alsmjzpyda-gnutar-1.35/bin:/nix/store/q0vax9kk0mylpd91d9j18556awm1l6ip-gzip-1.13/bin:/nix/store/cdbhdah4qillzy7z9rsi3svbarwm8a6i-python3-3.11.6/bin:/nix/store/693r22pwssm9pl0bs8qym3sqv7l1z730-unzip-6.0/bin:/nix/store/v89g5yvikb31g5xf67zfzycqbag37afw-which-2.21/bin:/nix/store/xga5kc0ggmhyarkphb5wqb8ipb6xb724-zip-3.0/bin \
    SYSROOT=bazel-out/darwin-opt-exec-C7777A24/bin/external/nix_rust/rust_nix_impl \
    TMPDIR=/tmp \
  /var/tmp/_bazel_claudio/install/cd2c210472e7996ec15d2a8e1f15308e/process-wrapper '--timeout=0' '--kill_delay=15' '--stats=/private/var/tmp/_bazel_claudio/7ec48aaaaf38cb969348d6b03cb24398/sandbox/processwrapper-sandbox/5/stats.out' bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust/util/process_wrapper/process_wrapper.sh -- bazel-out/darwin-opt-exec-C7777A24/bin/external/nix_rust/rust_nix_impl/bin/rustc external/rules_rust/util/process_wrapper/main.rs '--crate-name=process_wrapper' '--crate-type=bin' '--error-format=human' '--out-dir=bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust/util/process_wrapper' '--codegen=opt-level=3' '--codegen=debuginfo=0' '--remap-path-prefix=${pwd}=' '--emit=dep-info,link' '--color=always' '--target=x86_64-apple-darwin' -L bazel-out/darwin-opt-exec-C7777A24/bin/external/nix_rust/rust_nix_impl/lib/rustlib/x86_64-apple-darwin/lib '-Cstrip=debuginfo' '--edition=2018' '--codegen=linker=external/nixpkgs_config_cc/cc_wrapper.sh' --codegen 'link-args=-undefined dynamic_lookup -headerpad_max_install_names -lpthread -ldl -lc++ -lm' '--extern=tinyjson=bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust_tinyjson/libtinyjson-4031717389.rlib' '-Ldependency=bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust_tinyjson' '--sysroot=bazel-out/darwin-opt-exec-C7777A24/bin/external/nix_rust/rust_nix_impl')
error: unable to run `strip`: No such file or directory (os error 2)

error: aborting due to previous error

Target //:hello failed to build
INFO: Elapsed time: 2.082s, Critical Path: 1.45s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully

This seems to be caused by rustc expecting a strip binary in PATH. Is that right? Is there a way to inform rustc which tool it should call when stripping a binary instead of doing a PATH lookup, ie. use the strip tool from the cc toolchain?

\edit: This is only the case on Darwin (or Illumos), reference: https://github.com/rust-lang/rust/blob/8d39ec1825024f3014e1f847942ac5bbfcf055b0/compiler/rustc_codegen_ssa/src/back/link.rs#L1043-L1103

avdv avatar Jan 04 '24 07:01 avdv

FYI @avdv It's possible with generated toolchain configs: https://github.com/TraceMachina/nativelink/blob/7f4a007f9b5ed24d063a2fcb705816141643f378/local-remote-execution/generated/cc/BUILD#L143

Something I'm noticing though is that the cc-wrapper.sh from nixpkgs doesn't play nice with Bazel 7 which flips incompatible_cc_toolchain_resolution and somehow propagates PATH differently from before. Technically I think this is a good thing. But it seems to lead to an issue where the rules_rust link envs need an explicit mktemp tool provided in the PATH.

So far I'm finding this a bit tricky. The default path for rules_rust in linker actions looks like this:

PATH="bazel-out/k8-opt-exec-ST-6b9c548ec54e/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/bin"

So there is no entrypoint for coreutils from nixpkgs. Overriding the rust_tolchain with the one from rules_nixpkgs might fix this, but I'm trying to get this working with the default bazel rust toolchain.

So far it looks to me like the only way is to add ${pkgs.coreutils}/bin to the PATH of the link action. The same thing would work with ${pkgs.binutils} for the strip command, but that indeed sees like a hacky solution. Passing these paths to clan via -B won't work since the failure occurs in the cc-wrapper script, not in the "binary" clang invocation.

aaronmondal avatar Jan 08 '24 00:01 aaronmondal

Is there any easy workaround for this? I know nothing about bazel and was trying to compile some software that uses it when I ran into this issue on macos.

evelant avatar Jul 10 '24 18:07 evelant

Is there any easy workaround for this? I know nothing about bazel and was trying to compile some software that uses it when I ran into this issue on macos.

@evelant I think you would only have to install Xcode command line tools so that /usr/bin/strip is available. Otherwise it depends on what the project you're trying to build does. Is it using nixpkgs?

So far it looks to me like the only way is to add ${pkgs.coreutils}/bin to the PATH of the link action. The same thing would work with ${pkgs.binutils} for the strip command, but that indeed sees like a hacky solution. Passing these paths to clan via -B won't work since the failure occurs in the cc-wrapper script, not in the "binary" clang invocation.

@aaronmondal FYI, I just discovered that on nixos-24.05 using strip from pkgs.binutils and compiling with -Cstrip=debuginfo produces a binary that just gets killed immediately:

$ bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust/util/process_wrapper/process_wrapper
Killed: 9
$ sudo dtruss bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust/util/process_wrapper/process_wrapper
dtrace: failed to execute bazel-out/darwin-opt-exec-C7777A24/bin/external/rules_rust/util/process_wrapper/process_wrapper: Malformed Mach-o file

Removing the -Cstrip=debuginfo flag or using srip from pkgs.darwin.cctools instead works.

avdv avatar Jul 11 '24 07:07 avdv

I already have xcode command line tools installed and strip is on my path. I was trying to build TypeDB 3.0 https://github.com/vaticle/typedb/tree/3.0. Despite strip being on the path bazel fails to find it. I assume it's modifying the path in an attempt to make builds hermetic but I have no idea beyond that.

evelant avatar Jul 11 '24 12:07 evelant

OK it must be an issue with bazel from nixpkgs (I had installed bazel 6.5.0 from nixpkgs unstable via home-manager on nix-darwin). I installed bazelisk with homebrew and it works fine that way.

evelant avatar Jul 11 '24 13:07 evelant