How to get glob results from the different package?

818 views
Skip to first unread message

Konstantin

unread,
Mar 21, 2022, 11:41:49 PM3/21/22
to bazel-discuss
I have a macro running in one package. In that macro I need to know what files are present in another package. I can run glob in the second package to get the list of files, but I don't see a way to pass that list to the macro in the first package. Any clue?

Thank you!
Konstantin

Alex Humesky

unread,
Mar 25, 2022, 4:23:09 PM3/25/22
to Konstantin, bazel-discuss
Typically for one package to get files from another package, you would define a file group in the other package with the files you're interested in:

foo/BUILD:

filegroup(
  name = "files",
  srcs = glob(["*.ext"]),
  visibility = ["//visibility:public"],
)

bar/BUILD:

java_binary(
  ...,
  resources = ["//foo:files"],
  ...,
)

However, a macro cannot inspect the values or providers of a target (i.e. //foo:files). So the next thing one might try is to glob the files and put that into a variable:

foo/BUILD:
FILES = glob(["*.ext"])

but the problem then is that you can't load that variable into another BUILD file:

load("//foo:BUILD", "FILES")

def macro(...):
  if "foo" in FILES:
    ...


that would give:
ERROR: BUILD:1:6: in load statement: The label must reference a file with extension '.bzl'

Is there any way that you can move what you'd like to do into Starlark? Then you can have a "srcs" attribute or similar, put the filegroup in there, and it would work as usual in Starlark.


--
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-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/323586fc-d762-4b60-83ff-e3c4f3669234n%40googlegroups.com.

Alex Humesky

unread,
Mar 25, 2022, 4:27:40 PM3/25/22
to Konstantin, bazel-discuss
and by "into Starlark", I mean into a Starlark rule (macros are still Starlark afterall)

One other option might be to merge the packages together, then the glob() will work (since globs don't cross package boundaries), but I don't know if that works with the constraints you have.

Konstantin

unread,
Mar 26, 2022, 12:11:21 AM3/26/22
to bazel-discuss
Alex, thank you for your answer! 

Unfortunately what I wanted does not seem to be possible. I cannot move the logic into a rule and I cannot merge packages. 
For my specific case I was able to work around this problem without globbing files from another package, but my workaround is not reusable in general.

Konstantin

Alex Humesky

unread,
Mar 28, 2022, 6:31:35 PM3/28/22
to Konstantin, bazel-discuss
Gotcha, yeah I'm not sure that this is possible, glad though that you were able to find a workaround!

Reply all
Reply to author
Forward
0 new messages