bazelbuild / bazelbuild/bazel

Support root_module_optional_deps in module_ctx.extension_metadata

Open
#25,046 5 comments 0 reactions 0 assignees View on GitHub
not stale P3 team-ExternalDeps type: feature request
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 16h
Merged PRs (30d)
72

Description

### Description of the feature request:

As of today, module extensions can report what *must* be put into `use_repo` calls using `root_module_direct_deps ` and `root_module_direct_dev_deps` with the help of `module_ctx.is_dev_dependency`, this is good in cases where all the repositories created by the module extension most likely will be used somewhere in the repo.

However, repositories created by the module extension might be not be used at all and can be confusing to people and sometimes leads to spurious warning messages if omitted.

For instance, the rules_oci oci_pull extension creates several repositories and reports them with `root_module_direct_deps`. Given the example

```
oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_base",
platforms = ["linux/amd64", "linux/arm64/v8"]
)

use_repo(oci, "distroless_base")
```

the extension reports the following repos, `distroless_base`, `distroless_base_linux_amd64`, `distroless_linux_arm64_v8`, here `distroless_base` and the other repos are interchangeable, while some users need the platform specific, other don't.

Today, there is not much the extension do because it doesn't have a mechanism to determine if they were requested in the first place, so the extension has to assume they are going to be needed, and reports them with `root_module_direct_deps`.

Feature request is to have another field in `extension_metadata` that extensions can report optional deps.

```
module_ctx.extension_metadata(
root_module_direct_deps = ["distroless_base"],
root_module_optional_deps = ["distroless_base_linux_amd64", "distroless_linux_arm64_v8"]
)
```
Given module extension above, when users run `bazel mod tidy`, Bazel will create a `use_repo` call with `distroless_base` entry and add the other two repositories if we are references anywhere in the dependency graph.

### Which category does this issue belong to?

External Dependency

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

Reporting dependencies from module extension that are needed optionally.

See: https://github.com/bazelbuild/bazel/issues/24950
See: https://github.com/bazel-contrib/rules_oci/issues/727
See: https://github.com/bazel-contrib/rules_oci/pull/611
See: https://github.com/bazel-contrib/rules_oci/pull/611/files#r1646582755 cc @fmeum

### Which operating system are you running Bazel on?

darwin-arm64

### What is the output of `bazel info release`?

_No response_

### 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 HEAD` ?

```text

```

### Have you found anything relevant by searching the web?

_No response_

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

_No response_

Contributor guide

Open the contributing guide

Research direction

Start with the module_ctx.extension_metadata API and the bazel mod tidy behavior described in the issue. Read the linked issues and rules_oci references for existing design context. Done means optional repositories are reported separately and bazel mod tidy adds them only when referenced in the dependency graph.

Written by the indexing model from the issue text.

Assessment

Tech stack
build-system
Domain
build-system
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.