Skip to content

Commit ae1ffb7

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 #13
1 parent 0b03b0d commit ae1ffb7

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

build_env.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,19 @@ 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:
175177
install_site_file(site_packages_path, file)
178+
if add_pth:
179+
pths.add(file.env_path.parts[0])
180+
if add_pth:
181+
pth.write_text("\n".join(pths), encoding="utf-8")
176182

177183

178184
# A copy of importlib_metadata:entry_points that takes a list of search paths.
@@ -266,7 +272,7 @@ def main():
266272
builder = venv.EnvBuilder(clear=True, symlinks=True, with_pip=True)
267273
builder.create(str(env_path))
268274

269-
install_files(env_path, files)
275+
install_files(env_path, files, build_env_input.get("add_pth", False))
270276
generate_console_scripts(env_path)
271277

272278
extra_commands = build_env_input.get("commands")

venv.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def _py_venv_deps_impl(ctx):
2424
for dep in ctx.attr.deps:
2525
if PyInfo not in dep:
2626
continue
27+
print(dep[PyInfo].imports.to_list())
2728
imports.extend([i for i in dep[PyInfo].imports.to_list() if i not in imports])
2829

2930
deps = depset(transitive = [dep[DefaultInfo].default_runfiles.files for dep in ctx.attr.deps])
@@ -44,6 +45,7 @@ def _py_venv_deps_impl(ctx):
4445
"workspace": ctx.workspace_name,
4546
"imports": imports,
4647
"files": files,
48+
"add_pth": ctx.attr.add_pth,
4749
"commands": ctx.attr.commands,
4850
"always_link": ctx.attr.always_link,
4951
}
@@ -58,12 +60,13 @@ _py_venv_deps = rule(
5860
"data": attr.label_list(),
5961
"commands": attr.string_list(),
6062
"always_link": attr.bool(),
63+
"add_pth": attr.bool(),
6164
"output": attr.output(),
6265
},
6366
toolchains = [PYTHON_TOOLCHAIN_TYPE],
6467
)
6568

66-
def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, **kwargs):
69+
def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_link = False, venv_location = None, add_pth = False, **kwargs):
6770
deps = deps or []
6871
data = data or []
6972
extra_pip_commands = extra_pip_commands or []
@@ -78,6 +81,7 @@ def py_venv(name, deps = None, data = None, extra_pip_commands = None, always_li
7881
commands = extra_pip_commands,
7982
always_link = always_link,
8083
output = out_name,
84+
add_pth = add_pth,
8185
**kwargs,
8286
)
8387

0 commit comments

Comments
 (0)