Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions busted/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions busted/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions examples/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions examples/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion examples/fennel/basic/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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"],
Expand Down
3 changes: 3 additions & 0 deletions examples/fennel/basic/main.fnl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(local example (require :fennel.basic.example))

(print (example.add-2 2))
17 changes: 17 additions & 0 deletions examples/fennel/basic/main_outside_runfiles_test.sh
Original file line number Diff line number Diff line change
@@ -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 ]]
9 changes: 9 additions & 0 deletions examples/lua/busted_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"],
)
7 changes: 7 additions & 0 deletions examples/lua/busted_test/maths_more_test.lua
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions examples/lua/busted_test/runs_all_files_test.sh
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion fennel/private/fennel_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions lua/private/binary_wrapper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 <runfiles>/_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")
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
4 changes: 2 additions & 2 deletions lua/private/lua_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions luaunit/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion luaunit/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down