In implementation of a bazel aspect, is there a way to get the absolute filesystem path of workspace root?

1.2k Views Asked by At

I am trying to implement the generation of clang compilation database in bazel aspect. It appears that the "directory" field of this clang file must be specified as absolute path. Now when I try to generate this file with bazel aspect, all relevant bazel APIs returns path relative path in side the workspace.

By "workspace root" I mean ~/.cache/bazel/_bazel_<user>/<hash>/execroot/__main__.

I have thought of two hacks to make this work:

  1. Use ctx.actions.run_shell instead of ctx.actions.write to generate the file where shell could evaluate workspace root path from sandbox PWD with $(realpath ../../../../../execroot/__main__).

I have verified this to work but this is certainly undesired approach since bazel makes no guarantee how sandbox work path would relate to workspace root, thus may easily break across bazel version.

  1. Use a script to generate the file.

This would break the generation of the file into two pieces as all compile commands information still have to come from bazel aspect, which makes it a nuisance to maintain.

Another way I thought about but see no apparent way to make it work is to somehow pass the __workspace_dir__ variable from WORKSPACE.bazel file to the aspect context, which I have not find anyway to do.

So, is there any elegant way of getting this info from aspect implementation context?

0

There are 0 best solutions below