We are generating a number of Go source files as part of our build. Previously we used a genrule
(example here) which resulted in the generated files being stored in bazel-genfiles/
.
We recently switched to using a custom rule as demonstrated in rules_go
(https://github.com/bazelbuild/rules_go/tree/master/examples/bindata). This change means that the output source files are stored in bazel-bin/
instead of bazel-genfiles/
.
This change of output location has broken Go integration in some of the IDEs used by our developers. Notably, gocode
, the autocompletion engine used by vim-go
and VSCode, when running in bzl
(Bazel) lookup mode seems to expect to find generated sources in bazel-genfiles/
, not bazel-bin/
, and therefore fails.
How do I modify my rule to save the output to bazel-genfiles/
instead of bazel-bin/
? My rule is equivalent to the example in rules_go
:
def _bindata_impl(ctx):
out = ctx.new_file(ctx.label.name + ".go")
ctx.action(
inputs = ctx.files.srcs,
outputs = [out],
executable = ctx.file._bindata,
arguments = [
"-o", out.path,
"-pkg", ctx.attr.package,
"-prefix", ctx.label.package,
] + [src.path for src in ctx.files.srcs],
)
return [
DefaultInfo(
files = depset([out])
)
]
bindata = rule(
_bindata_impl,
attrs = {
"srcs": attr.label_list(allow_files = True, cfg = "data"),
"package": attr.string(mandatory=True),
"_bindata": attr.label(allow_files=True, single_file=True, default=Label("@com_github_jteeuwen_go_bindata//go-bindata:go-bindata")),
},
)
I would expect an argument to either ctx.new_file
or ctx.action
but cannot find anything relevant in the Skylark reference or tutorial.
Many thanks!
Try setting output_to_genfiles=True
in the rule()
definition. It is mentioned in the rule docs.
So:
bindata = rule(
_bindata_impl,
attrs = {
"srcs": attr.label_list(allow_files = True, cfg = "data"),
"package": attr.string(mandatory=True),
"_bindata": attr.label(allow_files=True, single_file=True, default=Label("@com_github_jteeuwen_go_bindata//go-bindata:go-bindata")),
},
output_to_genfiles = True,
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With