bazelbuild / rules_rust

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

cargo bootstrap failure #2874

Closed sirbeancounter closed 1 month ago

sirbeancounter commented 2 months ago

A project using bazel with rust_rules using bzlmod to build a tool coded in Rust seems to work very well. This project is used as a bazel module in another larger project (not Rust) to provide this tool and that also seems to work very well. The problem is in the larger projects CI pipeline where the bazel build is run within a docker container image. It produces the following error after running bazel build ...:

ERROR: Traceback (most recent call last):
        File "/repo/user/project/build/bazel_output_base/external/rules_rust~/crate_universe/extension.bzl", line 383, column 37, in _crate_impl
                _generate_hub_and_spokes(module_ctx, cargo_bazel, cfg, annotations, packages = packages)
        File "/repo/user/project/build/bazel_output_base/external/rules_rust~/crate_universe/extension.bzl", line 132, column 16, in _generate_hub_and_spokes
                cargo_bazel(splice_args)
        File "/repo/user/project/build/bazel_output_base/external/rules_rust~/crate_universe/private/module_extensions/cargo_bazel_bootstrap.bzl", line 68, column 17, in run
                fail("%s returned with exit code %d:\n%s" % (pretty_args, result.return_code, result.stderr))
Error in fail: /repo/user/project/build/bazel_output_base/modextwd/rules_rust~~crate/cargo-bazel splice --output-dir /repo/user/project/build/bazel_output_base/modextwd/rules_rust~~crate/crates/splicing-output --config /repo/user/project/build/bazel_output_base/modextwd/rules_rust~~crate/crates/config.json --splicing-manifest /repo/user/project/build/bazel_output_base/modextwd/rules_rust~~crate/crates/splicing_manifest.json --cargo /repo/user/project/build/bazel_output_base/external/rules_rust~~rust_host_tools~rust_host_tools/bin/cargo --rustc /repo/user/project/build/bazel_output_base/external/rules_rust~~rust_host_tools~rust_host_tools/bin/rustc returned with exit code 1:

error: Cargo couldn't find your home directory. This probably means that $HOME was not set.

Error: Failed to generate lockfile

Caused by:
    Failed to query cargo version
ERROR: Analysis of target '***' failed; build aborted: error evaluating module extension crate in @@rules_rust~//crate_universe:extension.bzl

$USER is user and $HOME is set but not to /home/user, instead it is: HOME=/repo/user/project/build

Any help to understand or debug the problem would be really appreciated. I think it is related to how Cargo locates its home directory. So the build works when users run the build themselves and ~/.cargo (default Cargo home directory) is populated accordingly. The question is how to set up Cargo to work when you need to specify alternative Cargo home directory.

sirbeancounter commented 1 month ago

I managed to solve this problem by mapping /etc/passwd from host to container when running the docker container build image, e.g. by adding the following argument to the docker run command:

--volume /etc/passwd:/etc/passwd:ro