file location relative to filegroup

476 views
Skip to first unread message

Peter Grarup

unread,
Oct 13, 2022, 10:41:39 AM10/13/22
to bazel-discuss
I have a BUILD file that exposes a set of config files in a filegroup.

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

some of the files are located in subfolders relative to the BUILD file.

when I use this filegroup in a starlark rule the files contain the full path. Is there a way to get the root from where the file group was created?

Brian Silverman

unread,
Oct 14, 2022, 3:45:37 AM10/14/22
to Peter Grarup, bazel-discuss
pkg_tar(strip_prefix = ".") does this. rules_pkg has a system for mapping paths, I find that passing files around like that is usually easier than implementing the logic myself.

The core of the logic is applying dest_path to each element of `src[DefaultInfo].files.to_list()`. That's a function which determines a path, and removes a prefix if it's present. In the `strip_prefix = "."` case, this prefix is `compute_data_path(ctx, ".")`, which evaluates to `ctx.label.package`. The path itself is calculated in `safe_short_path` like this:
    working_path = file_.path
    if not file_.is_source:
        working_path = working_path[len(file_.root.path)+1:]

I think `working_path[len(ctx.label.package):]` will give you the result you're looking for, assuming your filegroup and your rule are in the same package. If not, `ctx.attr.src.label.package` will tell you the package for the filegroup being passed as an attribute.

If you're looking for any other tricky path manipulation, I find that rules_pkg has lots of examples.


--
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/1708ffae-7c5d-4173-9898-392b2e991bfdn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages