Skip to content

Commit

Permalink
Add VIRTUAL_ENV to env var when running in a venv (#16425)
Browse files Browse the repository at this point in the history
Activating a virtual environment sets a VIRTUAL_ENV environment
variable. Replicate this behaviour in `ActivateVirtualEnv` function.

https://docs.python.org/3/library/venv.html#how-venvs-work
> When a virtual environment has been activated, the VIRTUAL_ENV
environment variable is set to the path of the environment
  • Loading branch information
julienp committed Jun 20, 2024
1 parent 2f3f9f2 commit a1880cb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/python
description: Add VIRTUAL_ENV environment variable when running inside a virtual environment
8 changes: 6 additions & 2 deletions sdk/python/toolchain/pip.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,8 @@ func NewVirtualEnvError(dir, fullPath string) error {

// ActivateVirtualEnv takes an array of environment variables (same format as os.Environ()) and path to
// a virtual environment directory, and returns a new "activated" array with the virtual environment's
// "bin" dir ("Scripts" on Windows) prepended to the `PATH` environment variable and `PYTHONHOME` variable
// removed.
// "bin" dir ("Scripts" on Windows) prepended to the `PATH` environment variable, the `VIRTUAL_ENV`
// variable set to the path, and the `PYTHONHOME` variable removed.
func ActivateVirtualEnv(environ []string, virtualEnvDir string) []string {
virtualEnvBin := filepath.Join(virtualEnvDir, virtualEnvBinDirName())
var hasPath bool
Expand All @@ -351,6 +351,8 @@ func ActivateVirtualEnv(environ []string, virtualEnvDir string) []string {
result = append(result, path)
} else if strings.EqualFold(key, "PYTHONHOME") {
// Skip PYTHONHOME to "unset" this value.
} else if strings.EqualFold(key, "VIRTUAL_ENV") {
// Skip VIRTUAL_ENV, we always set this to `virtualEnvDir`
} else {
result = append(result, env)
}
Expand All @@ -359,6 +361,8 @@ func ActivateVirtualEnv(environ []string, virtualEnvDir string) []string {
path := "PATH=" + virtualEnvBin
result = append(result, path)
}
virtualEnv := "VIRTUAL_ENV=" + virtualEnvDir
result = append(result, virtualEnv)
return result
}

Expand Down
31 changes: 21 additions & 10 deletions sdk/python/toolchain/pip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,38 @@ func TestIsVirtualEnv(t *testing.T) {
func TestActivateVirtualEnv(t *testing.T) {
t.Parallel()

venvName := "venv"
venvDir := filepath.Join(venvName, "bin")
venvDir := "/some/path/venv"
venvBinDir := filepath.Join(venvDir, "bin")
if runtime.GOOS == windows {
venvDir = filepath.Join(venvName, "Scripts")
venvBinDir = filepath.Join(venvDir, "Scripts")
}

tests := []struct {
input []string
expected []string
}{
{
input: []string{"PYTHONHOME=foo", "PATH=bar", "FOO=blah"},
expected: []string{fmt.Sprintf("PATH=%s%sbar", venvDir, string(os.PathListSeparator)), "FOO=blah"},
input: []string{"PYTHONHOME=foo", "PATH=bar", "FOO=blah"},
expected: []string{
fmt.Sprintf("PATH=%s%sbar", venvBinDir, string(os.PathListSeparator)),
"FOO=blah",
"VIRTUAL_ENV=" + venvDir,
},
},
{
input: []string{"PYTHONHOME=foo", "FOO=blah"},
expected: []string{"FOO=blah", "PATH=" + venvDir},
input: []string{"PYTHONHOME=foo", "FOO=blah"},
expected: []string{
"FOO=blah",
"PATH=" + venvBinDir,
"VIRTUAL_ENV=" + venvDir,
},
},
{
input: []string{"PythonHome=foo", "Path=bar"},
expected: []string{fmt.Sprintf("Path=%s%sbar", venvDir, string(os.PathListSeparator))},
input: []string{"PythonHome=foo", "Path=bar"},
expected: []string{
fmt.Sprintf("Path=%s%sbar", venvBinDir, string(os.PathListSeparator)),
"VIRTUAL_ENV=" + venvDir,
},
},
}
//nolint:paralleltest // false positive because range var isn't used directly in t.Run(name) arg
Expand All @@ -79,7 +90,7 @@ func TestActivateVirtualEnv(t *testing.T) {
t.Run(fmt.Sprintf("%#v", test.input), func(t *testing.T) {
t.Parallel()

actual := ActivateVirtualEnv(test.input, venvName)
actual := ActivateVirtualEnv(test.input, venvDir)
assert.Equal(t, test.expected, actual)
})
}
Expand Down

0 comments on commit a1880cb

Please sign in to comment.