Skip to content

Commit 5968bcb

Browse files
authored
fix(venv_link): Repair venv link munging (#599)
Fixes the #593 report. Reworks the venv linking script so that we can add tests covering munging and prevent future regressions. --- ### Changes are visible to end-users: yes - Searched for relevant documentation and updated as needed: yes/no - Breaking change (forces users to change their own code or config): yes/no - Suggested release notes appear below: yes Fixed a regression in the naming of virtualenv links. ### Test plan - New test cases added
1 parent 5f6c518 commit 5968bcb

File tree

3 files changed

+52
-25
lines changed

3 files changed

+52
-25
lines changed

py/private/py_venv/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2+
load(":defs.bzl", "py_venv_test")
23

34
package(default_visibility = [
45
"//docs:__pkg__",
@@ -33,3 +34,13 @@ bzl_library(
3334
"@aspect_bazel_lib//lib:paths",
3435
],
3536
)
37+
38+
py_venv_test(
39+
name = "test_link",
40+
srcs = [
41+
"link.py",
42+
"test_link.py",
43+
],
44+
imports = ["."],
45+
main = "test_link.py",
46+
)

py/private/py_venv/link.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,43 @@
1111
import site
1212
from pathlib import Path
1313

14-
virtualenv_home = os.path.realpath(os.environ["VIRTUAL_ENV"])
15-
virtualenv_name = os.path.basename(virtualenv_home)
16-
runfiles_dir = os.path.realpath(os.environ["RUNFILES_DIR"])
17-
builddir = os.path.realpath(os.environ["BUILD_WORKING_DIRECTORY"])
18-
target_package, target_name = os.environ["BAZEL_TARGET"].split("//", 1)[1].split(":")
19-
20-
PARSER = argparse.ArgumentParser(
21-
prog="link",
22-
usage=__doc__,
23-
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
24-
)
25-
26-
PARSER.add_argument(
27-
"--dest",
28-
dest="dest",
29-
default=builddir,
30-
help="Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY.",
31-
)
32-
33-
PARSER.add_argument(
34-
"--name",
35-
dest="name",
36-
default=".{}+{}".format(target_package.replace("/", "+"), virtualenv_name.lstrip(".")),
37-
help="Name to link the virtualenv as.",
38-
)
3914

15+
def munge_venv_name(target_package, virtualenv_name):
16+
acc = (target_package or "").replace("/", "+")
17+
if acc:
18+
acc += "+"
19+
acc += virtualenv_name.lstrip(".")
20+
return "." + acc
21+
4022

4123
if __name__ == "__main__":
24+
virtualenv_home = os.path.realpath(os.environ["VIRTUAL_ENV"])
25+
virtualenv_name = os.path.basename(virtualenv_home)
26+
runfiles_dir = os.path.realpath(os.environ["RUNFILES_DIR"])
27+
builddir = os.path.realpath(os.environ["BUILD_WORKING_DIRECTORY"])
28+
target_package, target_name = os.environ["BAZEL_TARGET"].split("//", 1)[1].split(":")
29+
30+
PARSER = argparse.ArgumentParser(
31+
prog="link",
32+
usage=__doc__,
33+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
34+
)
35+
36+
PARSER.add_argument(
37+
"--dest",
38+
dest="dest",
39+
default=builddir,
40+
help="Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY.",
41+
)
42+
43+
PARSER.add_argument(
44+
"--name",
45+
dest="name",
46+
default=munge_venv_name(target_package, virtualenv_name),
47+
help="Name to link the virtualenv as.",
48+
)
49+
50+
4251
PARSER.print_help(sys.stdout)
4352
opts = PARSER.parse_args()
4453
dest = Path(os.path.join(opts.dest, opts.name))

py/private/py_venv/test_link.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/usr/bin/env python3
2+
3+
from link import munge_venv_name
4+
5+
assert munge_venv_name("", ".foo_venv") == ".foo_venv"
6+
assert munge_venv_name("bar", ".foo_venv") == ".bar+foo_venv"
7+
assert munge_venv_name("bar/baz", ".foo_venv") == ".bar+baz+foo_venv"

0 commit comments

Comments
 (0)