Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Bzlmod Migration Help - Accessing .bzl Files From Other Repos

37 views
Skip to first unread message

Charlie Johnston

unread,
Dec 13, 2024, 12:42:29 PM12/13/24
to bazel-discuss
I have a framework used within an organization that has a base set of rules that exist in a GIT repository (call it 'rules') that are then used by other GIT repos (call these 'apps' for simplicity) to share the same set of rules and functionality.

One of the features of the 'rules' repo today is that it expects some generated and/or manually written .bzl files in the 'apps' workspaces. The helper macros within 'rules' depend on these .bzl files for shared workspace information, including some injected "functions" for custom plugins.

In the legacy WORKSPACE world, I have this working where the 'rules' repo can load these files with load("@//:workspace_config.bzl", "CONFIG") and the '@' with no name has effectively mapped to the 'current bazel workspace'.

When moving to bzlmod I have found that the 'rules' repo can no longer access the current Bazel workspace's workspace_config.bzl file because '@' becomes empty and becomes unknown to the 'rules' module.

I have a feeling this is breaking the module system purely from dependency semantics in that it's now somewhat circular in that 'apps' depends on 'rules' but 'rules' depends on somethings from 'apps'. However I was wondering if there was any known ways around this so that 'rules' can access a .bzl file in something that depends on it. I can look to rewrite how the rules require these config files (i.e. options) but if there is something easier, would prefer that.

Thank you!

-Charlie


Allan Clark

unread,
Jan 10, 2025, 4:59:07 AMJan 10
to bazel-discuss
Hi Charlie;

You might need to:
1. create an extension.bzl that defines a repository_rule() that includes attrs={"macros":  attr.label(allow_single_file=True, ...)}
2. the implementation of that repository_rule:
   a. reads the contents (can we?) of the repository_ctx.attr.macro value,
   b. creates a repository_ctx/file for the BUILD
   c. uses a template expansion to use the macros' values to create a macros.bzl (say that defines SHIP_CONFIG as a JSON object)
3. use this extension in your MODULE.bazel
4. use_repo(instance_of_extension, name_of_repo)
5.  your `apps` repo's BUILD.bazel can now load(@name_of_repo//macros.bzl, config="SHIP_CONFIG"), then use "config" as a local variable

something like this might work if it makes sense; otherwise, create a basic repo for the "rules" and "apps", and include a BUILD in the apps that you want to use something that "rules" creates base on apps' config.
Reply all
Reply to author
Forward
0 new messages