فهرست منبع

fix: update initialization scripts and bind mount configurations for devcontainer

Yuki Takei 5 روز پیش
والد
کامیت
9158fef1f3

+ 1 - 1
.devcontainer/app/devcontainer.json

@@ -17,7 +17,7 @@
   // Use 'forwardPorts' to make a list of ports inside the container available locally.
   // "forwardPorts": [],
 
-  "initializeCommand": "/bin/bash .devcontainer/pdf-converter/initializeCommand.sh",
+  "initializeCommand": "/bin/bash .devcontainer/scripts/init-home.sh",
   // Use 'postCreateCommand' to run commands after the container is created.
   "postCreateCommand": "/bin/bash ./.devcontainer/app/postCreateCommand.sh",
 

+ 0 - 9
.devcontainer/app/initializeCommand.sh

@@ -1,9 +0,0 @@
-# prevent file not found error on docker compose up
-if [ ! -f ".devcontainer/compose.extend.yml" ]; then
-
-cat > ".devcontainer/compose.extend.yml" <<EOF
-services:
-  {}
-EOF
-
-fi

+ 2 - 0
.devcontainer/compose.yml

@@ -5,6 +5,8 @@ services:
       - ..:/workspace/growi:delegated
       - pnpm-store:/workspace/.pnpm-store
       - ${HOME}/.claude:/home/vscode/.claude
+      - ${HOME}/.config/gh:/home/vscode/.config/gh
+      - ${HOME}/.config/glab-cli:/home/vscode/.config/glab-cli
       - ../../growi-docker-compose:/workspace/growi-docker-compose:delegated
       - ../../share:/workspace/share:delegated
       - page_bulk_export_tmp:/tmp/page-bulk-export

+ 1 - 1
.devcontainer/pdf-converter/devcontainer.json

@@ -11,7 +11,7 @@
   // Use 'forwardPorts' to make a list of ports inside the container available locally.
   // "forwardPorts": [],
 
-  "initializeCommand": "/bin/bash .devcontainer/pdf-converter/initializeCommand.sh",
+  "initializeCommand": "/bin/bash .devcontainer/scripts/init-home.sh",
   // Use 'postCreateCommand' to run commands after the container is created.
   "postCreateCommand": "/bin/bash ./.devcontainer/pdf-converter/postCreateCommand.sh",
 

+ 0 - 9
.devcontainer/pdf-converter/initializeCommand.sh

@@ -1,9 +0,0 @@
-# prevent file not found error on docker compose up
-if [ ! -f ".devcontainer/compose.extend.yml" ]; then
-
-cat > ".devcontainer/compose.extend.yml" <<EOF
-services:
-  {}
-EOF
-
-fi

+ 73 - 0
.devcontainer/scripts/init-home.sh

@@ -0,0 +1,73 @@
+#!/bin/bash
+# init-home.sh
+#
+# Host-side initialization script. Invoked from devcontainer.json's initializeCommand
+# before `docker compose up`, so that bind-mount target directories exist with the
+# correct ownership/permission on the host. Without this, Docker creates the missing
+# host directories as root, breaking writes from the non-root vscode user inside
+# the container.
+#
+# Idempotent: safe to re-run; never destroys existing content.
+#
+# - Pre-create ~/.claude/, ~/.config/gh/, ~/.config/glab-cli/ on the host so the
+#   compose bind mounts succeed.
+# - Generate .devcontainer/compose.extend.yml as an empty-stub if missing, so
+#   `docker compose up` does not fail with a missing-file error before the user
+#   has authored their own extension.
+# - Write UID/GID into .devcontainer/.env (merge, do not clobber existing keys)
+#   so compose.yml can expand ${UID}/${GID} without depending on shell exports.
+#
+# POSIX-portable (bash + coreutils). No GNU-only flags so macOS/Linux both work.
+
+set -euo pipefail
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+DEVCONTAINER_DIR="$(cd "${SCRIPT_DIR}/.." && pwd)"
+DEVCONTAINER_ENV="${DEVCONTAINER_DIR}/.env"
+COMPOSE_EXTEND="${DEVCONTAINER_DIR}/compose.extend.yml"
+
+# ---------------------------------------------------------------------------
+# Pre-create bind-mount target directories on the host.
+# mkdir -p is idempotent and preserves existing contents.
+# Creating them here (as the host user) prevents Docker from creating them
+# as root when the bind mount is first applied.
+# ---------------------------------------------------------------------------
+mkdir -p \
+    "${HOME}/.claude" \
+    "${HOME}/.config/gh" \
+    "${HOME}/.config/glab-cli"
+
+# ---------------------------------------------------------------------------
+# Stub compose.extend.yml: prevents file-not-found errors on `docker compose up`
+# for users who have not authored their own overrides yet.
+# ---------------------------------------------------------------------------
+if [ ! -f "${COMPOSE_EXTEND}" ]; then
+    cat > "${COMPOSE_EXTEND}" <<'EOF'
+services:
+  {}
+EOF
+fi
+
+# ---------------------------------------------------------------------------
+# Merge UID/GID into .devcontainer/.env without clobbering other keys.
+# compose.yml and docker compose auto-load this file as a variable source.
+# id -u / id -g are POSIX-standard on both macOS and Linux.
+# ---------------------------------------------------------------------------
+HOST_UID="$(id -u)"
+HOST_GID="$(id -g)"
+
+if [ -f "${DEVCONTAINER_ENV}" ]; then
+    # Drop any existing UID=/GID= lines, then append fresh values.
+    tmp_env="$(mktemp)"
+    grep -Ev '^(UID|GID)=' "${DEVCONTAINER_ENV}" > "${tmp_env}" || true
+    mv "${tmp_env}" "${DEVCONTAINER_ENV}"
+fi
+
+{
+    echo "UID=${HOST_UID}"
+    echo "GID=${HOST_GID}"
+} >> "${DEVCONTAINER_ENV}"
+
+echo "init-home.sh: initialization complete"
+echo "  host dirs: ~/.claude, ~/.config/gh, ~/.config/glab-cli"
+echo "  ${DEVCONTAINER_ENV}: UID=${HOST_UID}, GID=${HOST_GID}"