Skip to content

Commit 5008f8a

Browse files
authored
fix: include rb_binary script wrapper in DefaultInfo
1 parent d7cd5d6 commit 5008f8a

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

examples/gem/BUILD

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template")
2+
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION")
13
load(
24
"@rules_ruby//ruby:defs.bzl",
35
"rb_binary",
@@ -9,6 +11,31 @@ load(
911

1012
package(default_visibility = ["//:__subpackages__"])
1113

14+
expand_template(
15+
name = "shell_wrapper",
16+
out = "wrapper.sh",
17+
data = [
18+
":Rakefile",
19+
":rake",
20+
],
21+
substitutions = {
22+
"@@rlocation_lib@@": BASH_RLOCATION_FUNCTION.replace("$", "$$"),
23+
"@@rake_binary@@": "$(rlocationpaths :rake)",
24+
"@@rakefile@@": "$(rlocationpaths :Rakefile)",
25+
},
26+
template = "wrapper.tpl.sh",
27+
)
28+
29+
sh_test(
30+
name = "shell_wrapper_test",
31+
srcs = [":shell_wrapper"],
32+
data = [
33+
":Rakefile",
34+
":rake",
35+
],
36+
deps = ["@bazel_tools//tools/bash/runfiles"],
37+
)
38+
1239
rb_binary(
1340
name = "rake",
1441
main = "@bundle//bin:rake",

examples/gem/MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"Bazel dependencies"
22

33
bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True)
4+
bazel_dep(name = "aspect_bazel_lib", version = "2.11.0", dev_dependency = True)
45
bazel_dep(name = "rules_ruby", version = "0.0.0", dev_dependency = True)
56
local_path_override(
67
module_name = "rules_ruby",

examples/gem/Rakefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
desc "say goodnight to bash"
2+
task "goodnight" do
3+
puts "exit 0"
4+
end

examples/gem/wrapper.tpl.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
@@rlocation_lib@@
4+
5+
set -euo pipefail
6+
7+
rake_binary_expansion=(@@rake_binary@@)
8+
9+
echo "calling the rake binary"
10+
rake="$(rlocation @@rake_binary@@)"
11+
rakefile="$(rlocation @@rakefile@@)"
12+
"$rake" -f "$rakefile" goodnight
13+
set -x
14+
eval "$("$rake" -f "$rakefile" goodnight)"
15+
set +x
16+
17+
echo "rake failed to fail"
18+
echo "rlocationpaths for rake binary:"
19+
printf ' %s\n' "${rake_binary_expansion[@]}"
20+
exit 1

ruby/private/binary.bzl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,14 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {},
129129
# buildifier: disable=function-docstring
130130
def rb_binary_impl(ctx):
131131
bundler = False
132+
bundler_srcs = []
132133
env = {}
133134
java_bin = ""
134135

135136
# TODO: avoid expanding the depset to a list, it may be expensive in a large graph
136-
transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps).to_list()
137-
transitive_deps = get_transitive_deps(ctx.attr.deps).to_list()
138-
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list()
137+
transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps)
138+
transitive_deps = get_transitive_deps(ctx.attr.deps)
139+
transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps)
139140

140141
ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"]
141142
if ctx.attr.ruby != None:
@@ -148,28 +149,30 @@ def rb_binary_impl(ctx):
148149
tools.extend(java_toolchain.java_runtime.files.to_list())
149150
java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:]
150151

151-
for dep in transitive_deps:
152+
for dep in transitive_deps.to_list():
152153
# TODO: Remove workspace name check together with `rb_bundle()`
153154
if dep.label.workspace_name.endswith("bundle"):
154155
bundler = True
155156

156157
if BundlerInfo in dep:
157158
info = dep[BundlerInfo]
158-
transitive_srcs.extend([info.gemfile, info.bin, info.path])
159+
bundler_srcs.extend([info.gemfile, info.bin, info.path])
159160
bundler = True
160161

161162
# See https://bundler.io/v2.5/man/bundle-config.1.html for confiugration keys.
162163
env.update({
163164
"BUNDLE_GEMFILE": _to_rlocation_path(info.gemfile),
164165
"BUNDLE_PATH": _to_rlocation_path(info.path),
165166
})
167+
if len(bundler_srcs) > 0:
168+
transitive_srcs = depset(bundler_srcs, transitive = [transitive_srcs])
166169

167170
bundle_env = get_bundle_env(ctx.attr.env, ctx.attr.deps)
168171
env.update(bundle_env)
169172
env.update(ruby_toolchain.env)
170173
env.update(ctx.attr.env)
171174

172-
runfiles = ctx.runfiles(transitive_srcs + transitive_data + tools)
175+
runfiles = ctx.runfiles(tools, transitive_files = depset(transitive = [transitive_srcs, transitive_data]))
173176
runfiles = get_transitive_runfiles(runfiles, ctx.attr.srcs, ctx.attr.deps, ctx.attr.data)
174177

175178
# Propagate executable from source rb_binary() targets.
@@ -188,14 +191,13 @@ def rb_binary_impl(ctx):
188191
return [
189192
DefaultInfo(
190193
executable = script,
191-
files = depset(transitive_srcs + transitive_data + tools),
192194
runfiles = runfiles,
193195
),
194196
RubyFilesInfo(
195197
binary = executable,
196-
transitive_data = depset(transitive_data + tools),
197-
transitive_deps = depset(transitive_deps),
198-
transitive_srcs = depset(transitive_srcs),
198+
transitive_data = depset(tools, transitive = [transitive_data]),
199+
transitive_deps = transitive_deps,
200+
transitive_srcs = transitive_srcs,
199201
bundle_env = bundle_env,
200202
),
201203
RunEnvironmentInfo(

0 commit comments

Comments
 (0)