Using pkg_tar from Skylark rules

1,425 views
Skip to first unread message

Eli Treuherz

unread,
Jun 18, 2018, 8:33:23 AM6/18/18
to bazel-discuss
As far as I can tell, now that rules like pkg_tar are being ported to Skylark instead of being native, there is no way to use them from custom Skylark rules. If you try, pkg_tar's rule() call is run during the analysis of your custom rule, rather than the loading phase, and fails. I know I can still tar things by running tar directly with actions.run_shell, but this means I either have to go without, or re-implement, pkg_tar's most useful features like prefix-stripping.

Am I going about this wrong, or is there no way to use Skylark rules within other rules?

Example files:

# 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)


Evan Jones

unread,
Jun 18, 2018, 9:25:12 AM6/18/18
to bazel-discuss
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_compound

Evan

Xin Gao

unread,
Jun 18, 2018, 11:30:14 AM6/18/18
to e.tre...@gmail.com, Evan Jones, bazel-discuss
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:
It exposes the container_image rules' implementation, and other rule can directly call it.

Xin


On Mon, Jun 18, 2018 at 6:25 AM, Evan Jones <evan....@bluecore.com> wrote:
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_compound

Evan



On Monday, June 18, 2018 at 8:33:23 AM UTC-4, Eli Treuherz wrote:

--
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/d0f3ffb4-5954-4ed6-8a5a-4221ad643462%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Dmitry Lomov

unread,
Jun 18, 2018, 4:27:34 PM6/18/18
to Xin Gao, e.tre...@gmail.com, Evan Jones, bazel-discuss
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:
It 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.

For more options, visit https://groups.google.com/d/optout.



--
Google Germany GmbH
Erika-Mann-Straße 33, 80636 München, Germany

Tony Aiuto

unread,
Aug 7, 2019, 11:01:52 AM8/7/19
to bazel-discuss
Closing the loop a bit. We have migrated pkg_tar from Bazel to a standalone repository. https://github.com/bazelbuild/rules_pkg
This does not immediately help with the original question but it does significantly lower the bar to refactoring the rule so that the implementation is reusable.




On Monday, June 18, 2018 at 4:27:34 PM UTC-4, Dmitry Lomov wrote:
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:
It 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.
Reply all
Reply to author
Forward
0 new messages