0

I'm trying to set an environment variable for my mocha_test rule in a Bazel BUILD file to provide the absolute path to a directory that's created using a genrule. The genrule is responsible for creating the directory and processing some dependencies, but I'm not sure how to pass the correct absolute path using "location".

Here's my current BUILD file:

genrule(
    name = "app_dir_rule",
    srcs = [],
    outs = ["app_dir"],
    cmd = """
        mkdir -p $@

        $(location :app_binary) process_dependencies --output=$@/vendor
        cp $(location :app_binary) $@
    """,
    tools = [":app_binary"],
    visibility = ["//visibility:private"],
)

mocha_test(
    name = "mocha",
    args = ["--inspect"],
    chdir = package_name(),
    data = [
        ":.mocharc.js",
        ":.prettierignore",
        ...
        ":app_dir_rule"
    ],
    env = {
        "APP_DIR": "$(location :app_dir_rule)"
    },
    tags = ["local"],
)

Could anyone help me to figure out the correct way to pass the absolute path for my application directory in this Bazel BUILD file??

rakib
  • 131
  • 3
  • 10
  • Hi, Can you give a try using execpath function instead of location? `location` returns the relative path whereas `execpath` function returns the absolute path to the output directory. If you want to run a tool from within a genrule, the recommended way to get its path is $([execpath](https://bazel.build/reference/be/make-variables#predefined_label_variables) toolname), where [toolname](https://bazel.build/reference/be/general#genrule.tools) must be listed in the genrule's tools attribute. Please refer [bazel doc](https://bazel.build/reference/be/make-variables) – Pavan Singh Jun 09 '23 at 06:21
  • `execpath` still giving me relative path. `bazel-out/darwin_arm64-fastbuild/bin/domains/app_dir` – rakib Jun 09 '23 at 13:57
  • 1
    Bazel deliberately doesn't provide ways to generate absolute paths. If you do it some other way (like `$$(readlink -f $(location :app_binary))` via shell expansion in a genrule), then caching doesn't work very well because the absolute path varies every time the action is executed. Also those absolute paths differ between actions, so the absolute path isn't really useful. Can you provide more details about why you want an absolute path here? – Brian Silverman Jun 12 '23 at 20:34

0 Answers0