Skip to content

Commit 9e70c88

Browse files
committed
fix(venv_link): Restore original venv name mangling (#589)
Reported by @jimmyt857 in testing of v1.6.0-rc0 Previously running the `examples/py_binary:py_binary.venv` target would create a link `$BUILD_WORKING_DIRECTORY/.examples+py_binary+py_binary.venv`. This behavior regressed in 1.5.0 and the link became simply `$BUILD_WORKING_DIRECTORY/.py_binary.venv`. Restore the original behavior. --- ### Changes are visible to end-users: no - Searched for relevant documentation and updated as needed: no - Breaking change (forces users to change their own code or config): no - Suggested release notes appear below: no ### Test plan - [x] Manual testing ```shellsession ❯ bazel run //examples/py_binary:py_binary.venv INFO: Analyzed target //examples/py_binary:py_binary.venv (0 packages loaded, 0 targets configured). INFO: Found 1 target... Target //examples/py_binary:py_binary.venv up-to-date: bazel-bin/examples/py_binary/py_binary.venv INFO: Elapsed time: 0.815s, Critical Path: 0.05s INFO: 2 processes: 1 internal, 1 darwin-sandbox. INFO: Build completed successfully, 2 total actions INFO: Running command line: bazel-bin/examples/py_binary/py_binary.venv usage: link [options] Helper to create a symlink to a virtualenv in the source tree. optional arguments: -h, --help show this help message and exit --dest DEST Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY. (default: /Users/arrdem/Documents/work/aspect/rules_py) --name NAME Name to link the virtualenv as. (default: .examples+py_binary+py_binary.venv) Linking: /private/var/tmp/_bazel_arrdem/93bfea6cdc1153cc29a75400cd38823a/execroot/aspect_rules_py/bazel-out/darwin_arm64-fastbuild/bin/examples/py_binary/.py_binary.venv -> /Users/arrdem/Documents/work/aspect/rules_py/.examples+py_binary+py_binary.venv Link is up to date! To configure the virtualenv in your IDE, configure an interpreter with the homedir /Users/arrdem/Documents/work/aspect/rules_py/.examples+py_binary+py_binary.venv To activate the virtualenv in your shell run source /Users/arrdem/Documents/work/aspect/rules_py/.examples+py_binary+py_binary.venv/bin/activate ```
1 parent 5ea06cb commit 9e70c88

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

py/private/py_venv/link.py

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,47 +15,43 @@
1515
virtualenv_name = os.path.basename(virtualenv_home)
1616
runfiles_dir = os.path.realpath(os.environ["RUNFILES_DIR"])
1717
builddir = os.path.realpath(os.environ["BUILD_WORKING_DIRECTORY"])
18-
19-
# Chop off the runfiles tree prefix
20-
virtualenv_path = virtualenv_home.lstrip(runfiles_dir).lstrip("/")
21-
# Chop off the repo name to get a repo-relative path
22-
virtualenv_path = virtualenv_path[virtualenv_path.find("/"):]
18+
target_package, target_name = os.environ["BAZEL_TARGET"].split("//", 1)[1].split(":")
2319

2420
PARSER = argparse.ArgumentParser(
2521
prog="link",
2622
usage=__doc__,
23+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
2724
)
2825

2926
PARSER.add_argument(
30-
"--venv-name",
31-
dest="venv_name",
32-
default=virtualenv_name,
33-
help="Name to link the virtualenv under.",
27+
"--dest",
28+
dest="dest",
29+
default=builddir,
30+
help="Dir to link the virtualenv into. Default is $BUILD_WORKING_DIRECTORY.",
3431
)
3532

3633
PARSER.add_argument(
37-
"--dest",
38-
dest="dest",
39-
default=os.path.join(builddir, os.path.dirname(virtualenv_path)),
40-
help="Dir to link the virtualenv into",
34+
"--name",
35+
dest="name",
36+
default=".{}+{}".format(target_package.replace("/", "+"), virtualenv_name.lstrip(".")),
37+
help="Name to link the virtualenv as.",
4138
)
4239

40+
4341
if __name__ == "__main__":
42+
PARSER.print_help(sys.stdout)
4443
opts = PARSER.parse_args()
45-
dest = Path(os.path.join(opts.dest, opts.venv_name))
46-
print("""\
47-
Linking: {venv_home} -> {venv_path}
44+
dest = Path(os.path.join(opts.dest, opts.name))
45+
print("""
4846
49-
To activate the virtualenv run:
50-
source {venv_path}/bin/activate
47+
Linking: {venv_home} -> {venv_path}
5148
""".format(
5249
venv_home = virtualenv_home,
5350
venv_path = dest,
5451
))
5552

5653
if dest.exists() and dest.is_symlink() and dest.readlink() == Path(virtualenv_home):
5754
print("Link is up to date!")
58-
exit(0)
5955

6056
else:
6157
try:
@@ -67,4 +63,22 @@
6763
# From -> to
6864
dest.symlink_to(virtualenv_home, target_is_directory=True)
6965
print("Link created!")
70-
exit(0)
66+
67+
print("""
68+
To configure the virtualenv in your IDE, configure an interpreter with the homedir
69+
{venv_path}
70+
71+
Please note that you may encounter issues if your editor doesn't evaluate
72+
the `activate` script. If you do please file an issue at
73+
https://github.com/aspect-build/rules_py/issues/new?template=BUG-REPORT.yaml
74+
75+
To activate the virtualenv in your shell run
76+
source {venv_path}/bin/activate
77+
78+
virtualenvwrapper users may further want to
79+
$ ln -s {venv_path} $WORKON_HOME/{venv_name}
80+
""".format(
81+
venv_home = virtualenv_home,
82+
venv_name = opts.name,
83+
venv_path = dest,
84+
))

0 commit comments

Comments
 (0)