Skip to content

Commit

Permalink
Mount user-specified bind mounts before Bazel's own magic.
Browse files Browse the repository at this point in the history
This makes it possible to mount directories under /tmp somewhere else. Before, /tmp was overridden by the implementation of hermetic /tmp.

Fixes #20527.

RELNOTES: None.
PiperOrigin-RevId: 592247867
Change-Id: Ib5b75cd21ffe4fa4c8ee3f75d82894da6dd61f54
  • Loading branch information
lberki authored and copybara-github committed Dec 19, 2023
1 parent 132f73a commit 3748084
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ private ImmutableList<BindMount> getBindMounts(

LinuxSandboxUtil.validateBindMounts(bindMounts);
ImmutableList.Builder<BindMount> result = ImmutableList.builder();
bindMounts.forEach((k, v) -> result.add(BindMount.of(k, v)));

if (sandboxTmp != null) {
// First mount the real exec root and the empty directory created as the working dir of the
Expand All @@ -445,7 +446,6 @@ private ImmutableList<BindMount> getBindMounts(
result.add(BindMount.of(tmpPath, sandboxTmp));
}

bindMounts.forEach((k, v) -> result.add(BindMount.of(k, v)));
return result.build();
}

Expand Down
28 changes: 28 additions & 0 deletions src/test/shell/bazel/bazel_sandboxing_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,34 @@ EOF
bazel build //pkg:a &>$TEST_log || fail "expected build to succeed"
}

function test_add_mount_pair_tmp_source() {
if [[ "$PLATFORM" == "darwin" ]]; then
# Tests Linux-specific functionality
return 0
fi

create_workspace_with_default_repos WORKSPACE

sed -i.bak '/sandbox_tmpfs_path/d' $TEST_TMPDIR/bazelrc

mkdir -p pkg
cat > pkg/BUILD <<'EOF'
genrule(
name = "gen",
outs = ["gen.txt"],
cmd = "cp /etc/data.txt $@",
)
EOF

local mounted=$(mktemp -d "/tmp/bazel_mounted.XXXXXXXX")
trap "rm -fr $mounted" EXIT
echo GOOD > "$mounted/data.txt"

# This assumes the existence of /etc on the host system
bazel build --sandbox_add_mount_pair="$mounted:/etc" //pkg:gen || fail "build failed"
assert_contains GOOD bazel-bin/pkg/gen.txt
}

# The test shouldn't fail if the environment doesn't support running it.
check_sandbox_allowed || exit 0

Expand Down

0 comments on commit 3748084

Please sign in to comment.