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

We generate several Go source files as part of our assembly. We used the example genrule( ) before, as a result of which the generated files were saved in bazel-genfiles/.

We recently switched to using a custom rule, as shown in rules_go( https://github.com/bazelbuild/rules_go/tree/master/examples/bindata ). This change means that the original source files are stored in bazel-bin/instead bazel-genfiles/.

This change in output location disrupted Go's integration in some IDEs used by our developers. It is noteworthy that gocodethe autocomplete mechanism used by vim-goVSCode when starting in search mode bzl(Bazel) seems to expect to find the generated sources in bazel-genfiles/, rather than bazel-bin/, and therefore will not work.

How do I change my rule to keep the output in bazel-genfiles/instead 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 of either ctx.new_file, or ctx.action, but cannot find anything relevant in the Skylark link or tutorial.

Many thanks!

+4
source share
1 answer

output_to_genfiles=True rule(). .

:

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,
    )
+6

Source: https://habr.com/ru/post/1683322/


All Articles