Skip to content

Give access to a unique module identifier from module extensions and macros #17652

Closed
@aherrmann

Description

@aherrmann

Description of the feature request:

Please add the capability to obtain a unique identifier for a module in the following contexts:

  • For a module extension to obtain the unique module identifier of a module that uses the module extension. For example, this could take the form of an attribute on the bazel_module objects exposed to a module extension through module_ctx.modules.
  • For a macro to obtain the same unique module identifier of the module out of which the macro is invoked. For example, this could take the form of a function in the native module similar to package_relative_label.
    This should take into account external workspaces generated by a module. For example, if module A defines a module extension that generates an external workspace, then macros invoked out of that external workspace should resolve the calling module identifier to that of module A.

The unique module identifier should be distinct between different versions of the same module introduced by a multi version override.

What underlying problem are you trying to solve with this feature?

I am trying to implement registry in a "hub and spoke model" or "hub repo" for named tags of a module extension, such that a module that requested a tag with a given name, can later gain access to the corresponding external workspace under the provided tag-name.

For example

# @mymod//:MODULE.bazel
module(name = "mymod", version = "1.2.3")

nix_pkg = use_extension("@rules_nixpkgs_core//extensions:packages.bzl", "nix_pkg")
nix_pkg.attr(name = "mytool", ...)
use_repo(nix_pkg, "nixpkgs_packages")

# @mymod//:BUILD.bazel
load("@nixpkgs_packages//:defs.bzl", "get_pkg")

genrule(
   ...
   tools = [get_pkg("mytool")],
)

# @nixpkgs_packages//:defs.bzl

def get_pkg(tag_name):
    module_id = native.calling_module_id()
    return _registry[module_id][tag_name]
    # omitted error handling for clarity

Which operating system are you running Bazel on?

Ubuntu 22.04

What is the output of bazel info release?

release 6.0.0

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

The aforementioned issues #17493 #17048 describing hub-repos.
Not found, but started this related thread on Bazel Slack.

Any other information, logs, or outputs that you want to share?

As suggested by @fmeum on the related thread on Bazel Slack:

Ideally the ID would be human writable and concise in the sense that it only includes the module version if needed for uniqueness (e.g. my_module in the single version and [email protected] in the multi-version case). That way it would look good in error messages and could also be specified by users when they need to refer to a particular module (e.g. to specify an override-like tag for it).

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1I'll work on this now. (Assignee required)area-BzlmodBzlmod-specific PRs, issues, and feature requeststeam-ExternalDepsExternal dependency handling, remote repositiories, WORKSPACE file.type: feature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions