Skip to content

Commit 43efa46

Browse files
committed
pth: Add a add_pth option to py_pyenv.
Currently, rules_pyvenv installs all the python deps by symlinking them in site-packages. However, in bazel, the python deps are actually added to the PYTHONPATH, which means that for some deps, just symlinking into site-packages doesn't work. However, venv supports this via pth files. pth files will append the directories specified in the pth file to the PYTHONPATH, which exhibits the behavior we want. Fixes cedarai#13
1 parent 0b03b0d commit 43efa46

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

build_env.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,20 @@ def install_site_file(site_packages_path: pathlib.Path, file: EnvFile) -> None:
166166
site_path.symlink_to(file.path.resolve())
167167

168168

169-
def install_files(env_path: pathlib.Path, files: List[EnvFile]) -> None:
169+
def install_files(env_path: pathlib.Path, files: List[EnvFile], add_pth: bool) -> None:
170170
site_packages_path = find_site_packages(env_path)
171+
pth = site_packages_path / "venv.pth"
172+
pths = set()
171173
for file in files:
172174
if is_data_file(file):
173175
install_data_file(env_path, file)
174176
else:
175-
install_site_file(site_packages_path, file)
177+
if add_pth:
178+
pths.add(file.env_path.parts[0])
179+
else:
180+
install_site_file(site_packages_path, file)
181+
if add_pth:
182+
pth.write_text("\n".join(pths), encoding="utf-8")
176183

177184

178185
# A copy of importlib_metadata:entry_points that takes a list of search paths.
@@ -266,7 +273,7 @@ def main():
266273
builder = venv.EnvBuilder(clear=True, symlinks=True, with_pip=True)
267274
builder.create(str(env_path))
268275

269-
install_files(env_path, files)
276+
install_files(env_path, files, build_env_input.get("add_pth", False))
270277
generate_console_scripts(env_path)
271278

272279
extra_commands = build_env_input.get("commands")

venv.bzl

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _py_venv_deps_impl(ctx):
4444
"workspace": ctx.workspace_name,
4545
"imports": imports,
4646
"files": files,
47+
"add_pth": ctx.attr.add_pth,
4748
"commands": ctx.attr.commands,
4849
"always_link": ctx.attr.always_link,
4950
}
@@ -58,12 +59,13 @@ _py_venv_deps = rule(
5859
"data": attr.label_list(),
5960
"commands": attr.string_list(),
6061
"always_link": attr.bool(),
62+
"add_pth": attr.bool(),
6163
"output": attr.output(),
6264
},
6365
toolchains = [PYTHON_TOOLCHAIN_TYPE],
6466
)
6567

66-
def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, **kwargs):
68+
def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, add_pth = False, **kwargs):
6769
deps = deps or []
6870
data = data or []
6971
extra_pip_commands = extra_pip_commands or []
@@ -78,6 +80,7 @@ def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_li
7880
commands = extra_pip_commands,
7981
always_link = always_link,
8082
output = out_name,
83+
add_pth = add_pth,
8184
**kwargs,
8285
)
8386

0 commit comments

Comments
 (0)