Skip to content

Commit

Permalink
refactor: py_venv macro improved defaults for usability (#396)
Browse files Browse the repository at this point in the history
After testing real developer workflows in vscode, this way works
correctly and is much more practical.

Fixes #395
<img width="750" alt="Screenshot 2024-10-01 at 2 55 13 PM"
src="https://github.com/user-attachments/assets/20a57379-8828-4a87-a7b9-6ab4feba2684">


---

### Changes are visible to end-users: no

### Test plan

Try a few things in VScode:

1. in the root, run `bazel run app:app_bin.venv` and see VSCode offers
to open the resulting virtual env, labels it well
2. open the app folder in the editor, then run `bazel run app_bin.venv`
which also is detected
3. run `bazel run app_test.venv` and the editor offers to switch
4. open a terminal in vscode and run `python -m pytest` and the tests
run correctly, meaning they should also be debuggable
  • Loading branch information
alexeagle authored Oct 1, 2024
1 parent 7a9e4b2 commit b38c18f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions py/private/py_venv.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,12 @@ A collision can occour when multiple packages providing the same file are instal
)

def py_venv(name, **kwargs):
# By default, VSCode (and likely other tools) expect to find virtualenv's in the root of the project opened in the editor.
# They also provide a nice name to see "which one is open" when discovered this way.
# See https://github.com/aspect-build/rules_py/issues/395
default_venv_name = ".{}".format(paths.join(native.package_name(), name).replace("/", "+"))
_py_venv(
name = name,
location = kwargs.pop("location", native.package_name()),
venv_name = kwargs.pop("venv_name", ".{}".format(name)),
venv_name = kwargs.pop("venv_name", default_venv_name),
**kwargs
)
4 changes: 3 additions & 1 deletion py/private/venv.tmpl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function alocation {
}

VENV_TOOL="$(rlocation {{VENV_TOOL}})"
VENV_ROOT="${BUILD_WORKSPACE_DIRECTORY}"
VENV_ROOT="${BUILD_WORKING_DIRECTORY}"
VIRTUAL_ENV="$(alocation "${VENV_ROOT}/{{ARG_VENV_LOCATION}}")"

"${VENV_TOOL}" \
Expand All @@ -31,3 +31,5 @@ VIRTUAL_ENV="$(alocation "${VENV_ROOT}/{{ARG_VENV_LOCATION}}")"
--pth-file "$(rlocation {{ARG_PTH_FILE}})" \
--pth-entry-prefix "${RUNFILES_DIR}" \
--collision-strategy "{{ARG_COLLISION_STRATEGY}}"

echo "Created virtualenv in ${VIRTUAL_ENV}"

0 comments on commit b38c18f

Please sign in to comment.