fix(pypi): inherit proxy env variables in compile_pip_requirements test (#2941)
Bazel does not pass environment variables implicitly (even running test
outside of sandbox). This forces compile_pip_requirements test to fail
with timeout when attempting to run it behind the proxy. Also changes
test_command in dependency_resolver string helper to use dot instead of
underscore following deprecation notice
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c9668c5..e48e3d4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -98,6 +98,7 @@
* (py_test, py_binary) Allow external files to be used for main
* (pypi) Correctly aggregate the sources when the hashes specified in the lockfile differ
by platform even though the same version is used. Fixes [#2648](https://github.com/bazel-contrib/rules_python/issues/2648).
+* (pypi) `compile_pip_requirements` test rule works behind the proxy
{#v0-0-0-added}
### Added
diff --git a/python/private/pypi/dependency_resolver/dependency_resolver.py b/python/private/pypi/dependency_resolver/dependency_resolver.py
index a42821c..f3a339f 100644
--- a/python/private/pypi/dependency_resolver/dependency_resolver.py
+++ b/python/private/pypi/dependency_resolver/dependency_resolver.py
@@ -165,7 +165,7 @@
update_command = (
os.getenv("CUSTOM_COMPILE_COMMAND") or f"bazel run {target_label_prefix}.update"
)
- test_command = f"bazel test {target_label_prefix}_test"
+ test_command = f"bazel test {target_label_prefix}.test"
os.environ["CUSTOM_COMPILE_COMMAND"] = update_command
os.environ["PIP_CONFIG_FILE"] = os.getenv("PIP_CONFIG_FILE") or os.devnull
diff --git a/python/private/pypi/pip_compile.bzl b/python/private/pypi/pip_compile.bzl
index c989950..78b681b 100644
--- a/python/private/pypi/pip_compile.bzl
+++ b/python/private/pypi/pip_compile.bzl
@@ -45,7 +45,6 @@
By default this rules generates a filegroup named "[name]" which can be included in the data
of some other compile_pip_requirements rule that references these requirements
(e.g. with `-r ../other/requirements.txt`).
-
It also generates two targets for running pip-compile:
- validate with `bazel test [name].test`
@@ -160,6 +159,12 @@
}
env = kwargs.pop("env", {})
+ env_inherit = kwargs.pop("env_inherit", [])
+ proxy_variables = ["https_proxy", "http_proxy", "no_proxy", "HTTPS_PROXY", "HTTP_PROXY", "NO_PROXY"]
+
+ for var in proxy_variables:
+ if var not in env_inherit:
+ env_inherit.append(var)
py_binary(
name = name + ".update",
@@ -182,6 +187,7 @@
"@@platforms//os:windows": {"USERPROFILE": "Z:\\FakeSetuptoolsHomeDirectoryHack"},
"//conditions:default": {},
}) | env,
+ env_inherit = env_inherit,
# kwargs could contain test-specific attributes like size
**dict(attrs, **kwargs)
)