@@ -743,4 +743,296 @@ EOF
743
743
bazel run //pkg:my_executable > $TEST_log 2>&1 || fail " Binary should have exit code 0"
744
744
}
745
745
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
+
746
1038
run_suite " rules test"
0 commit comments