2 years ago
#54230
Joe
Running Bazel executable in another Bazel rule fails to find runfiles
I have a go_binary in Bazel called dbgen
that generates Go files from SQL files. I want to create a new Bazel rule that wraps the dbgen
binary to create the Go files in Bazel. However, I hit the following error when building with the new rule.
could not locate runfiles directory
How do I fix this error in the Bazel rule implementation below?
Details
The dbgen
command that I want to wrap works if I invoke it with bazel run directly, meaning it can access the runfiles.
bazel run dbgen -- --pggen=./pggen --query-files foo.pggen.sql
Here's the rule I'm attempting to create. The error could not locate runfiles directory
is a result calling the bazel.ListRunfiles
Go library in rules_go. It seems that when Bazel executes the ctx.actions.run
that the command doesn't get the runfiles of the tools
dependencies.
rule.bzl
def _erp_pggen_impl(ctx):
args = ctx.actions.args()
args.add("--pggen", ctx.executable._pggen.path)
args.add("--query-files", ",".join([s.path for s in ctx.files.srcs]))
outs = [
ctx.actions.declare_file(file.path.replace(".pggen.sql", ".pggen.sql.go"))
for file in ctx.files.srcs
]
ctx.actions.run(
inputs = [ctx.executable._pggen] + ctx.files.srcs,
outputs = outs,
executable = ctx.attr._dbgen.files_to_run,
tools = [ctx.executable._dbgen],
arguments = [args],
mnemonic = "ErpDbgen",
)
return [
DefaultInfo(files = depset(outs)),
OutputGroupInfo(go_generated_srcs = outs),
]
erp_pggen = rule(
implementation = _erp_pggen_impl,
attrs = {
"srcs": attr.label_list(allow_files = [".pggen.sql"]),
"_dbgen": attr.label(
executable = True,
cfg = "host",
default = Label("//erp/db/dbgen:dbgen"),
),
"_pggen": attr.label(
executable = True,
cfg = "host",
default = Label("//third_party/go/pggen:pggen"),
),
}
)
Related issues
- https://github.com/bazelbuild/bazel/issues/5463: issue setting the runfiles for the scala toolchain.
Bazel version: 4.2 OS: macOS
dependencies
bazel
bazel-rules
0 Answers
Your Answer