Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put the output of a custom rule in `bazel-genfiles/` instead of `bazel-out/`?

Tags:

bazel

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!

like image 662
user2514169 Avatar asked Aug 09 '17 09:08

user2514169


1 Answers

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,
    )
like image 129
zlalanne Avatar answered Nov 19 '22 00:11

zlalanne