@@ -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
6989fix_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 "
0 commit comments