Skip to content

Conversation

@randomizedcoder
Copy link

What type of PR is this?

This is small PR to provide a proof of concept idea to allow "impure environment variables" to be passed into the rules_go sandbox. This is mostly for a discussion, as I imagine the "owners" of rules_go will have their own and probably better ideas for how to do this.

Really, I just wanted to be able to set LD_LIBRARY_PATH within the sandbox, but I implemented this in a very generic way, so that any environment variable can be set. Maybe people will want to limit this to a specific set of variable, or to exactly LD_LIBRARY_PATH. The docs do include WARNINGS about why this is a bad idea.

This is arguably a bug fix, and is a new feature.

Bug fix
Feature

What does this PR do? Why is it needed?
This PR allows environment variables to be set within the sandbox for rules_go.

The reason is that for golang race tests, the go code needs to be linked against the libraries precompiled with LLVM, and so you need a linker. The problem is that the ld.lld linker needs libxml2.so.2, and there's no way currently to set the LD_LIBRARY_PATH, so you can't supply libxml2.so.2 via a sysroot.

This PR introduces "impure_env" to allow you to set LD_LIBRARY_PATH, or any other environment variable.

go_test(
    name = "hello_test",
    srcs = ["hello_test.go"],
    embed = [":hello_lib"],
    cgo = True,
    cdeps = [":system_deps"],
    data = ["@bazel_sysroot_tarball//:all"],
    impure_env = {
        "LD_LIBRARY_PATH": "external/bazel_sysroot_tarball/lib",   <----- LD_LIBRARY_PATH
    },
    clinkopts = [
        "-L@bazel_sysroot_tarball//:lib",
        "-Wl,-rpath,$$ORIGIN/../../external/bazel_sysroot_tarball/lib",
        "-Wl,--no-as-needed",
        "-lxml2",
    ],
)

Which issues(s) does this PR fix?

With this PR, I'm able to use bazel on NixOS to run the go race tests!

Fixes #
#4360

Other notes for review

To demonstrate this, there is a very simple repo: https://github.com/randomizedcoder/go_hello_world_race

The idea is to demonstrate how to execute go race tests.

The repo includes nix configuration to create a sysroot, which is hosted here: https://github.com/randomizedcoder/bazel_remote_runner_sysroot/

The sysroot contains the basic libraries clang needs to link the code required for the race test. Please note the included libraries is currently more than is strictly required, but the repo is still only ~22MB, so it's not too bad.
https://github.com/randomizedcoder/go_hello_world_race/blob/main/sysroot/default.nix#L6

The patched rules_go is currently applied as an archive_override in this repo :https://github.com/randomizedcoder/go_hello_world_race/blob/main/MODULE.bazel#L21

With the new rules_go, I'm finally after weeks for trying able to run go race tests with bazel!!

bazelisk test //:hello_test --features=race --sandbox_debug --verbose_failures
...
1748208735.178445248: src/main/tools/linux-sandbox-pid1.cc:554: child exited normally with code 0
1748208735.179069961: src/main/tools/linux-sandbox.cc:253: child exited normally with code 0

Run this command to start an interactive shell in an identical sandboxed environment:
(exec env - \
    EXPERIMENTAL_SPLIT_XML_GENERATION=1 \
    GO_TEST_RUN_FROM_BAZEL=1 \
    JAVA_RUNFILES=bazel-out/k8-fastbuild/bin/hello_test_/hello_test.runfiles \
    LD_LIBRARY_PATH=external/bazel_sysroot_tarball/lib \
    PATH=/home/das/.cache/bazelisk/downloads/sha256/511d7879b4662382d6e1590a39d64241e4447515e586ad63df6ba15b8f72efaf/bin:/run/wrappers/bin:/usr/bin:/usr/sbin:/nix/store/33ivrhi748b8k8rdymrmjyqf9785dllb-cursor-0.49.6-extracted/usr/bin/:/nix/store/33ivrhi748b8k8rdymrmjyqf9785dllb-cursor-0.49.6-extracted/usr/sbin/:/nix/store/33ivrhi748b8k8rdymrmjyqf9785dllb-cursor-0.49.6-extracted/usr/games/:/nix/store/33ivrhi748b8k8rdymrmjyqf9785dllb-cursor-0.49.6-extracted/bin/:/nix/store/33ivrhi748b8k8rdymrmjyqf9785dllb-cursor-0.49.6-extracted/sbin/:/nix/store/m3hnxdlz6v6650ggqz29nsqbvzvdvnsy-bash-interactive-5.2p37/bin:/nix/store/hzw38c3f7s0w200cgk9645z53al7k8lw-binutils-2.44/bin:/nix/store/h5rn37dd6vfvr9xb0jq85sq8hf6xchry-coreutils-9.6/bin:/nix/store/qi25cvn9j1xyvl9p7lp8nw9wqk5k648r-gawk-5.3.1/bin:/nix/store/pmfmr1wrh8da7lk42hvyxgg4cnx629dr-libarchive-3.7.8/bin:/nix/store/fch0kdhkh26kd6r1wqsa9121xcy6xnvf-pv-1.9.31/bin:/nix/store/rgz9x6gjjfl34l1ikxlpz1cd3gf5m7ld-squashfs-4.6.1/bin:/run/wrappers/bin:/usr/bin:/usr/sbin:/run/wrappers/bin:/home/das/.nix-profile/bin:/nix/profile/bin:/home/das/.local/state/nix/profile/bin:/etc/profiles/per-user/das/bin:/nix/var/nix/profiles/default/bin:/run/current-system/sw/bin:/nix/store/xhg020fc8p329vrqb72m7bgc22wlvxs8-ghostty-1.1.3/bin \
    PYTHON_RUNFILES=bazel-out/k8-fastbuild/bin/hello_test_/hello_test.runfiles \
    RUNFILES_DIR=bazel-out/k8-fastbuild/bin/hello_test_/hello_test.runfiles \
    RUN_UNDER_RUNFILES=1 \
    TEST_BINARY=hello_test_/hello_test \
    TEST_INFRASTRUCTURE_FAILURE_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.infrastructure_failure \
    TEST_LOGSPLITTER_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.raw_splitlogs/test.splitlogs \
    TEST_PREMATURE_EXIT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.exited_prematurely \
    TEST_SIZE=medium \
    TEST_SRCDIR=bazel-out/k8-fastbuild/bin/hello_test_/hello_test.runfiles \
    TEST_TARGET=//:hello_test \
    TEST_TIMEOUT=300 \
    TEST_TMPDIR=_tmp/df357f155bebd4a0bf649c1b474a0148 \
    TEST_UNDECLARED_OUTPUTS_ANNOTATIONS=bazel-out/k8-fastbuild/testlogs/hello_test/test.outputs_manifest/ANNOTATIONS \
    TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR=bazel-out/k8-fastbuild/testlogs/hello_test/test.outputs_manifest \
    TEST_UNDECLARED_OUTPUTS_DIR=bazel-out/k8-fastbuild/testlogs/hello_test/test.outputs \
    TEST_UNDECLARED_OUTPUTS_MANIFEST=bazel-out/k8-fastbuild/testlogs/hello_test/test.outputs_manifest/MANIFEST \
    TEST_UNUSED_RUNFILES_LOG_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.unused_runfiles_log \
    TEST_WARNINGS_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.warnings \
    TEST_WORKSPACE=_main \
    TMPDIR=/tmp \
    TZ=UTC \
    XML_OUTPUT_FILE=bazel-out/k8-fastbuild/testlogs/hello_test/test.xml \
  /home/das/.cache/bazel/_bazel_das/install/254cc618fc52f01b2b1d5fd6ff343774/linux-sandbox -T 300 -t 15 -w /dev/shm -w /home/das/.cache/bazel/_bazel_das/2069c7e7bbea1cec17d73a6b1498e560/sandbox/linux-sandbox/10/execroot/_main -w /home/das/.cache/bazel/_bazel_das/2069c7e7bbea1cec17d73a6b1498e560/sandbox/linux-sandbox/10/execroot/_main/_tmp/df357f155bebd4a0bf649c1b474a0148 -w /tmp -M /home/das/.cache/bazel/_bazel_das/2069c7e7bbea1cec17d73a6b1498e560/sandbox/linux-sandbox/10/_hermetic_tmp -m /tmp -S /home/das/.cache/bazel/_bazel_das/2069c7e7bbea1cec17d73a6b1498e560/sandbox/linux-sandbox/10/stats.out -D /home/das/.cache/bazel/_bazel_das/2069c7e7bbea1cec17d73a6b1498e560/sandbox/linux-sandbox/10/debug.out -- /bin/sh -i)
INFO: Found 1 test target...
Target //:hello_test up-to-date:
  bazel-bin/hello_test_/hello_test
INFO: Elapsed time: 74.241s, Critical Path: 61.11s
INFO: 15 processes: 1 action cache hit, 5 internal, 10 linux-sandbox.
INFO: Build completed successfully, 15 total actions
//:hello_test                                                            PASSED in 0.0s

Executed 1 out of 1 test: 1 test passes.
There were tests whose specified size is too big. Use the --test_verbose_timeout_warnings command line option to see which ones these are.

[das@t:~/Downloads/go_hello_world_race]$ 

Looking forward to discussing the idea with you.

Thanks in advance,
Dave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant