Skip to content

Commit 72dbc41

Browse files
committed
Add a feature for flipping the isolation bit
1 parent e08842b commit 72dbc41

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

py/private/py_venv/py_venv.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def _py_venv_base_impl(ctx):
133133
py_toolchain.interpreter_version_info.major,
134134
py_toolchain.interpreter_version_info.minor,
135135
),
136+
"--include-system-site-packages=" + ({
137+
True: "true",
138+
False: "false"
139+
}[ctx.attr.include_system_site_packages]),
136140
] + (["--debug"] if ctx.attr.debug else []),
137141
inputs = rfs.merge_all([
138142
ctx.runfiles(files = [
@@ -294,6 +298,24 @@ A collision can occur when multiple packages providing the same file are install
294298
"debug": attr.bool(
295299
default = False,
296300
),
301+
"include_system_site_packages": attr.bool(
302+
default = True,
303+
doc = """`pyvenv.cfg` feature flag for the `include-system-site-packages` key.
304+
305+
When `True`, the user's site directory AND the interpreter's site directory will
306+
be included into the runtime pythonpath.
307+
308+
When `False`, only the virtualenv's site directory and the interpreter's core
309+
libraries will be included into the runtime pythonpath.
310+
311+
`False` is obviously preferable as it increases hermeticity, but the choice of
312+
`False` cases for instance a `pip` or `setuptools` bundled into the interpreter
313+
to be unusable. Many libraries assume these packages will always be available
314+
and may not reliably declare their dependencies such that Bazel will satisfy
315+
them, so choosing isolation could expose packaging errors.
316+
317+
"""
318+
),
297319
})
298320

299321
_attrs.update(**_py_library.attrs)

py/tools/py/src/pyvenv.cfg.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
home = ./bin/python3
22
implementation = CPython
33
version_info = {{MAJOR}}.{{MINOR}}.{{PATCH}}
4-
include-system-site-packages = true
4+
include-system-site-packages = {{INCLUDE_SYSTEM_SITE}}
55
relocatable = true
66
{{INTERPRETER}}

py/tools/py/src/venv.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ pub fn create_empty_venv<'a>(
210210
env_file: &Option<PathBuf>,
211211
venv_shim: &Option<PathBuf>,
212212
debug: bool,
213+
include_system_site_packages: bool,
213214
) -> miette::Result<Virtualenv> {
214215
let build_dir = current_dir().into_diagnostic()?;
215216
let home_dir = &build_dir.join(location.to_path_buf());
@@ -265,7 +266,11 @@ aspect_runfiles_repo = {1}
265266
.replace("{{MAJOR}}", &venv.version_info.major.to_string())
266267
.replace("{{MINOR}}", &venv.version_info.minor.to_string())
267268
.replace("{{PATCH}}", &venv.version_info.patch.to_string())
268-
.replace("{{INTERPRETER}}", interpreter_cfg_snippet),
269+
.replace("{{INTERPRETER}}", interpreter_cfg_snippet)
270+
.replace(
271+
"{{INCLUDE_SYSTEM_SITE}}",
272+
&include_system_site_packages.to_string(),
273+
),
269274
)
270275
.into_diagnostic()?;
271276

py/tools/venv_bin/src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::path::PathBuf;
22

3+
use clap::ArgAction;
34
use clap::Parser;
45
use miette::miette;
56
use miette::Context;
@@ -90,6 +91,16 @@ struct VenvArgs {
9091

9192
#[arg(long, default_value_t = false)]
9293
debug: bool,
94+
95+
#[clap(
96+
long,
97+
default_missing_value("true"),
98+
default_value("true"),
99+
num_args(0..=1),
100+
require_equals(true),
101+
action = ArgAction::Set,
102+
)]
103+
include_system_site_packages: bool,
93104
}
94105

95106
fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
@@ -119,6 +130,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
119130
&args.env_file,
120131
&args.venv_shim,
121132
args.debug,
133+
args.include_system_site_packages,
122134
)?;
123135

124136
py::venv::populate_venv_with_copies(
@@ -147,6 +159,7 @@ fn venv_cmd_handler(args: VenvArgs) -> miette::Result<()> {
147159
&args.env_file,
148160
&args.venv_shim,
149161
args.debug,
162+
args.include_system_site_packages,
150163
)?;
151164

152165
py::venv::populate_venv_with_pth(

0 commit comments

Comments
 (0)