diff --git a/examples/gem/BUILD b/examples/gem/BUILD index bdbe8594..302e2dc5 100644 --- a/examples/gem/BUILD +++ b/examples/gem/BUILD @@ -1,3 +1,5 @@ +load("@aspect_bazel_lib//lib:expand_template.bzl", "expand_template") +load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION") load( "@rules_ruby//ruby:defs.bzl", "rb_binary", @@ -9,6 +11,31 @@ load( package(default_visibility = ["//:__subpackages__"]) +expand_template( + name = "shell_wrapper", + out = "wrapper.sh", + data = [ + ":Rakefile", + ":rake", + ], + substitutions = { + "@@rlocation_lib@@": BASH_RLOCATION_FUNCTION.replace("$", "$$"), + "@@rake_binary@@": "$(rlocationpaths :rake)", + "@@rakefile@@": "$(rlocationpaths :Rakefile)", + }, + template = "wrapper.tpl.sh", +) + +sh_test( + name = "shell_wrapper_test", + srcs = [":shell_wrapper"], + data = [ + ":Rakefile", + ":rake", + ], + deps = ["@bazel_tools//tools/bash/runfiles"], +) + rb_binary( name = "rake", main = "@bundle//bin:rake", diff --git a/examples/gem/MODULE.bazel b/examples/gem/MODULE.bazel index 77587ac0..0cea7e97 100644 --- a/examples/gem/MODULE.bazel +++ b/examples/gem/MODULE.bazel @@ -1,6 +1,7 @@ "Bazel dependencies" bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True) +bazel_dep(name = "aspect_bazel_lib", version = "2.11.0", dev_dependency = True) bazel_dep(name = "rules_ruby", version = "0.0.0", dev_dependency = True) local_path_override( module_name = "rules_ruby", diff --git a/examples/gem/Rakefile b/examples/gem/Rakefile new file mode 100644 index 00000000..877fd8ba --- /dev/null +++ b/examples/gem/Rakefile @@ -0,0 +1,4 @@ +desc "say goodnight to bash" +task "goodnight" do + puts "exit 0" +end diff --git a/examples/gem/wrapper.tpl.sh b/examples/gem/wrapper.tpl.sh new file mode 100644 index 00000000..c2e715e7 --- /dev/null +++ b/examples/gem/wrapper.tpl.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +@@rlocation_lib@@ + +set -euo pipefail + +rake_binary_expansion=(@@rake_binary@@) + +echo "calling the rake binary" +rake="$(rlocation @@rake_binary@@)" +rakefile="$(rlocation @@rakefile@@)" +"$rake" -f "$rakefile" goodnight +set -x +eval "$("$rake" -f "$rakefile" goodnight)" +set +x + +echo "rake failed to fail" +echo "rlocationpaths for rake binary:" +printf ' %s\n' "${rake_binary_expansion[@]}" +exit 1 diff --git a/ruby/private/binary.bzl b/ruby/private/binary.bzl index b2ce84de..d7c8920e 100644 --- a/ruby/private/binary.bzl +++ b/ruby/private/binary.bzl @@ -129,13 +129,14 @@ def generate_rb_binary_script(ctx, binary, bundler = False, args = [], env = {}, # buildifier: disable=function-docstring def rb_binary_impl(ctx): bundler = False + bundler_srcs = [] env = {} java_bin = "" # TODO: avoid expanding the depset to a list, it may be expensive in a large graph - transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps).to_list() - transitive_deps = get_transitive_deps(ctx.attr.deps).to_list() - transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps).to_list() + transitive_data = get_transitive_data(ctx.files.data, ctx.attr.deps) + transitive_deps = get_transitive_deps(ctx.attr.deps) + transitive_srcs = get_transitive_srcs(ctx.files.srcs, ctx.attr.deps) ruby_toolchain = ctx.toolchains["@rules_ruby//ruby:toolchain_type"] if ctx.attr.ruby != None: @@ -148,14 +149,14 @@ def rb_binary_impl(ctx): tools.extend(java_toolchain.java_runtime.files.to_list()) java_bin = java_toolchain.java_runtime.java_executable_runfiles_path[3:] - for dep in transitive_deps: + for dep in transitive_deps.to_list(): # TODO: Remove workspace name check together with `rb_bundle()` if dep.label.workspace_name.endswith("bundle"): bundler = True if BundlerInfo in dep: info = dep[BundlerInfo] - transitive_srcs.extend([info.gemfile, info.bin, info.path]) + bundler_srcs.extend([info.gemfile, info.bin, info.path]) bundler = True # See https://bundler.io/v2.5/man/bundle-config.1.html for confiugration keys. @@ -163,13 +164,15 @@ def rb_binary_impl(ctx): "BUNDLE_GEMFILE": _to_rlocation_path(info.gemfile), "BUNDLE_PATH": _to_rlocation_path(info.path), }) + if len(bundler_srcs) > 0: + transitive_srcs = depset(bundler_srcs, transitive = [transitive_srcs]) bundle_env = get_bundle_env(ctx.attr.env, ctx.attr.deps) env.update(bundle_env) env.update(ruby_toolchain.env) env.update(ctx.attr.env) - runfiles = ctx.runfiles(transitive_srcs + transitive_data + tools) + runfiles = ctx.runfiles(tools, transitive_files = depset(transitive = [transitive_srcs, transitive_data])) runfiles = get_transitive_runfiles(runfiles, ctx.attr.srcs, ctx.attr.deps, ctx.attr.data) # Propagate executable from source rb_binary() targets. @@ -188,14 +191,13 @@ def rb_binary_impl(ctx): return [ DefaultInfo( executable = script, - files = depset(transitive_srcs + transitive_data + tools), runfiles = runfiles, ), RubyFilesInfo( binary = executable, - transitive_data = depset(transitive_data + tools), - transitive_deps = depset(transitive_deps), - transitive_srcs = depset(transitive_srcs), + transitive_data = depset(tools, transitive = [transitive_data]), + transitive_deps = transitive_deps, + transitive_srcs = transitive_srcs, bundle_env = bundle_env, ), RunEnvironmentInfo(