toolchains_llvm icon indicating copy to clipboard operation
toolchains_llvm copied to clipboard

Don't forbid use_extension("llvm.bzl") inside non-root modules

Open EdSchouten opened this issue 2 months ago • 3 comments

Though I understand why the authors of this module don't want to permit module extension tag declarations of llvm.toolchain() in non-root modules, not being able to depend on the extension entirely is fairly restrictive.

In one of my Bazel projects I want to be able to use clang-format as part of some reformatting script. These scripts are not just used by the module itself, but also by some other modules that depend on it. I therefore have the following stanza in MODULE.bazel:

llvm_dev = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm", dev_dependency = True)
llvm_dev.toolchain(
    llvm_version = "17.0.6",
)
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
use_repo(llvm, "llvm_toolchain_llvm")

I can then use label @llvm_toolchain_llvm//:bin/clang-format to get a copy of clang-format. The current logic prevents me from doing this, because the second use_extension() call is rejected outright, even if it doesn't invoke llvm.toolchain().

EdSchouten avatar Oct 08 '25 08:10 EdSchouten

It’s a reformatting script that I keep in bb-storage that’s also capable of reformatting code in any other bb-* repo.

So I’m not able to mark it dev_dependency=True

EdSchouten avatar Oct 08 '25 10:10 EdSchouten

It’s a reformatting script that I keep in bb-storage that’s also capable of reformatting code in any other bb-* repo.

So I’m not able to mark it dev_dependency=True

I assume you are doing something like the following:

"_clang_format_tool": attr.label(
        doc = "The target of the clang-format executable.",
        default = Label("@llvm_toolchain_llvm//:bin/clang-format"),
        allow_single_file = True,
        executable = True,
        cfg = "exec",
    ),

If you are doing something different, mind sharing?

helly25 avatar Oct 08 '25 10:10 helly25

@EdSchouten What do you think of introducing an @rules_go//go style alias for clang-format? A problem with the current approach I only now realized is that such an extension usage would break if the root module doesn't contain a toolchains tag or uses a different name - it would refer to a non-existent repo.

fmeum avatar Oct 08 '25 10:10 fmeum