Skip to content

Commit 0b64525

Browse files
authored
Make Bash runfiles library repo mapping aware (bazelbuild#16753)
Work towards bazelbuild#16124 Closes bazelbuild#16693. PiperOrigin-RevId: 487811689 Change-Id: Iec5ef01ae09394372b87e5cb697ceb728563d2dc
1 parent c3e2b98 commit 0b64525

File tree

5 files changed

+738
-59
lines changed

5 files changed

+738
-59
lines changed

src/test/py/bazel/bzlmod/bazel_module_test.py

+60
Original file line numberDiff line numberDiff line change
@@ -814,5 +814,65 @@ def testCppRunfilesLibraryRepoMapping(self):
814814
allow_failure=True)
815815
self.AssertExitCode(exit_code, 0, stderr, stdout)
816816

817+
def testBashRunfilesLibraryRepoMapping(self):
818+
self.main_registry.setModuleBasePath('projects')
819+
projects_dir = self.main_registry.projects
820+
821+
self.main_registry.createLocalPathModule('data', '1.0', 'data')
822+
projects_dir.joinpath('data').mkdir(exist_ok=True)
823+
scratchFile(projects_dir.joinpath('data', 'WORKSPACE'))
824+
scratchFile(projects_dir.joinpath('data', 'foo.txt'), ['hello'])
825+
scratchFile(
826+
projects_dir.joinpath('data', 'BUILD'), ['exports_files(["foo.txt"])'])
827+
828+
self.main_registry.createLocalPathModule('test', '1.0', 'test',
829+
{'data': '1.0'})
830+
projects_dir.joinpath('test').mkdir(exist_ok=True)
831+
scratchFile(projects_dir.joinpath('test', 'WORKSPACE'))
832+
scratchFile(
833+
projects_dir.joinpath('test', 'BUILD'), [
834+
'sh_test(',
835+
' name = "test",',
836+
' srcs = ["test.sh"],',
837+
' data = ["@data//:foo.txt"],',
838+
' args = ["$(rlocationpath @data//:foo.txt)"],',
839+
' deps = ["@bazel_tools//tools/bash/runfiles"],',
840+
')',
841+
])
842+
test_script = projects_dir.joinpath('test', 'test.sh')
843+
scratchFile(
844+
test_script, """#!/usr/bin/env bash
845+
# --- begin runfiles.bash initialization v2 ---
846+
# Copy-pasted from the Bazel Bash runfiles library v2.
847+
set -uo pipefail; f=bazel_tools/tools/bash/runfiles/runfiles.bash
848+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
849+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
850+
source "$0.runfiles/$f" 2>/dev/null || \
851+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
852+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
853+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
854+
# --- end runfiles.bash initialization v2 ---
855+
[[ -f "$(rlocation $1)" ]] || exit 1
856+
[[ -f "$(rlocation data/foo.txt)" ]] || exit 2
857+
""".splitlines())
858+
os.chmod(test_script, 0o755)
859+
860+
self.ScratchFile('MODULE.bazel', ['bazel_dep(name="test",version="1.0")'])
861+
self.ScratchFile('WORKSPACE')
862+
863+
# Run sandboxed on Linux and macOS.
864+
exit_code, stderr, stdout = self.RunBazel([
865+
'test', '@test//:test', '--test_output=errors',
866+
'--test_env=RUNFILES_LIB_DEBUG=1'
867+
],
868+
allow_failure=True)
869+
self.AssertExitCode(exit_code, 0, stderr, stdout)
870+
# Run unsandboxed on all platforms.
871+
exit_code, stderr, stdout = self.RunBazel(
872+
['run', '@test//:test'],
873+
allow_failure=True,
874+
env_add={'RUNFILES_LIB_DEBUG': '1'})
875+
self.AssertExitCode(exit_code, 0, stderr, stdout)
876+
817877
if __name__ == '__main__':
818878
unittest.main()

src/test/shell/bazel/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ sh_test(
357357
"@bazel_tools//tools/bash/runfiles",
358358
],
359359
shard_count = 3,
360+
tags = ["requires-network"],
360361
)
361362

362363
sh_test(

src/test/shell/bazel/bazel_rules_test.sh

+292
Original file line numberDiff line numberDiff line change
@@ -743,4 +743,296 @@ EOF
743743
bazel run //pkg:my_executable >$TEST_log 2>&1 || fail "Binary should have exit code 0"
744744
}
745745

746+
function setup_bash_runfiles_current_repository() {
747+
touch MODULE.bazel
748+
749+
cat >> WORKSPACE <<'EOF'
750+
local_repository(
751+
name = "other_repo",
752+
path = "other_repo",
753+
)
754+
EOF
755+
756+
mkdir -p pkg
757+
cat > pkg/BUILD.bazel <<'EOF'
758+
sh_library(
759+
name = "library",
760+
srcs = ["library.sh"],
761+
deps = ["@bazel_tools//tools/bash/runfiles"],
762+
visibility = ["//visibility:public"],
763+
)
764+
sh_binary(
765+
name = "binary",
766+
srcs = ["binary.sh"],
767+
deps = [
768+
":library",
769+
"@other_repo//pkg:library2",
770+
"@bazel_tools//tools/bash/runfiles",
771+
],
772+
)
773+
sh_test(
774+
name = "test",
775+
srcs = ["test.sh"],
776+
deps = [
777+
":library",
778+
"@other_repo//pkg:library2",
779+
"@bazel_tools//tools/bash/runfiles",
780+
],
781+
)
782+
EOF
783+
784+
cat > pkg/library.sh <<'EOF'
785+
#!/usr/bin/env bash
786+
# --- begin runfiles.bash initialization v2 ---
787+
# Copy-pasted from the Bazel Bash runfiles library v2.
788+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
789+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
790+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
791+
source "$0.runfiles/$f" 2>/dev/null || \
792+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
793+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
794+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
795+
# --- end runfiles.bash initialization v2 ---
796+
797+
function library() {
798+
echo "in pkg/library.sh: '$(runfiles_current_repository)'"
799+
}
800+
export -f library
801+
EOF
802+
chmod +x pkg/library.sh
803+
804+
cat > pkg/binary.sh <<'EOF'
805+
#!/usr/bin/env bash
806+
# --- begin runfiles.bash initialization v2 ---
807+
# Copy-pasted from the Bazel Bash runfiles library v2.
808+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
809+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
810+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
811+
source "$0.runfiles/$f" 2>/dev/null || \
812+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
813+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
814+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
815+
# --- end runfiles.bash initialization v2 ---
816+
817+
echo "in pkg/binary.sh: '$(runfiles_current_repository)'"
818+
source $(rlocation _main/pkg/library.sh)
819+
library
820+
source $(rlocation other_repo/pkg/library2.sh)
821+
library2
822+
EOF
823+
chmod +x pkg/binary.sh
824+
825+
cat > pkg/test.sh <<'EOF'
826+
#!/usr/bin/env bash
827+
# --- begin runfiles.bash initialization v2 ---
828+
# Copy-pasted from the Bazel Bash runfiles library v2.
829+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
830+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
831+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
832+
source "$0.runfiles/$f" 2>/dev/null || \
833+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
834+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
835+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
836+
# --- end runfiles.bash initialization v2 ---
837+
838+
echo "in pkg/test.sh: '$(runfiles_current_repository)'"
839+
source $(rlocation _main/pkg/library.sh)
840+
library
841+
source $(rlocation other_repo/pkg/library2.sh)
842+
library2
843+
EOF
844+
chmod +x pkg/test.sh
845+
846+
mkdir -p other_repo
847+
touch other_repo/WORKSPACE
848+
849+
mkdir -p other_repo/pkg
850+
cat > other_repo/pkg/BUILD.bazel <<'EOF'
851+
sh_library(
852+
name = "library2",
853+
srcs = ["library2.sh"],
854+
deps = ["@bazel_tools//tools/bash/runfiles"],
855+
visibility = ["//visibility:public"],
856+
)
857+
sh_binary(
858+
name = "binary",
859+
srcs = ["binary.sh"],
860+
deps = [
861+
"//pkg:library2",
862+
"@//pkg:library",
863+
"@bazel_tools//tools/bash/runfiles",
864+
],
865+
)
866+
sh_test(
867+
name = "test",
868+
srcs = ["test.sh"],
869+
deps = [
870+
"//pkg:library2",
871+
"@//pkg:library",
872+
"@bazel_tools//tools/bash/runfiles",
873+
],
874+
)
875+
EOF
876+
877+
cat > other_repo/pkg/library2.sh <<'EOF'
878+
#!/usr/bin/env bash
879+
# --- begin runfiles.bash initialization v2 ---
880+
# Copy-pasted from the Bazel Bash runfiles library v2.
881+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
882+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
883+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
884+
source "$0.runfiles/$f" 2>/dev/null || \
885+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
886+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
887+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
888+
# --- end runfiles.bash initialization v2 ---
889+
890+
function library2() {
891+
echo "in external/other_repo/pkg/library2.sh: '$(runfiles_current_repository)'"
892+
}
893+
export -f library2
894+
EOF
895+
chmod +x pkg/library.sh
896+
897+
cat > other_repo/pkg/binary.sh <<'EOF'
898+
#!/usr/bin/env bash
899+
# --- begin runfiles.bash initialization v2 ---
900+
# Copy-pasted from the Bazel Bash runfiles library v2.
901+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
902+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
903+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
904+
source "$0.runfiles/$f" 2>/dev/null || \
905+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
906+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
907+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
908+
# --- end runfiles.bash initialization v2 ---
909+
910+
echo "in external/other_repo/pkg/binary.sh: '$(runfiles_current_repository)'"
911+
source $(rlocation _main/pkg/library.sh)
912+
library
913+
source $(rlocation other_repo/pkg/library2.sh)
914+
library2
915+
EOF
916+
chmod +x other_repo/pkg/binary.sh
917+
918+
cat > other_repo/pkg/test.sh <<'EOF'
919+
#!/usr/bin/env bash
920+
# --- begin runfiles.bash initialization v2 ---
921+
# Copy-pasted from the Bazel Bash runfiles library v2.
922+
set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash
923+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
924+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
925+
source "$0.runfiles/$f" 2>/dev/null || \
926+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
927+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
928+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
929+
# --- end runfiles.bash initialization v2 ---
930+
931+
echo "in external/other_repo/pkg/test.sh: '$(runfiles_current_repository)'"
932+
source $(rlocation _main/pkg/library.sh)
933+
library
934+
source $(rlocation other_repo/pkg/library2.sh)
935+
library2
936+
EOF
937+
chmod +x other_repo/pkg/test.sh
938+
}
939+
940+
function test_bash_runfiles_current_repository_binary_enable_runfiles() {
941+
setup_bash_runfiles_current_repository
942+
943+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --enable_runfiles //pkg:binary \
944+
&>"$TEST_log" || fail "Run should succeed"
945+
expect_log "in pkg/binary.sh: ''"
946+
expect_log "in pkg/library.sh: ''"
947+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
948+
949+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --enable_runfiles @other_repo//pkg:binary \
950+
&>"$TEST_log" || fail "Run should succeed"
951+
expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'"
952+
expect_log "in pkg/library.sh: ''"
953+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
954+
}
955+
956+
function test_bash_runfiles_current_repository_test_enable_runfiles() {
957+
setup_bash_runfiles_current_repository
958+
959+
bazel test --enable_bzlmod --enable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \
960+
--test_output=all //pkg:test &>"$TEST_log" || fail "Test should succeed"
961+
expect_log "in pkg/test.sh: ''"
962+
expect_log "in pkg/library.sh: ''"
963+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
964+
965+
bazel test --enable_bzlmod --enable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \
966+
--test_output=all @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed"
967+
expect_log "in external/other_repo/pkg/test.sh: 'other_repo'"
968+
expect_log "in pkg/library.sh: ''"
969+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
970+
}
971+
972+
function test_bash_runfiles_current_repository_binary_noenable_runfiles() {
973+
setup_bash_runfiles_current_repository
974+
975+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --noenable_runfiles //pkg:binary \
976+
&>"$TEST_log" || fail "Run should succeed"
977+
expect_log "in pkg/binary.sh: ''"
978+
expect_log "in pkg/library.sh: ''"
979+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
980+
981+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --noenable_runfiles @other_repo//pkg:binary \
982+
&>"$TEST_log" || fail "Run should succeed"
983+
expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'"
984+
expect_log "in pkg/library.sh: ''"
985+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
986+
}
987+
988+
function test_bash_runfiles_current_repository_test_noenable_runfiles() {
989+
setup_bash_runfiles_current_repository
990+
991+
bazel test --enable_bzlmod --noenable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \
992+
--test_output=all //pkg:test &>"$TEST_log" || fail "Test should succeed"
993+
expect_log "in pkg/test.sh: ''"
994+
expect_log "in pkg/library.sh: ''"
995+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
996+
997+
bazel test --enable_bzlmod --noenable_runfiles --test_env=RUNFILES_LIB_DEBUG=1 \
998+
--test_output=all @other_repo//pkg:test &>"$TEST_log" || fail "Test should succeed"
999+
expect_log "in external/other_repo/pkg/test.sh: 'other_repo'"
1000+
expect_log "in pkg/library.sh: ''"
1001+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
1002+
}
1003+
1004+
function test_bash_runfiles_current_repository_binary_nobuild_runfile_links() {
1005+
setup_bash_runfiles_current_repository
1006+
1007+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --nobuild_runfile_links //pkg:binary \
1008+
&>"$TEST_log" || fail "Run should succeed"
1009+
expect_log "in pkg/binary.sh: ''"
1010+
expect_log "in pkg/library.sh: ''"
1011+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
1012+
1013+
RUNFILES_LIB_DEBUG=1 bazel run --enable_bzlmod --nobuild_runfile_links @other_repo//pkg:binary \
1014+
&>"$TEST_log" || fail "Run should succeed"
1015+
expect_log "in external/other_repo/pkg/binary.sh: 'other_repo'"
1016+
expect_log "in pkg/library.sh: ''"
1017+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
1018+
}
1019+
1020+
function test_bash_runfiles_current_repository_test_nobuild_runfile_links() {
1021+
setup_bash_runfiles_current_repository
1022+
1023+
bazel test --enable_bzlmod --noenable_runfiles --nobuild_runfile_links \
1024+
--test_env=RUNFILES_LIB_DEBUG=1 --test_output=all //pkg:test \
1025+
&>"$TEST_log" || fail "Test should succeed"
1026+
expect_log "in pkg/test.sh: ''"
1027+
expect_log "in pkg/library.sh: ''"
1028+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
1029+
1030+
bazel test --enable_bzlmod --noenable_runfiles --nobuild_runfile_links \
1031+
--test_env=RUNFILES_LIB_DEBUG=1 --test_output=all @other_repo//pkg:test \
1032+
&>"$TEST_log" || fail "Test should succeed"
1033+
expect_log "in external/other_repo/pkg/test.sh: 'other_repo'"
1034+
expect_log "in pkg/library.sh: ''"
1035+
expect_log "in external/other_repo/pkg/library2.sh: 'other_repo'"
1036+
}
1037+
7461038
run_suite "rules test"

0 commit comments

Comments
 (0)