Search code examples
bazel

During 'bazel build', when is `target.runfiles` directory properly set up?


For a cc_binary (or py_binary, sh_binary), when does bazel create its runfiles directory and have all symlinks correctly set up?

  • Is it created right after the cc_binary is built, and before any rule that takes this cc_binary as input, OR
  • Is it created after the whole build process of all targets is finished?

I'm trying to write a custom rule to pack the contents in the runfiles directory of a cc_binary into a tarball. This custom rule takes the cc_binary and all targets in its runfiles as input. If the runfiles directory is properly set up right after the cc_binary is built, then I just need to directly pack this directory. If not, I probably need to set up a tmp runfiles directory by myself in my custom rule.

Also, is this behavior guaranteed to keep in the future releases?

Thanks a lot!


Solution

  • Bazel does not guarantee any ordering; all it guarantees is that by the end of the build, both the binary and the runfiles tree should be there.

    If you want an action to be executed after both of these have been built, you need to depend on both the binary and its runfiles manifest. This doesn't guarantee, though, that the runfiles tree will be in the sandbox the packing action rules on.

    I think it's possibly to craft something from ctx.actions.run(input_manifests=) and ctx.resolve_command that does that, although it's very arcane at the moment.