bazel-contrib / bazel-contrib/rules_python

Avoid downloading protobuf dependancy when not needed

未关闭
#2,543 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
help wanted need: upstream support
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

Hi we recently migrated a project from workspace to bzlmod and noticed our builds now pull in more things from the internet as a result.
In particular we saw that our builds started to download protobuf (from https://github.com/protocolbuffers/protobuf/releases/download/v29.0-rc2/protobuf-29.0-rc2.zip).
This was despite none of our targets referencing (either directly nor indirectly) `py_proto_library` (or any other protobuf target from `rules_python` or any other module).

It seems that in a workspace project dependencies are fetched lazily [1] whereas in a bzlmod project, dependencies are fetched eagerly.

Reading the source of this library, I saw a comment in the MODULE.bzl https://github.com/bazelbuild/rules_python/blob/29fdcc2c292d64e7c66746d1c701ab67aa05c79b/MODULE.bazel#L12 that suggests the `protobuf` dep will be loaded only if the `py_proto_library` is used. However, that doesn't match my tests where `protobuf` is download by all projects that depend on `rules_python` even if they do not use _any_ rules from it [2].

I am wondering

1. if there is a way to use `rules_python` without pulling `protobuf` if we do not need protobuf support?
2. If the comment linked above needs updating/removing?

#### Related

I think https://github.com/bazelbuild/rules_python/issues/2498 is similar although that is about rules_kotlin and is more about toolchains fetched as module extensions rather than modules downloaded directly.

#### Notes

1: https://bazel.build/extending/repo#when_is_the_implementation_function_executed
2: You can test this by creating a custom registry (https://bazel.build/external/registry) and modifying the `source.json` file of all the protobuf modules to set the `url` field to something that does not exist. Building a bzlmod project that uses rules_python but does not have any `py_proto_library` targets will then fail showing that bazel does try to fetch protobuf.

贡献指南

打开贡献指南

调研方向

首先阅读第 12 行附近的 MODULE.bzl,并追踪 bzlmod 下 protobuf 依赖与 py_proto_library 的关系。使用 protobuf 源 URL 无效的自定义 registry,在一个依赖 rules_python 但未定义任何 protobuf target 的项目中复现该行为。完成的标准是:除非使用了相关支持,否则项目不再尝试获取 protobuf,并且注释或文档与该行为一致。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
build-system
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。