refactor: make devcontainer configuration portable and user-agnostic

Improve devcontainer robustness by eliminating hardcoded paths and user assumptions:

devcontainer.json changes:
- Replace hardcoded /workspaces/velxio paths with ${containerWorkspaceFolder} variable
- Ensures compatibility with GitHub Codespaces and other workspace path variations
- Update all volume mount targets and PATH environment variable to use dynamic paths

post-create.sh improvements:
- Dynamically detect workspace root from script location instead of assuming /workspaces/velxio
- Use $(whoami) to get current user instead of hardcoding 'vscode' user
- Replace absolute paths with relative paths for all operations
- Makes script work correctly in any devcontainer environment (VS Code, Codespaces, Gitpod)

post-start.sh improvements:
- Apply same dynamic workspace root detection pattern
- Use current user instead of hardcoded 'vscode' for chown operations
- Ensures ownership fixes work regardless of container user configuration

These changes enable the devcontainer to work seamlessly across different hosting environments without requiring manual configuration adjustments.
This commit is contained in:
Sipho Mokoena 2026-03-27 04:43:29 +02:00
parent c1b9244341
commit c0720f0743
3 changed files with 31 additions and 21 deletions

View File

@ -20,17 +20,17 @@
"remoteUser": "vscode",
"waitFor": "postCreateCommand",
"mounts": [
"source=${localWorkspaceFolder}/wokwi-libs,target=/workspaces/velxio/wokwi-libs,type=bind,consistency=cached",
"source=${localWorkspaceFolder}/wokwi-libs,target=${containerWorkspaceFolder}/wokwi-libs,type=bind,consistency=cached",
"source=velxio-arduino-cache,target=/home/vscode/.arduino15,type=volume",
"source=velxio-frontend-nodemodules,target=/workspaces/velxio/frontend/node_modules,type=volume",
"source=velxio-avr8js-nodemodules,target=/workspaces/velxio/wokwi-libs/avr8js/node_modules,type=volume",
"source=velxio-rp2040js-nodemodules,target=/workspaces/velxio/wokwi-libs/rp2040js/node_modules,type=volume",
"source=velxio-wokwi-elements-nodemodules,target=/workspaces/velxio/wokwi-libs/wokwi-elements/node_modules,type=volume"
"source=velxio-frontend-nodemodules,target=${containerWorkspaceFolder}/frontend/node_modules,type=volume",
"source=velxio-avr8js-nodemodules,target=${containerWorkspaceFolder}/wokwi-libs/avr8js/node_modules,type=volume",
"source=velxio-rp2040js-nodemodules,target=${containerWorkspaceFolder}/wokwi-libs/rp2040js/node_modules,type=volume",
"source=velxio-wokwi-elements-nodemodules,target=${containerWorkspaceFolder}/wokwi-libs/wokwi-elements/node_modules,type=volume"
],
"postCreateCommand": "bash .devcontainer/post-create.sh",
"postStartCommand": "bash .devcontainer/post-start.sh",
"remoteEnv": {
"PATH": "${containerEnv:PATH}:/workspaces/velxio/frontend/node_modules/.bin:/workspaces/velxio/backend/venv/bin"
"PATH": "${containerEnv:PATH}:${containerWorkspaceFolder}/frontend/node_modules/.bin:${containerWorkspaceFolder}/backend/venv/bin"
},
"forwardPorts": [5173, 8001],
"portsAttributes": {

View File

@ -1,15 +1,20 @@
#!/bin/bash
set -e
cd /workspaces/velxio
# Get the workspace root (devcontainer sets this as pwd, but we can also derive it from script location)
WORKSPACE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$WORKSPACE_ROOT"
# Get current user dynamically
CURRENT_USER=$(whoami)
echo "==> Fixing ownership for mounted volumes..."
# Fix ownership of directories that are mounted as volumes
sudo chown -R vscode:vscode /workspaces/velxio/frontend/node_modules 2>/dev/null || true
sudo chown -R vscode:vscode /workspaces/velxio/wokwi-libs/avr8js/node_modules 2>/dev/null || true
sudo chown -R vscode:vscode /workspaces/velxio/wokwi-libs/rp2040js/node_modules 2>/dev/null || true
sudo chown -R vscode:vscode /workspaces/velxio/wokwi-libs/wokwi-elements/node_modules 2>/dev/null || true
sudo chown -R vscode:vscode /home/vscode/.arduino15 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" frontend/node_modules 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" wokwi-libs/avr8js/node_modules 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" wokwi-libs/rp2040js/node_modules 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" wokwi-libs/wokwi-elements/node_modules 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" "$HOME/.arduino15" 2>/dev/null || true
echo "==> Installing arduino-cli..."
if ! command -v arduino-cli &> /dev/null; then
@ -31,28 +36,28 @@ if ! sudo arduino-cli core list | grep -q "rp2040:rp2040"; then
fi
echo "==> Setting up Python virtual environment (base layer)..."
(cd /workspaces/velxio/backend
(cd backend
python3 -m venv venv
./venv/bin/pip install wheel setuptools
./venv/bin/pip install -r requirements.txt) &
echo "==> Installing frontend dependencies..."
(cd /workspaces/velxio/frontend
(cd frontend
HUSKY=0 npm install) &
echo "==> Building wokwi-libs..."
# Local wokwi-libs are cloned from GitHub instead of using npm packages.
# This allows us to modify the emulators and components for Velxio-specific behavior.
# Build outputs go to dist/ directories and are resolved via Vite aliases.
(cd /workspaces/velxio/wokwi-libs/avr8js
(cd wokwi-libs/avr8js
HUSKY=0 npm install
npm run build) &
(cd /workspaces/velxio/wokwi-libs/rp2040js
(cd wokwi-libs/rp2040js
HUSKY=0 npm install
npm run build) &
(cd /workspaces/velxio/wokwi-libs/wokwi-elements
(cd wokwi-libs/wokwi-elements
HUSKY=0 npm install
npm run build) &

View File

@ -1,16 +1,21 @@
#!/bin/bash
set -e
cd /workspaces/velxio
# Get the workspace root
WORKSPACE_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$WORKSPACE_ROOT"
# Get current user dynamically
CURRENT_USER=$(whoami)
echo "==> Fixing ownership for mounted volumes..."
sudo chown -R vscode:vscode /workspaces/velxio/frontend/node_modules 2>/dev/null || true
sudo chown -R "$CURRENT_USER:$CURRENT_USER" frontend/node_modules 2>/dev/null || true
echo "==> Syncing Python dependencies..."
(cd /workspaces/velxio/backend
(cd backend
source venv/bin/activate
pip install -q -r requirements.txt)
echo "==> Syncing frontend dependencies..."
cd /workspaces/velxio/frontend
cd frontend
HUSKY=0 npm install