bazelbazel-rulesstarlark

Bazel Question: `filegroup` to `declare_directory`


I have a general question about how to make filegroup into a declare_directory.

So what I can think about is like, filegroup -> pkg_tar -> untar_to_dir(A rule to untar the tarball into the declare_directory).

It would work, and I already proved it. However, I want to know whether any other more graceful method to avoid tar and untar the filegroup?

One idea I tried, but much complicated, is to copy them into the declare_directory file by file. But the tricky point is, it could not gracefully deal with keeping the same dir structure in the new dir. (like I have to mkdir a lot of sub-directory before cp).


Solution

  • From another thread

    You can just use `cp` instead of `tar`, like this rule does https://github.com/aspect-build/bazel-lib/blob/main/docs/copy_to_directory.md
    it takes sources which can include filegroup, and outputs a TreeArtifact (ctx.actions_declare_directory)
    

    And actually I wrote a func files_to_dir and did the same thing.