Skip to content
This repository was archived by the owner on Jan 12, 2025. It is now read-only.

Commit 7862f79

Browse files
[Bug]: Codespaces creation fails when pre-commit feature is enabled (#514)
1 parent 0fae85a commit 7862f79

File tree

6 files changed

+81
-38
lines changed

6 files changed

+81
-38
lines changed

src/pipx-package/devcontainer-feature.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "Pipx package",
33
"id": "pipx-package",
4-
"version": "1.1.6",
4+
"version": "1.1.7",
55
"description": "Installs a pipx package.",
66
"documentationURL": "http://github.com/devcontainers-contrib/features/tree/main/src/pipx-package",
77
"installsAfter": [
@@ -42,8 +42,8 @@
4242
"proposals": [
4343
"python3"
4444
],
45-
"default": "python3",
46-
"description": "Select the python interpreter to use"
45+
"default": "",
46+
"description": "Force python interpreter to be use (must already exists on the container). If none selected (the default) then python3 will be used (and will be installed in case it does not exists)"
4747
}
4848
}
4949
}

src/pipx-package/install.sh

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PACKAGE=${PACKAGE:-""}
55
VERSION=${VERSION:-"latest"}
66
INJECTIONS=${INJECTIONS:-""}
77
INCLUDEDEPS=${INCLUDEDEPS:-"false"}
8-
INTERPRETER=${INTERPRETER:-"python3"}
8+
INTERPRETER=${INTERPRETER:-""}
99

1010
# PEP 668 compatibility
1111
export PIP_BREAK_SYSTEM_PACKAGES=1
@@ -45,32 +45,41 @@ install_via_pipx() {
4545
local VERSION=$3
4646
local INJECTIONS=$4
4747
local INCLUDEDEPS=$5
48-
if [ "$INTERPRETER" = "python3" ]; then
4948

50-
# if no python - install it
51-
if ! type python3 >/dev/null 2>&1; then
52-
echo "installing python3-minimal libffi-dev"
53-
apt-get update -y
54-
apt-get -y install python3-minimal
55-
fi
56-
57-
# if no pip - install it
58-
if ! type pip3 >/dev/null 2>&1; then
59-
echo "installing python3-pip"
60-
apt-get update -y
61-
apt-get -y install libffi-dev python3-pip
62-
fi
63-
64-
if ! python3 -Im ensurepip --version >/dev/null 2>&1; then
65-
echo "installing python3-venv"
66-
apt-get update -y
67-
apt-get -y install python3-venv
49+
if [ -n "$INTERPRETER" ]; then
50+
if ! type $INTERPRETER >/dev/null 2>&1; then
51+
echo "interpreter given is invalid: $INTERPRETER"
52+
exit 1
6853
fi
54+
local _interpreter=$INTERPRETER
55+
else
56+
local _interpreter="python3"
6957
fi
70-
71-
if ! type "$INTERPRETER"; then
72-
echo "interpreter given is invalid: $INTERPRETER"
73-
exit 1
58+
59+
if [ -z "$INTERPRETER" ]; then # if interpreter selected manually - it should exists (validated above)
60+
61+
if [ "$_interpreter" = "python3" ]; then
62+
63+
# if no python - install it
64+
if ! type python3 >/dev/null 2>&1; then
65+
echo "installing python3-minimal libffi-dev"
66+
apt-get update -y
67+
apt-get -y install python3-minimal
68+
fi
69+
70+
# if no pip - install it
71+
if ! type pip3 >/dev/null 2>&1; then
72+
echo "installing python3-pip"
73+
apt-get update -y
74+
apt-get -y install libffi-dev python3-pip
75+
fi
76+
77+
if ! python3 -Im ensurepip --version >/dev/null 2>&1; then
78+
echo "installing python3-venv"
79+
apt-get update -y
80+
apt-get -y install python3-venv
81+
fi
82+
fi
7483
fi
7584

7685
export PYTHONUSERBASE=/tmp/pip-tmp
@@ -80,9 +89,7 @@ install_via_pipx() {
8089
export PIPX_BIN_DIR="${PIPX_HOME}/bin"
8190
mkdir -p "${PIPX_HOME}"
8291

83-
# if pipx not exists - install it
84-
if ! $INTERPRETER -m pip list | grep pipx >/dev/null 2>&1; then
85-
92+
_install_pipx() {
8693
PATH="${PATH}:${PIPX_BIN_DIR}"
8794

8895
# Create pipx group, dir, and set sticky bit
@@ -96,18 +103,34 @@ install_via_pipx() {
96103
chmod -R g+r+w "${PIPX_HOME}"
97104
find "${PIPX_HOME}" -type d -print0 | xargs -0 -n 1 chmod g+s
98105

99-
$INTERPRETER -m pip install --disable-pip-version-check --no-cache-dir --user pipx 2>&1
100-
/tmp/pip-tmp/bin/pipx install --pip-args=--no-cache-dir pipx
101-
pipx_bin=/tmp/pip-tmp/bin/pipx
106+
$_interpreter -m pip install --disable-pip-version-check --no-cache-dir --user pipx 2>&1
107+
$PYTHONUSERBASE/bin/pipx install --pip-args=--no-cache-dir --force pipx
108+
pipx_bin=$PYTHONUSERBASE/bin/pipx
102109

103110
updaterc "export PIPX_HOME=\"${PIPX_HOME}\""
104111
updaterc "export PIPX_BIN_DIR=\"${PIPX_BIN_DIR}\""
105112
updaterc "if [[ \"\${PATH}\" != *\"\${PIPX_BIN_DIR}\"* ]]; then export PATH=\"\${PATH}:\${PIPX_BIN_DIR}\"; fi"
106-
113+
}
114+
115+
116+
if $_interpreter -m pip list | grep pipx >/dev/null 2>&1; then
117+
# if pipx exists in the selected interpreter - use it
118+
pipx_bin="$_interpreter -m pipx"
119+
elif [ -n "$INTERPRETER" ]; then
120+
# if interpreter was *explicitely* selected,
121+
# and pipx is not installed with it - install it
122+
_install_pipx
123+
pipx_bin="$_interpreter -m pipx"
124+
elif type pipx >/dev/null 2>&1; then
125+
# if a global pipx is install - use it
126+
pipx_bin="pipx"
107127
else
108-
pipx_bin="$INTERPRETER -m pipx"
128+
# if no pipx installed what so ever - install it
129+
_install_pipx
130+
pipx_bin=$PYTHONUSERBASE/bin/pipx
109131
fi
110132

133+
111134
if [ "$(${pipx_bin} list --short | grep "$PACKAGE")" != "" ]; then
112135
echo "$PACKAGE already exists - skipping installation"
113136
else
@@ -130,7 +153,7 @@ install_via_pipx() {
130153
done
131154

132155
# cleaning pipx to save disk space
133-
rm -rf /tmp/pip-tmp
156+
rm -rf $PYTHONUSERBASE
134157
fi
135158
}
136159

src/pre-commit/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ $nanolayer_location \
1616
install \
1717
devcontainer-feature \
1818
"ghcr.io/devcontainers-contrib/features/pipx-package:1.1.6" \
19-
--option package='pre-commit' --option version="$VERSION"
19+
--option package='pre-commit' --option version="$VERSION" --verbose
2020

2121

2222

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
source dev-container-features-test-lib
6+
7+
check "pipx list --short | grep pre-commit" pipx list --short | grep "pre-commit"
8+
9+
check "type pre-commit" type pre-commit
10+
11+
reportResults

test/pipx-package/scenarios.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
}
2020
}
2121
},
22+
"install_precommit": {
23+
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
24+
"features": {
25+
"pipx-package": {
26+
"version": "latest",
27+
"package": "pre-commit"
28+
}
29+
}
30+
},
2231
"install_black_custom_interpreter": {
2332
"image": "debian:bullseye",
2433
"features": {

test/pre-commit/scenarios.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"test": {
3-
"image": "mcr.microsoft.com/devcontainers/base:debian",
3+
"image": "mcr.microsoft.com/devcontainers/python:1-3.11-bullseye",
44
"features": {
55
"pre-commit": {}
66
}

0 commit comments

Comments
 (0)