Skip to content

Commit d9910bc

Browse files
authored
Warn when Python debugging library path is not found (#71)
* buildx now requires a context * Append any library paths to PYTHONPATH to allow users to manually override file locations if required.
1 parent b84ad4b commit d9910bc

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

buildx.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ PLATFORMS=linux/amd64,linux/arm64
77

88
if ! docker buildx inspect skaffold-builder >/dev/null 2>&1; then
99
echo ">> creating "docker buildx" builder 'skaffold-builder'"
10-
docker buildx create --name skaffold-builder --platform $PLATFORMS
10+
# Docker 3.3.0 require creating a builder within a context
11+
docker context create skaffold
12+
docker buildx create --name skaffold-builder --platform $PLATFORMS skaffold
1113
fi
1214

1315
loadOrPush=$(if [ "$PUSH_IMAGE" = true ]; then echo --platform $PLATFORMS --push; else echo --load; fi)

python/helper-image/launcher/env.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ func (e env) AsPairs() []string {
4444
return m
4545
}
4646

47-
// PrependFilepath prepands a path to a environment variable.
48-
func (e env) PrependFilepath(key string, path string) {
47+
// AppendFilepath appands a path to a environment variable.
48+
func (e env) AppendFilepath(key string, path string) {
4949
v := e[key]
5050
if v != "" {
51-
v = path + string(filepath.ListSeparator) + v
51+
v = v + string(filepath.ListSeparator) + path
5252
} else {
5353
v = path
5454
}

python/helper-image/launcher/env_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestEnvFromPairs(t *testing.T) {
8080
}
8181
}
8282

83-
func TestEnvPrependFilepath(t *testing.T) {
83+
func TestEnvAppendFilepath(t *testing.T) {
8484
tests := []struct {
8585
description string
8686
env env
@@ -89,15 +89,15 @@ func TestEnvPrependFilepath(t *testing.T) {
8989
expected map[string]string
9090
}{
9191
{"empty", env{}, "PATH", "value", env{"PATH": "value"}},
92-
{"existing value", env{"PATH": "other"}, "PATH", "value", env{"PATH": "value" + string(filepath.ListSeparator) + "other"}},
92+
{"existing value", env{"PATH": "other"}, "PATH", "value", env{"PATH": "other" + string(filepath.ListSeparator) + "value"}},
9393
{"other value unchanged", env{"PYTHONPATH": "other"}, "PATH", "value", env{"PATH": "value", "PYTHONPATH": "other"}},
94-
{"existing value with other value unchanged", env{"PATH": "other", "PYTHONPATH": "other"}, "PATH", "value", env{"PATH": "value" + string(filepath.ListSeparator) + "other", "PYTHONPATH": "other"}},
94+
{"existing value with other value unchanged", env{"PATH": "other", "PYTHONPATH": "other"}, "PATH", "value", env{"PATH": "other" + string(filepath.ListSeparator) + "value", "PYTHONPATH": "other"}},
9595
}
9696

9797
for _, test := range tests {
9898
t.Run(test.description, func(t *testing.T) {
9999
result := test.env // not a copy but that's ok for this test
100-
result.PrependFilepath(test.key, test.value)
100+
result.AppendFilepath(test.key, test.value)
101101
if len(result) != len(test.expected) {
102102
t.Errorf("expected %v but got %v", test.expected, result)
103103
} else {

python/helper-image/launcher/launcher.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,19 +257,25 @@ func (pc *pythonContext) updateEnv(ctx context.Context) error {
257257
}
258258
// The skaffold-debug-python helper image places pydevd and debugpy in /dbg/python/lib/pythonM.N,
259259
// but separates pydevd and pydevd-pycharm in separate directories to avoid possible leakage.
260+
var libraryPath string
260261
switch pc.debugMode {
261262
case ModePtvsd, ModeDebugpy:
262-
libraryPath := fmt.Sprintf(dbgRoot+"/python/lib/python%d.%d/site-packages", major, minor)
263-
pc.env.PrependFilepath("PYTHONPATH", libraryPath)
263+
libraryPath = fmt.Sprintf(dbgRoot+"/python/lib/python%d.%d/site-packages", major, minor)
264264

265265
case ModePydevd:
266-
libraryPath := fmt.Sprintf(dbgRoot+"/python/pydevd/python%d.%d/lib/python%d.%d/site-packages", major, minor, major, minor)
267-
pc.env.PrependFilepath("PYTHONPATH", libraryPath)
266+
libraryPath = fmt.Sprintf(dbgRoot+"/python/pydevd/python%d.%d/lib/python%d.%d/site-packages", major, minor, major, minor)
267+
268268
case ModePydevdPycharm:
269-
libraryPath := fmt.Sprintf(dbgRoot+"/python/pydevd-pycharm/python%d.%d/lib/python%d.%d/site-packages", major, minor, major, minor)
270-
pc.env.PrependFilepath("PYTHONPATH", libraryPath)
269+
libraryPath = fmt.Sprintf(dbgRoot+"/python/pydevd-pycharm/python%d.%d/lib/python%d.%d/site-packages", major, minor, major, minor)
270+
}
271+
if libraryPath != "" {
272+
if !pathExists(libraryPath) {
273+
// Warn as the user may have installed debugpy themselves
274+
logrus.Warnf("Debugging support for Python %d.%d not found: may require manually installing %q", major, minor, pc.debugMode)
275+
}
276+
// Append to ensure user-configured values are found first.
277+
pc.env.AppendFilepath("PYTHONPATH", libraryPath)
271278
}
272-
273279
return nil
274280
}
275281

@@ -358,3 +364,11 @@ func logrusLevel(env env) logrus.Level {
358364
}
359365
return logrus.WarnLevel
360366
}
367+
368+
func pathExists(path string) bool {
369+
_, err := os.Stat(path)
370+
if err == nil || !os.IsNotExist(err) {
371+
return true
372+
}
373+
return false
374+
}

python/helper-image/launcher/launcher_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"io/ioutil"
23+
"path/filepath"
2324
"testing"
2425

2526
"github.com/google/go-cmp/cmp"
@@ -293,3 +294,12 @@ func TestLaunch(t *testing.T) {
293294
})
294295
}
295296
}
297+
298+
func TestPathExists(t *testing.T) {
299+
if pathExists(filepath.Join("this", "should", "not", "exist")) {
300+
t.Error("pathExists should have failed on non-existent path")
301+
}
302+
if !pathExists(t.TempDir()) {
303+
t.Error("pathExists failed on real path")
304+
}
305+
}

0 commit comments

Comments
 (0)