Skip to content

Commit

Permalink
vpython: use realpath when invoking virtualenv
Browse files Browse the repository at this point in the history
Use realpath for virtualenv.pyz to mitigate pypa/virtualenv#1949

Change-Id: I4ddfb78bc435f9791ccae5e87cf5c0da404a110a
Reviewed-on: https://chromium-review.googlesource.com/c/infra/luci/luci-go/+/5449147
Reviewed-by: Brian Ryner <bryner@google.com>
Commit-Queue: Chenlin Fan <fancl@chromium.org>
Auto-Submit: Chenlin Fan <fancl@chromium.org>
  • Loading branch information
fancl20 authored and LUCI CQ committed Apr 16, 2024
1 parent 15f0fa7 commit 988aaae
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion vpython/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (a *Application) ParseEnvs(ctx context.Context) (err error) {
// Determine our VirtualEnv base directory.
if v, ok := e.Lookup(VirtualEnvRootENV); ok {
a.VpythonRoot = v
} else {
} else if a.VpythonRoot == "" {
cdir, err := a.userCacheDir()
if err != nil {
logging.Infof(ctx, "failed to get user cache dir: %s", err)
Expand Down
30 changes: 25 additions & 5 deletions vpython/application/application_py3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -58,11 +59,9 @@ func getPythonEnvironment(ver string) *python.Environment {
}

func setupApp(ctx context.Context, app *Application) context.Context {
app.Arguments = append([]string{
"-vpython-root",
testStorageDir,
}, app.Arguments...)

if app.VpythonRoot == "" {
app.VpythonRoot = testStorageDir
}
app.Initialize(ctx)

So(app.ParseEnvs(ctx), ShouldBeNil)
Expand Down Expand Up @@ -166,6 +165,27 @@ func TestPythonBasic(t *testing.T) {
So(has, ShouldBeTrue)
So(rc, ShouldEqual, 42)
})

if runtime.GOOS != "windows" {
// See https://github.com/pypa/virtualenv/issues/1949
Convey("Test symlink root", func() {
symlinkRoot := filepath.Join(t.TempDir(), "link")
err := os.Symlink(testStorageDir, symlinkRoot)
So(err, ShouldBeNil)

c := cmd(t, &Application{
Arguments: []string{
"-vpython-spec",
testData("default.vpython3"),
"-c",
"print(123)",
},
VpythonRoot: symlinkRoot,
}, env)

So(output(c), ShouldEqual, "123")
})
}
})
}
})
Expand Down
16 changes: 12 additions & 4 deletions vpython/python/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import os
import subprocess
import sys
import tempfile

if sys.version_info[0] > 2:
ISOLATION_FLAG = '-I'
Expand All @@ -27,10 +28,17 @@
# Create virtual environment in ${out} directory
virtualenv = glob.glob(
os.path.join(os.environ['virtualenv'], '*', 'virtualenv.py*'))[0]
subprocess.check_call([
sys.executable, '-B', ISOLATION_FLAG, virtualenv,
'--no-download', '--always-copy', os.environ['out']
])

with tempfile.TemporaryDirectory() as d:
args = [
sys.executable, '-B', ISOLATION_FLAG,
# Use realpath to mitigate https://github.com/pypa/virtualenv/issues/1949
os.path.realpath(virtualenv),
'--no-download', '--always-copy', os.environ['out'],
]
if virtualenv.endswith('.pyz'):
args.extend(['--app-data', d])
subprocess.check_call(args)

# Install wheels to virtual environment
if 'wheels' in os.environ:
Expand Down

0 comments on commit 988aaae

Please sign in to comment.