dependenciesbazelbazel-extra-action

Is there any way to generate the deps list for particular rules?


I have a program that can look at my language's source file, and derive the correct value for the deps=[] value of its build rule.

I'm looking for a way to replace all my existing rules (that look like this):

build_lib(name = "foo", deps = [...])
build_lib(name = "bar", deps = [...])
build_lib(name = "baz", deps = [...])

To instead be:

build_lib_new(name = "foo")
build_lib_new(name = "bar")
build_lib_new(name = "baz")

with the same specified deps resolved internally in the rule by calling my program.

Ideally, build_lib_new would just be a wrapper rule around build_lib:

def derive_deps(name):
    deps = []
    # call my tool somehow?
    return deps

def build_lib_new(name):
    deps = derive_deps(name)
    build_lib(name,deps)

Now I'm stuck. Unfortunately, I think bazel wants to know all the dependencies up front as part of the analysis phase. I see that their are actions to run shell commands, but I believe those happen after the dependency graph is made.

Do I have to run the external tool outside of bazel to rewrite BUILD files?


Solution

  • Do I have to run the external tool outside of bazel to rewrite BUILD files?

    In short, yes. This is why tools like Gazelle and Jadep exist.

    If your tool runs as actions during execution phase, then the deps would be non-existent during loading and analysis phase. You'll need to run the tool before loading/analysis, maybe as a repository rule?

    I see that their are actions to run shell commands, but I believe those happen after the dependency graph is made.

    Correct. The analysis phase creates the configured target graph, and reifies it into the action (shell commands, artifacts, etc) graph for the execution phase.