Search code examples
bazel

Getting "name 'genrule' is not defined (did you mean 'rule'?)" error when using genrule in .bzl files


I can see everywhere in docs, SO questions, github, etc. that people are using genrule without any direct load statement, but when I try to use it in any .bzl file, I get the following error and I could not find out how to resolve this:

name 'genrule' is not defined (did you mean 'rule'?)

I've tried calling it like this:

genrule(
    name = "hello20s",
    outs = ["hello20s.txt"],
    cmd_bash = "sleep 20 && echo 'hello20s' >$@",
)

and I tried to replace cmd_bash with cmd and had no luck either. I don't think it is a matter of how or with what arguments to call it, but more load or env issue. Any ideas?

I can add, that I'm able to run similar commands using ctx.actions.run_shell within the same .bzl file, so the only glitch I have so far is that it doesn't recognize genrule specifically.

$ bazel verson

Bazelisk version: development

Build label: 5.1.1

OS: macOS 12.3.1


Solution

  • In macros defined in .bzl files, the "builtin" rules and function need to be referenced with the native prefix, eg native.genrule.

    However, rules can't be called in the context of another rules implementation function, only mention this as you mention calling ctx.actions.shell