Skip to content

Commit 51d7a3c

Browse files
fmeumcopybara-github
authored andcommitted
Fix in-process materialization of unresolved symlinks in runfiles
Unresolved symlinks, when consumed as runfiles with `--experimental_inprocess_symlink_creation`, are now created as symlinks directly pointing to their target path instead of to the absolute path of the unresolved symlink artifact under the execroot, just like with out-of-process symlink creation. Along the way, fix the propagation of `--experimental_inprocess_symlink_creation` to the exec configuration, which previously would have caused the new test cases to fail open. Fixes #23327 Closes #23328. PiperOrigin-RevId: 665754549 Change-Id: I6b240ca27dac513738056b015c97baec91677c4e
1 parent 8b2057e commit 51d7a3c

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/main/java/com/google/devtools/build/lib/exec/SymlinkTreeHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ void syncTreeRecursively(Path at) throws IOException {
307307
Path next = at.getChild(entry.getKey());
308308
if (entry.getValue() == null) {
309309
FileSystemUtils.createEmptyFile(next);
310+
} else if (entry.getValue().isSymlink()) {
311+
FileSystemUtils.ensureSymbolicLink(next, entry.getValue().getPath().readSymbolicLink());
310312
} else {
311313
FileSystemUtils.ensureSymbolicLink(next, entry.getValue().getPath().asFragment());
312314
}

src/main/starlark/builtins_bzl/common/builtin_exec_platforms.bzl

+1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ bazel_fragments["CoreOptions"] = fragment(
248248
"//command_line_option:experimental_exclude_defines_from_exec_config",
249249
"//command_line_option:experimental_exclude_starlark_flags_from_exec_config",
250250
"//command_line_option:experimental_propagate_custom_flag",
251+
"//command_line_option:experimental_inprocess_symlink_creation",
251252
],
252253
inputs = ["//command_line_option:features"],
253254
outputs = [

src/test/shell/bazel/bazel_symlink_test.sh

+40-1
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,28 @@ function test_unresolved_symlink_as_input_local() {
784784
bazel build //pkg:c || fail "symlink should resolve"
785785
}
786786

787+
function test_unresolved_symlink_as_input_local_inprocess() {
788+
if "$is_windows"; then
789+
# TODO(#10298): Support unresolved symlinks on Windows.
790+
return 0
791+
fi
792+
793+
setup_unresolved_symlink_as_input
794+
add_to_bazelrc build --spawn_strategy=local
795+
add_to_bazelrc build --experimental_inprocess_symlink_creation
796+
797+
bazel build //pkg:b && fail "symlink should not resolve"
798+
799+
bazel clean
800+
bazel build //pkg:file
801+
# Since the build isn't sandboxed, the symlink to //:a resolves even though
802+
# the action does not declare it as an input.
803+
bazel build //pkg:b || fail "symlink expected to resolve non-hermetically"
804+
805+
bazel clean
806+
bazel build //pkg:c || fail "symlink should resolve"
807+
}
808+
787809
function test_unresolved_symlink_as_input_sandbox() {
788810
if "$is_windows"; then
789811
# TODO(#10298): Support unresolved symlinks on Windows.
@@ -797,7 +819,7 @@ function test_unresolved_symlink_as_input_sandbox() {
797819

798820
bazel clean
799821
bazel build //pkg:a
800-
# Since the build isn't sandboxed, the symlink to //:a does not resolves even
822+
# Since the build is sandboxed, the symlink to //:a does not resolves even
801823
# though it would in the unsandboxed exec root due to //:a having been built
802824
# before.
803825
bazel build //pkg:b && fail "sandboxed build is not hermetic"
@@ -886,6 +908,23 @@ function test_unresolved_symlink_as_runfile_local() {
886908
bazel build //pkg:use_tool_non_hermetically && fail "symlink in runfiles resolved outside the runfiles tree" || true
887909
}
888910

911+
function test_unresolved_symlink_as_runfile_local_inprocess() {
912+
if "$is_windows"; then
913+
# TODO(#10298): Support unresolved symlinks on Windows.
914+
return 0
915+
fi
916+
917+
setup_unresolved_symlink_as_runfile
918+
add_to_bazelrc build --spawn_strategy=local
919+
add_to_bazelrc build --experimental_inprocess_symlink_creation
920+
921+
bazel build //pkg:use_tool || fail "local build failed"
922+
# Keep the implicitly built //pkg:a around to make the symlink resolve
923+
# outside the runfiles tree. The build should still fail as the relative
924+
# symlink is staged as is and doesn't resolve outside the runfiles tree.
925+
bazel build //pkg:use_tool_non_hermetically && fail "symlink in runfiles resolved outside the runfiles tree" || true
926+
}
927+
889928
function test_unresolved_symlink_as_runfile_symlink() {
890929
if "$is_windows"; then
891930
# TODO(#10298): Support unresolved symlinks on Windows.

0 commit comments

Comments
 (0)