Sometimes in my bazel rules I need to run a tool which produces bunch of files but writes them into output directory and directory path should be used as argument. So I ended up with following pattern:
out_dir = "{}_out".format(ctx.label)
out_files = []
for file in ctx.files.srcs:
file_path = # ... construct file path
o = ctx.actions.declare_file("{}/{}".format(out_dir, file_path))
out_files.append(o)
# Now we need to fetch first file and strip out file path part
out = out_files[0].path
out = out[0:(out.find(out_dir) + len(out_dir)]
But it feels very verbose and ugly and I can't find any good way to construct this path with rule attributes or something available at analysis phase. Is there any easy way to achieve this?