Skip to content

Commit 3b6f1b7

Browse files
committed
build: fix issues when opening dev container, minikube not found
Signed-off-by: Alessandro Yuichi Okimoto <yuichijpn@gmail.com>
1 parent a8266d1 commit 3b6f1b7

2 files changed

Lines changed: 63 additions & 21 deletions

File tree

.devcontainer/setup.sh

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,52 @@ print_error() {
6161
echo -e "${RED}[ERROR]${NC} $1"
6262
}
6363

64+
# Detect the current user - prefer codespace for main devcontainer compatibility
65+
CONTAINER_USER="${USER:-$(whoami)}"
66+
if ! id "$CONTAINER_USER" &>/dev/null; then
67+
# Fallback order: codespace (for base image), vscode (for features), UID 1000
68+
if id "codespace" &>/dev/null; then
69+
CONTAINER_USER="codespace"
70+
elif id "vscode" &>/dev/null; then
71+
CONTAINER_USER="vscode"
72+
else
73+
# Try to find the user with UID 1000
74+
CONTAINER_USER=$(getent passwd 1000 | cut -d: -f1 2>/dev/null || echo "codespace")
75+
if ! id "$CONTAINER_USER" &>/dev/null; then
76+
print_error "No suitable user found (codespace, vscode, or UID 1000)"
77+
exit 1
78+
fi
79+
fi
80+
fi
81+
82+
print_status "Using container user: $CONTAINER_USER"
83+
6484
# Add go-tools to PATH if not already there
65-
export PATH="/home/codespace/go-tools/bin:$PATH"
66-
export GOBIN="/home/codespace/go-tools/bin"
85+
export PATH="/home/${CONTAINER_USER}/go-tools/bin:$PATH"
86+
export GOBIN="/home/${CONTAINER_USER}/go-tools/bin"
6787

6888
# Function to fix permissions on cache directories
6989
fix_cache_permissions() {
7090
print_status "Ensuring cache directories have correct permissions..."
7191

7292
# Fix Go modules cache permissions
7393
sudo mkdir -p /go/pkg/mod /go/pkg/sumdb
74-
sudo chown -R codespace:codespace /go/pkg/mod /go/pkg/sumdb
94+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} /go/pkg/mod /go/pkg/sumdb
7595

7696
# Fix Go tools directory permissions
77-
if [ -d "/home/codespace/go-tools" ]; then
78-
sudo chown -R codespace:codespace /home/codespace/go-tools
97+
if [ -d "/home/${CONTAINER_USER}/go-tools" ]; then
98+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} /home/${CONTAINER_USER}/go-tools
7999
fi
80100

81101
# Fix Yarn cache permissions
82-
if [ -d "/home/codespace/.yarn" ]; then
83-
sudo chown -R codespace:codespace /home/codespace/.yarn
102+
if [ -d "/home/${CONTAINER_USER}/.yarn" ]; then
103+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} /home/${CONTAINER_USER}/.yarn
84104
fi
85105

86106
# Fix node_modules permissions
87107
for project in "${NODE_PROJECTS[@]}"; do
88108
if [ -d "$project/node_modules" ]; then
89-
sudo chown -R codespace:codespace "$project/node_modules"
109+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} "$project/node_modules"
90110
fi
91111
done
92112

@@ -99,20 +119,20 @@ check_go_tools() {
99119
local missing_tools=()
100120

101121
# Ensure PATH includes go-tools directory for checking
102-
export PATH="/home/codespace/go-tools/bin:$PATH"
122+
export PATH="/home/${CONTAINER_USER}/go-tools/bin:$PATH"
103123

104124
# Debug: Show where we're looking for tools
105125
if [ "${DEBUG_SETUP:-}" = "true" ]; then
106-
print_status "DEBUG: Checking for Go tools in PATH and /home/codespace/go-tools/bin/"
126+
print_status "DEBUG: Checking for Go tools in PATH and /home/${CONTAINER_USER}/go-tools/bin/"
107127
print_status "DEBUG: Current PATH: $PATH"
108-
if [ -d "/home/codespace/go-tools/bin" ]; then
109-
print_status "DEBUG: Contents of /home/codespace/go-tools/bin: $(ls -la /home/codespace/go-tools/bin/ 2>/dev/null || echo 'empty')"
128+
if [ -d "/home/${CONTAINER_USER}/go-tools/bin" ]; then
129+
print_status "DEBUG: Contents of /home/${CONTAINER_USER}/go-tools/bin: $(ls -la /home/${CONTAINER_USER}/go-tools/bin/ 2>/dev/null || echo 'empty')"
110130
fi
111131
fi
112132

113133
for tool in "${tools[@]}"; do
114134
# Check both PATH and direct file existence
115-
if command -v "$tool" &> /dev/null || [ -f "/home/codespace/go-tools/bin/$tool" ]; then
135+
if command -v "$tool" &> /dev/null || [ -f "/home/${CONTAINER_USER}/go-tools/bin/$tool" ]; then
116136
# Tool is available
117137
if [ "${DEBUG_SETUP:-}" = "true" ]; then
118138
print_success "DEBUG: Found $tool"
@@ -140,10 +160,10 @@ install_go_tools() {
140160
print_status "Installing Go development tools..."
141161

142162
# Ensure go-tools directory exists and has correct permissions
143-
sudo mkdir -p /home/codespace/go-tools/bin
144-
sudo chown -R codespace:codespace /home/codespace/go-tools
163+
sudo mkdir -p /home/${CONTAINER_USER}/go-tools/bin
164+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} /home/${CONTAINER_USER}/go-tools
145165

146-
cd /home/codespace/go-tools
166+
cd /home/${CONTAINER_USER}/go-tools
147167
if [ ! -e go.mod ]; then go mod init go-tools; fi
148168

149169
# Set additional Go environment variables for better module handling
@@ -239,7 +259,7 @@ install_node_deps() {
239259

240260
# Ensure node_modules directory has correct permissions if it exists
241261
if [ -d "$dir/node_modules" ]; then
242-
sudo chown -R codespace:codespace "$dir/node_modules"
262+
sudo chown -R ${CONTAINER_USER}:${CONTAINER_USER} "$dir/node_modules"
243263
fi
244264

245265
cd "$dir"

.github/.devcontainer/local-features/setup-user/install.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ USERNAME=${USERNAME:-"codespace"}
77

88
set -eux
99

10+
# Check if the specified user exists, if not use the actual container user
11+
if ! id "${USERNAME}" &>/dev/null; then
12+
echo "User ${USERNAME} does not exist. Detecting current user..."
13+
# Get the actual user that will be used in the container
14+
ACTUAL_USER=$(getent passwd 1000 | cut -d: -f1 2>/dev/null || echo "vscode")
15+
if id "${ACTUAL_USER}" &>/dev/null; then
16+
USERNAME="${ACTUAL_USER}"
17+
echo "Using existing user: ${USERNAME}"
18+
else
19+
echo "Neither ${USERNAME} nor ${ACTUAL_USER} exists. Using root for permissions."
20+
USERNAME="root"
21+
fi
22+
fi
23+
1024
if [ "$(id -u)" -ne 0 ]; then
1125
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
1226
exit 1
@@ -23,16 +37,24 @@ export DEBIAN_FRONTEND=noninteractive
2337

2438
# Set up user home directory permissions
2539
HOME_DIR="/home/${USERNAME}/"
26-
if [ -d "${HOME_DIR}" ]; then
40+
if [ -d "${HOME_DIR}" ] && [ "${USERNAME}" != "root" ]; then
41+
echo "Setting up permissions for user home directory: ${HOME_DIR}"
2742
chown -R ${USERNAME}:${USERNAME} ${HOME_DIR}
2843
chmod -R g+r+w "${HOME_DIR}"
2944
find "${HOME_DIR}" -type d | xargs -n 1 chmod g+s
45+
elif [ "${USERNAME}" = "root" ]; then
46+
echo "Skipping home directory setup for root user"
3047
fi
3148

3249
# Create npm global directory for user
33-
NPM_GLOBAL_DIR="/home/${USERNAME}/.npm-global"
34-
mkdir -p "${NPM_GLOBAL_DIR}"
35-
chown -R ${USERNAME}:${USERNAME} "${NPM_GLOBAL_DIR}"
50+
if [ "${USERNAME}" != "root" ]; then
51+
NPM_GLOBAL_DIR="/home/${USERNAME}/.npm-global"
52+
mkdir -p "${NPM_GLOBAL_DIR}"
53+
chown -R ${USERNAME}:${USERNAME} "${NPM_GLOBAL_DIR}"
54+
echo "Created NPM global directory: ${NPM_GLOBAL_DIR}"
55+
else
56+
echo "Skipping NPM global directory setup for root user"
57+
fi
3658

3759
# Configure sudo PATH
3860
echo "Defaults secure_path=\"/usr/local/go/bin:/go/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/${USERNAME}/.local/bin\"" >>/etc/sudoers.d/$USERNAME

0 commit comments

Comments
 (0)