Skip to content

Commit c27399b

Browse files
committed
tweak(venv): Use the interpreter to abspath
1 parent 5ea06cb commit c27399b

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

py/tools/py/src/activate.tmpl

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export PYTHONEXECUTABLE
7070
# unset PYTHONHOME if set
7171
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
7272
# could use `if (set -u; : $PYTHONHOME) ;` in bash.
73-
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
73+
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-_activate_undef}"
7474
unset PYTHONHOME
7575

7676
_OLD_VIRTUAL_PATH="$PATH"
@@ -85,6 +85,24 @@ _OLD_VIRTUAL_PATH="$PATH"
8585
# can provide some fallback handling around runfiles too.
8686
{{RUNFILES_INTERPRETER}}
8787

88+
_abspath() {
89+
"${PYTHONEXECUTABLE}" -c 'import os, sys; print(os.path.abspath(sys.argv[1]))' "$@"
90+
}
91+
92+
# Re-export abspath'd vars
93+
# This allows us to avoid relative path issues without incurring sandbox escapes
94+
VIRTUAL_ENV="$(_abspath "${VIRTUAL_ENV}")"
95+
export VIRTUAL_ENV
96+
97+
PYTHONEXECUTABLE="$(_abspath "${PYTHONEXECUTABLE}")"
98+
export PYTHONEXECUTABLE
99+
100+
if [ -n "${PYTHONHOME:-}" ]; then
101+
PYTHONHOME="$(_abspath "${PYTHONHOME}")"
102+
export PYTHONHOME
103+
fi
104+
105+
# Now we can put the venv's absolute bin on the path
88106
PATH="$VIRTUAL_ENV/bin:$PATH"
89107
export PATH
90108

py/tools/py/src/runfiles_interpreter.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# --- Runfiles-based interpreter setup --
1+
# --- Runfiles-based interpreter setup ---
22

33
# If the runfiles dir is unset AND we will fail to find a runfiles manifest
44
# based on inspecting $0, we need to try something different.

0 commit comments

Comments
 (0)