Skip to content

Commit 56057e9

Browse files
committed
Better support for local libraries
If a py_library is included in a venv and uses the imports attr, it will be linked into the site-packages directory with the proper path. Additionally, an always_link attr is added that, when True, will allways link py_library targets into site-packages even if they don't specify imports.
1 parent 115e8a4 commit 56057e9

File tree

7 files changed

+46
-6
lines changed

7 files changed

+46
-6
lines changed

build_env.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def find_site_packages(env_path: pathlib.Path) -> pathlib.Path:
9898
def get_files(build_env_input: Dict) -> List[EnvFile]:
9999
files = []
100100

101+
always_link = build_env_input.get("always_link", False)
101102
imports = [pathlib.Path(imp) for imp in build_env_input["imports"]]
102103
workspace = build_env_input["workspace"]
103104
for depfile in build_env_input["files"]:
@@ -106,10 +107,6 @@ def get_files(build_env_input: Dict) -> List[EnvFile]:
106107
type_ = depfile["t"]
107108
input_path = pathlib.Path(depfile["p"])
108109

109-
# Only add external and generated files
110-
if not (is_external(input_path) or type_ == "G"):
111-
continue
112-
113110
# If this is a directory, expand to each recursive child.
114111
if input_path.is_dir():
115112
paths = input_path.glob("**/*")
@@ -119,7 +116,8 @@ def get_files(build_env_input: Dict) -> List[EnvFile]:
119116

120117
for path in paths:
121118
site_packages_path = get_site_packages_path(workspace, path, imports)
122-
files.append(EnvFile(path, site_packages_path))
119+
if site_packages_path != path or always_link:
120+
files.append(EnvFile(path, site_packages_path))
123121

124122
return files
125123

example/BUILD.bazel

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ load("@rules_python//python/pip_install:requirements.bzl", "compile_pip_requirem
2121
py_venv(
2222
name = "venv",
2323
deps = [
24+
"//libraries/liba",
2425
requirement("black"),
2526
requirement("numpy"),
2627
],
@@ -29,6 +30,23 @@ py_venv(
2930
]
3031
)
3132

33+
py_venv(
34+
name = "venv_only_local",
35+
deps = [
36+
"//libraries/liba",
37+
"//libraries/libb",
38+
],
39+
)
40+
41+
py_venv(
42+
name = "venv_only_local_always_link",
43+
deps = [
44+
"//libraries/liba",
45+
"//libraries/libb",
46+
],
47+
always_link = True,
48+
)
49+
3250
compile_pip_requirements(
3351
name = "requirements",
3452
extra_args = ["--allow-unsafe"],

example/libraries/liba/BUILD.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
py_library(
4+
name = "liba",
5+
srcs = ["liba.py"],
6+
imports = ["."],
7+
)
8+

example/libraries/liba/liba.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo():
2+
pass
3+

example/libraries/libb/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
py_library(
4+
name = "libb",
5+
srcs = ["libb.py"],
6+
)
7+

example/libraries/libb/libb.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def foo():
2+
pass
3+

venv.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _py_venv_deps_impl(ctx):
4444
"imports": imports,
4545
"files": files,
4646
"commands": ctx.attr.commands,
47+
"always_link": ctx.attr.always_link,
4748
}
4849
ctx.actions.write(out, json.encode(doc))
4950

@@ -54,12 +55,13 @@ _py_venv_deps = rule(
5455
attrs = {
5556
"deps": attr.label_list(),
5657
"commands": attr.string_list(),
58+
"always_link": attr.bool(),
5759
"output": attr.output(),
5860
},
5961
toolchains = [PYTHON_TOOLCHAIN_TYPE],
6062
)
6163

62-
def py_venv(name, deps = None, extra_pip_commands = None, **kwargs):
64+
def py_venv(name, deps = None, extra_pip_commands = None, always_link = False, **kwargs):
6365
deps = deps or []
6466
extra_pip_commands = extra_pip_commands or []
6567

@@ -70,6 +72,7 @@ def py_venv(name, deps = None, extra_pip_commands = None, **kwargs):
7072
name = deps_name,
7173
deps = deps,
7274
commands = extra_pip_commands,
75+
always_link = always_link,
7376
output = out_name,
7477
**kwargs,
7578
)

0 commit comments

Comments
 (0)