diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 2743260..5fdd2cc 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -294,7 +294,7 @@ "moduleExtensions": { "//fennel:repositories.bzl%fennel_toolchains": { "general": { - "bzlTransitiveDigest": "RAQhfZyd702GAN//auQ2qbueUbI+Gb4On52TsK7698I=", + "bzlTransitiveDigest": "q+speGe+9kFNEr6MGn//rcVwFgU4odaX9Wwe+P3gFqo=", "usagesDigest": "QY7RI/hzhtcQ0ukhlBAlQahalci51yANMWBckvVbR+M=", "recordedInputs": [ "REPO_MAPPING:,aspect_bazel_lib aspect_bazel_lib+", diff --git a/busted/MODULE.bazel.lock b/busted/MODULE.bazel.lock index 878d8da..927f8c2 100644 --- a/busted/MODULE.bazel.lock +++ b/busted/MODULE.bazel.lock @@ -321,7 +321,7 @@ }, "@@rules_lua+//fennel:repositories.bzl%fennel_toolchains": { "general": { - "bzlTransitiveDigest": "RAQhfZyd702GAN//auQ2qbueUbI+Gb4On52TsK7698I=", + "bzlTransitiveDigest": "q+speGe+9kFNEr6MGn//rcVwFgU4odaX9Wwe+P3gFqo=", "usagesDigest": "psvo7k11C5WChexTj8zxZ2fwbdWNu+ItmV/GPhdMKvk=", "recordedInputs": [ "REPO_MAPPING:aspect_bazel_lib+,bazel_skylib bazel_skylib+", @@ -367,7 +367,7 @@ }, "@@rules_lua+//lua:defs.bzl%lua_dependency": { "general": { - "bzlTransitiveDigest": "WuTbStJXheaH5kSdt7sd0bmu2uwhqG3adUZO9WIm84M=", + "bzlTransitiveDigest": "8KqDNhcGQwIRr3depi9O9g3bI2JvIV2zj9NhJjIrBKg=", "usagesDigest": "xNHJ+QKDa+KhCyCvzAnmS9nmOHcUZEMmkStvaOZ/Bos=", "recordedInputs": [ "REPO_MAPPING:,lua_ansicolors rules_lua++lua_dependency+lua_ansicolors", diff --git a/busted/defs.bzl b/busted/defs.bzl index 30339c5..20204ff 100644 --- a/busted/defs.bzl +++ b/busted/defs.bzl @@ -23,10 +23,10 @@ set -e $(rlocation {runner}) \ --verbose \ -Xoutput --color --output=utfTerminal \ - $(rlocation {srcs}) $@ + {srcs} "$@" """.format( runner = to_rlocation_path(ctx, ctx.file._busted), - srcs = " ".join([to_rlocation_path(ctx, i) for i in srcs]), + srcs = " ".join(['"$(rlocation {})"'.format(to_rlocation_path(ctx, i)) for i in srcs]), ), is_executable = True, ) diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index 438605c..14ed06e 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel @@ -7,6 +7,7 @@ local_path_override( bazel_dep(name = "aspect_bazel_lib", version = "2.22.5") bazel_dep(name = "rules_foreign_cc", version = "0.17.0") +bazel_dep(name = "rules_shell", version = "0.8.0") # Use the host make, as building it from source runs hundreds of slow autoconf check actions foreign_cc_tools = use_extension("@rules_foreign_cc//foreign_cc:extensions.bzl", "tools") diff --git a/examples/MODULE.bazel.lock b/examples/MODULE.bazel.lock index ff55247..7a5c650 100644 --- a/examples/MODULE.bazel.lock +++ b/examples/MODULE.bazel.lock @@ -321,7 +321,7 @@ }, "@@rules_lua+//fennel:repositories.bzl%fennel_toolchains": { "general": { - "bzlTransitiveDigest": "RAQhfZyd702GAN//auQ2qbueUbI+Gb4On52TsK7698I=", + "bzlTransitiveDigest": "q+speGe+9kFNEr6MGn//rcVwFgU4odaX9Wwe+P3gFqo=", "usagesDigest": "AaPeEFABASgi89ywqhqIpG0D7KrQeFM9vEmaZ8TQBvM=", "recordedInputs": [ "REPO_MAPPING:aspect_bazel_lib+,bazel_skylib bazel_skylib+", @@ -367,7 +367,7 @@ }, "@@rules_lua+//lua:defs.bzl%lua_dependency": { "general": { - "bzlTransitiveDigest": "WuTbStJXheaH5kSdt7sd0bmu2uwhqG3adUZO9WIm84M=", + "bzlTransitiveDigest": "8KqDNhcGQwIRr3depi9O9g3bI2JvIV2zj9NhJjIrBKg=", "usagesDigest": "dwEeHFgGi854dDEZYSa/789mSpzGnxdN50iekSzRPYk=", "recordedInputs": [ "REPO_MAPPING:aspect_bazel_lib+,bazel_skylib bazel_skylib+", diff --git a/examples/fennel/basic/BUILD.bazel b/examples/fennel/basic/BUILD.bazel index 6f5143c..5ba3a19 100644 --- a/examples/fennel/basic/BUILD.bazel +++ b/examples/fennel/basic/BUILD.bazel @@ -1,11 +1,25 @@ -load("@rules_lua//fennel:defs.bzl", "fennel_library") +load("@rules_lua//fennel:defs.bzl", "fennel_binary", "fennel_library") load("@rules_lua_luaunit//:defs.bzl", "fennel_luaunit_test") +load("@rules_shell//shell:sh_test.bzl", "sh_test") fennel_library( name = "example", srcs = [":example.fnl"], ) +fennel_binary( + name = "main", + tool = "main.fnl", + deps = [":example"], +) + +sh_test( + name = "main_outside_runfiles_test", + srcs = ["main_outside_runfiles_test.sh"], + args = ["$(rootpath :main)"], + data = [":main"], +) + fennel_luaunit_test( name = "example_test", srcs = ["test_example.fnl"], diff --git a/examples/fennel/basic/main.fnl b/examples/fennel/basic/main.fnl new file mode 100644 index 0000000..98c6a1b --- /dev/null +++ b/examples/fennel/basic/main.fnl @@ -0,0 +1,3 @@ +(local example (require :fennel.basic.example)) + +(print (example.add-2 2)) diff --git a/examples/fennel/basic/main_outside_runfiles_test.sh b/examples/fennel/basic/main_outside_runfiles_test.sh new file mode 100755 index 0000000..1e069d3 --- /dev/null +++ b/examples/fennel/basic/main_outside_runfiles_test.sh @@ -0,0 +1,17 @@ +#!/usr/bin/env bash +# Runs the binary from a working directory outside the runfiles tree, as a direct bazel-bin/ run does, +# once with a runfiles tree and once with only a manifest (which one Bazel makes depends on the platform) +set -euo pipefail + +bin="$PWD/$1" +# The test sandbox has no runfiles manifest, so build one from the runfiles tree +find -L "$RUNFILES_DIR" -mindepth 1 -printf '%P %p\n' >"$TEST_TMPDIR/MANIFEST" +cd "$TEST_TMPDIR" + +out=$("$bin") +echo "runfiles tree: $out" +[[ "$out" == 4 ]] + +out=$(env -u RUNFILES_DIR RUNFILES_MANIFEST_FILE="$TEST_TMPDIR/MANIFEST" "$bin") +echo "manifest only: $out" +[[ "$out" == 4 ]] diff --git a/examples/lua/busted_test/BUILD.bazel b/examples/lua/busted_test/BUILD.bazel index d624ea5..3da3d42 100644 --- a/examples/lua/busted_test/BUILD.bazel +++ b/examples/lua/busted_test/BUILD.bazel @@ -1,5 +1,6 @@ load("@rules_lua//lua:defs.bzl", "lua_library") load("@rules_lua_busted//:defs.bzl", "busted_test") +load("@rules_shell//shell:sh_test.bzl", "sh_test") lua_library( name = "maths", @@ -9,7 +10,15 @@ lua_library( busted_test( name = "test_maths", srcs = [ + ":maths_more_test.lua", ":maths_test.lua", ], deps = [":maths"], ) + +sh_test( + name = "test_maths_runs_all_files", + srcs = ["runs_all_files_test.sh"], + args = ["$(rootpath :test_maths)"], + data = [":test_maths"], +) diff --git a/examples/lua/busted_test/maths_more_test.lua b/examples/lua/busted_test/maths_more_test.lua new file mode 100644 index 0000000..1e3f74a --- /dev/null +++ b/examples/lua/busted_test/maths_more_test.lua @@ -0,0 +1,7 @@ +maths_mod = require("lua.busted_test.maths") + +describe("Adding more numbers", function() + it("Can add 2 to a negative number", function() + assert.are.equal(1, maths_mod.add_2(-1)) + end) +end) diff --git a/examples/lua/busted_test/runs_all_files_test.sh b/examples/lua/busted_test/runs_all_files_test.sh new file mode 100755 index 0000000..9d0a9d4 --- /dev/null +++ b/examples/lua/busted_test/runs_all_files_test.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# Fails if the busted launcher drops any test file after the first +set -euo pipefail + +out=$("$1" --output=TAP) +echo "$out" +grep -q '^1\.\.2$' <<<"$out" diff --git a/fennel/private/fennel_binary.bzl b/fennel/private/fennel_binary.bzl index 298f57d..51251a8 100644 --- a/fennel/private/fennel_binary.bzl +++ b/fennel/private/fennel_binary.bzl @@ -20,7 +20,7 @@ def _fennel_binary_impl(ctx): set -e set +u export LUA_PATH="$LUA_PATH;$(alocation $(dirname $(rlocation {wrapper})))/?.lua" -$(rlocation {lua_rloc}) -l binary_wrapper $(rlocation {fennel_rloc}) $(rlocation {tool}) $@ +$(rlocation {lua_rloc}) -l binary_wrapper $(rlocation {fennel_rloc}) $(rlocation {tool}) "$@" """.format( lua_rloc = to_rlocation_path(ctx, lua_executable), fennel_rloc = to_rlocation_path(ctx, fennel_executable), diff --git a/lua/private/binary_wrapper.lua b/lua/private/binary_wrapper.lua index 8a2e9a4..20feac6 100644 --- a/lua/private/binary_wrapper.lua +++ b/lua/private/binary_wrapper.lua @@ -125,6 +125,10 @@ if os.getenv("RUNFILES_DIR") then local relevant = read_lua_mappings() local runfiles_dir = os.getenv("RUNFILES_DIR") + -- "?.lua" above only finds main repo modules when the working directory is /_main + add_to_path(runfiles_dir .. "/_main/?.lua") + add_to_path(runfiles_dir .. "/_main/?/init.lua") + for k, v in pairs(relevant) do add_to_path(runfiles_dir .. "/?.lua") add_to_path(runfiles_dir .. "/?/init.lua") @@ -194,6 +198,8 @@ elseif os.getenv("RUNFILES_MANIFEST_FILE") then inverted[v[1]] = k end + local manifest = {} + for line in io.lines(os.getenv("RUNFILES_MANIFEST_FILE")) do local t = {} -- // a manifest file that maps logical runfile paths to absolute paths paths for the real files in Bazel's cache. @@ -219,6 +225,7 @@ elseif os.getenv("RUNFILES_MANIFEST_FILE") then for str in string.gmatch(line, "([^ ]+)") do table.insert(t, str) end + manifest[t[1]] = t[2] local folder_name = string.gsub(t[1], "/.*", "") if inverted[folder_name] and is_relevant(folder_name) then @@ -227,6 +234,20 @@ elseif os.getenv("RUNFILES_MANIFEST_FILE") then add_to_path(root .. "/share/lua/" .. lua_numeric_ver .. "/" .. "?.lua") end end + + -- Main repo modules are split between the source tree and bazel-bin, so no package.path entry covers them + local loaders = package.searchers or package.loaders + table.insert(loaders, 2, function(module_name) + local base = "_main/" .. module_name:gsub("[.]", "/") + for _, candidate in ipairs({ base .. ".lua", base .. "/init.lua" }) do + local resolved = manifest[candidate] + if resolved then + return assert(loadfile(resolved)), resolved + end + end + + return "\n\tno entry '" .. base .. ".lua' in runfiles manifest" + end) else error("unable to find runfiles") end diff --git a/lua/private/lua_binary.bzl b/lua/private/lua_binary.bzl index 838bb56..c6bfb26 100644 --- a/lua/private/lua_binary.bzl +++ b/lua/private/lua_binary.bzl @@ -7,7 +7,7 @@ load("//lua:providers.bzl", "LuaLibrary") # See windows_utils.bzl for the cmd.exe equivalent. # Vendored from # https://github.com/bazelbuild/bazel/blob/master/tools/bash/runfiles/runfiles.bash -BASH_RLOCATION_FUNCTION = _default_location_function + r""" +BASH_RLOCATION_FUNCTION = "#!/usr/bin/env bash" + _default_location_function + r""" function alocation { local P=$1 if [[ "${P:0:1}" == "/" ]]; then @@ -30,7 +30,7 @@ def _lua_binary_impl(ctx): set -e set +u export LUA_PATH="$LUA_PATH;$(alocation $(dirname $(rlocation {wrapper})))/?.lua" -$(rlocation {lua_rloc}) -l binary_wrapper $(rlocation {tool}) $@ +$(rlocation {lua_rloc}) -l binary_wrapper $(rlocation {tool}) "$@" """.format( lua_rloc = to_rlocation_path(ctx, lua_executable), tool = to_rlocation_path(ctx, ctx.file.tool), diff --git a/luaunit/MODULE.bazel.lock b/luaunit/MODULE.bazel.lock index 375014c..83f6519 100644 --- a/luaunit/MODULE.bazel.lock +++ b/luaunit/MODULE.bazel.lock @@ -343,7 +343,7 @@ }, "@@rules_lua+//fennel:repositories.bzl%fennel_toolchains": { "general": { - "bzlTransitiveDigest": "RAQhfZyd702GAN//auQ2qbueUbI+Gb4On52TsK7698I=", + "bzlTransitiveDigest": "q+speGe+9kFNEr6MGn//rcVwFgU4odaX9Wwe+P3gFqo=", "usagesDigest": "EmvQ57tjeHO5q/wJDSit88pQQp9C4OHO5c92wSuAUx0=", "recordedInputs": [ "REPO_MAPPING:aspect_bazel_lib+,bazel_skylib bazel_skylib+", @@ -389,7 +389,7 @@ }, "@@rules_lua+//lua:defs.bzl%lua_dependency": { "general": { - "bzlTransitiveDigest": "WuTbStJXheaH5kSdt7sd0bmu2uwhqG3adUZO9WIm84M=", + "bzlTransitiveDigest": "8KqDNhcGQwIRr3depi9O9g3bI2JvIV2zj9NhJjIrBKg=", "usagesDigest": "UZSZfKBjDOuSf9XfQFNkpHin/ADgrXbg5Yi42po6FBk=", "recordedInputs": [ "REPO_MAPPING:aspect_bazel_lib+,bazel_skylib bazel_skylib+", diff --git a/luaunit/defs.bzl b/luaunit/defs.bzl index b6a9835..7f7535d 100644 --- a/luaunit/defs.bzl +++ b/luaunit/defs.bzl @@ -22,7 +22,7 @@ def luaunit_test_impl(ctx, srcs): BASH_RLOCATION_FUNCTION + """ set -e export TEST_FILES={srcs} -$(rlocation {runner}) $@ +$(rlocation {runner}) "$@" """.format( runner = to_rlocation_path(ctx, ctx.file._runner), srcs = ",".join([to_rlocation_path(ctx, i) for i in srcs]),