1

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?

xiay
  • 855
  • 8
  • 19
  • The issues you've raised aren't aspect-specific, they apply to any rule that needs the workspace path. See https://stackoverflow.com/questions/48545575/how-to-get-workspace-directory-in-bazel-rule, which answers the question. To summarize, use `run_shell` with specific `execution_requirements`, or write a repository rule. – Rohan Singh Aug 19 '20 at 13:52
  • Does this answer your question? [How to get WORKSPACE directory in bazel rule](https://stackoverflow.com/questions/48545575/how-to-get-workspace-directory-in-bazel-rule) – Rohan Singh Aug 19 '20 at 13:53
  • @RohanSingh, not really. the `run_shell` approach is basically my approach 1 which have no means to read the value back into bazel context. The repository rule could actually use `__workspace_dir__` passed from `WORKSPACE` instead but does not solve my problem. – xiay Aug 19 '20 at 17:58
  • the question @RohanSingh linked is indeed a duplicate of this, but unfortunately the asker of that question has settled for the same workaround xiay posted. It would be great to have a real solution. – TamaMcGlinn Oct 20 '22 at 13:29

0 Answers0