# test.bzl
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
def _impl(ctx):
foo = ctx.actions.declare_file("foo.txt")
ctx.actions.run_shell(
command = "touch",
arguments = [foo.basename],
outputs = [foo],
)
pkg_tar(
name = "foo",
srcs = [foo]
)
example = rule(
outputs = {
"tar": "foo.tar",
},
implementation = _impl,
)
#BUILD
load("//:test.bzl", "example")
example(
name = "example",
)$ bazel build //:example
ERROR: /home/eli/Projects/bzltest/BUILD:3:1: in example rule //:example:
Traceback (most recent call last):
File "/home/eli/Projects/bzltest/BUILD", line 3
example(name = 'example')
File "/home/eli/Projects/bzltest/test.bzl", line 13, in _impl
pkg_tar(name = "foo", srcs = [foo])
File "/home/eli/.cache/bazel/_bazel_eli/2742e4ab532584f616e2c6fe2c69c68c/external/bazel_tools/tools/build_defs/pkg/pkg.bzl", line 207, in pkg_tar
_real_pkg_tar(**kwargs)
rule() can only be called during the loading phase
ERROR: Analysis of target '//:example' failed; build aborted: Analysis of target '//:example' failed; build aborted
INFO: Elapsed time: 0.181s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded)
I am not an expert, but I've had success using macros to combine multiple rules together: https://docs.bazel.build/versions/master/skylark/cookbook.html#macro_compoundEvan
On Monday, June 18, 2018 at 8:33:23 AM UTC-4, Eli Treuherz wrote:
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/d0f3ffb4-5954-4ed6-8a5a-4221ad643462%40googlegroups.com.--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
I don't think you can call a macro inside a rule. What you can do is exposing pkg_tar's rule implementation part, leaving the marco as a thin wrapper, and calling the implementation directly in another rule.Similar use case:- container_image rule: https://github.com/bazelbuild/rules_docker/blob/master/container/image.bzl#L16It exposes the container_image rules' implementation, and other rule can directly call it.Xin
--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAKcgio6cRYF99m7s47nB_MZVN%2BV3-1_V%2BrkR9%3DCBR3%3DuNfOGcA%40mail.gmail.com.
Xin Gao is right - rules cannot be called from within other rules (no matter native or Skylark).Refactoring of pkg rules so that their implementation is reusable will be welcome.
On Mon, Jun 18, 2018 at 5:29 PM, 'Xin Gao' via bazel-discuss <bazel-...@googlegroups.com> wrote:
I don't think you can call a macro inside a rule. What you can do is exposing pkg_tar's rule implementation part, leaving the marco as a thin wrapper, and calling the implementation directly in another rule.Similar use case:- container_image rule: https://github.com/bazelbuild/rules_docker/blob/master/container/image.bzl#L16It exposes the container_image rules' implementation, and other rule can directly call it.Xin
--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAKcgio6cRYF99m7s47nB_MZVN%2BV3-1_V%2BrkR9%3DCBR3%3DuNfOGcA%40mail.gmail.com.