Get base directory of a filegroup

2,259 views
Skip to first unread message

Christopher Kilian

unread,
Mar 9, 2022, 7:15:56 PM3/9/22
to bazel-discuss
Hi all,

I'm looking for a way to get the base directory for all the files in a file group. For this usecase I'm using http_archive to download a folder and creating a filegroup with all the files in the archive. I want to be able to pass the location of the directory through a jvm flag. In Buck, calling $(location :my_filegroup) will expand to the directory containing the files in my filegroup, but in Bazel it expands to a list of all files in the filegroup separated by whitespace. The directory structure of the archive can be very deep, so it's not as simple as taking one of the files and calling File.dirname, since this might not be the directory that contains all the files. Is there a way I can get the directory containing the files in my filegroup in Bazel?

Thank you!

Chris

Alex Humesky

unread,
Mar 14, 2022, 8:03:31 PM3/14/22
to Christopher Kilian, bazel-discuss
Is this for a genrule or for a Starlark rule?
"$(location :my_filegroup)" makes me think genrule, but "file.dirname" makes me think Starlark rule.

Either way, as far as I'm aware, there's no built-in way to do this. Python has os.path.commonpath but that's not implemented in Starlark, nor in Skylib.

So in the general case, you'd need to implement this either in bash/shell or in Starlark. However, since you're using an external repository rule (http_archive), there's maybe a shortcut. The external archive will be located in a directory at "external/<repository name>" in the execution root (i.e. where the action is executed) for the action that depends on the filegroup. That will be common to all the files in the archive. If that's too high up in the directory structure though, you'll probably have to implement something to find the appropriate directory.

--
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/d8467666-c1df-4d0f-b687-9f610440bf7fn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages