bazel-contrib / bazel-contrib/rules_python

Support importing across repos with bzlmod

未关闭
#2,088 16 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看
type: bzlmod type: feature request
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

# 🚀 feature request

### Relevant Rules
`py_library` / `py_binary`

### Description

If there is a python library `@foo//bar:baz`, how can I import it from the main repository?

Pre-bzlmod, you could write `from foo.bar import baz`, as it would be in the directory `external/foo/bar/baz`, and the external directory was in your python path. Apparently this was not documented or intended, but nevertheless, people used it, as I don't believe it was ever considered when writing rules_python.

In bzlmod, however, you have repo mapping, where the directory `foo` is actually stored at `external/_main~~foo_ext~foo`, and thus `from foo.bar import baz` fails. This is the direct cause of #1679, and if we solve this, we fix that bug for free.

### Describe the solution you'd like
I'd personally like to make users prefix their imports to external repositories with the name of the workspace (based on the repo mapping). This would allow two python files in different repositories to `import foo.bar.baz` and each get their own `@foo` in the case of a repo name clash (this is how repo mapping is intended to be used, to allow each repo to have their own namespace of repos).

I previously achieved this in ChromeOS via a `sitecustomize.py` (see discussion in #1679).

### Describe alternatives you've considered

I can't really think of a viable alternative. Any solution without including the repo name in the import seems like it will end up in painful corner cases where you have name conflicts.

贡献指南

打开贡献指南

调研方向

首先阅读 #1679 中的讨论以及其中引用的 sitecustomize.py 方案。明确 bzlmod repo mapping 应如何确定跨 repository 的 Python 导入名称,包括名称冲突。完成的标准是形成一项经过同意并记录在案的实现方向,该方向支持所请求的跨 repository 导入并解决报告的失败问题。

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

评估

技术栈
python
领域
build-system
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

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