microsoft/TypeAgent

Public

mirrored fromhttps://github.com/microsoft/TypeAgentAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
agents/devcontainer-pnpm-install-error-fix

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

.devcontainer/scripts/post-create.sh

185lines · modecode

1#!/bin/bash
2# Copyright (c) Microsoft Corporation.
3# Licensed under the MIT License.
4
5#
6# TypeAgent DevContainer Post-Create Script
7# Runs once when the container is first created
8#
9
10echo "╔══════════════════════════════════════════════════════════════╗"
11echo "║ TypeAgent DevContainer Setup ║"
12echo "╚══════════════════════════════════════════════════════════════╝"
13echo ""
14
15# Detect environment
16detect_env() {
17 if [[ "$CODESPACES" == "true" ]]; then
18 echo "codespaces"
19 elif [[ -n "$WSL_DISTRO_NAME" ]] || grep -qi "wsl" /proc/version 2>/dev/null; then
20 if [[ -n "$WAYLAND_DISPLAY" ]] || [[ -n "$DISPLAY" ]]; then
21 echo "wsl2-gui"
22 else
23 echo "wsl2"
24 fi
25 else
26 echo "standard"
27 fi
28}
29
30ENV=$(detect_env)
31echo "Environment: $ENV"
32echo ""
33
34# Fix ownership of Docker named-volume mount points.
35# Named volumes mounted into the container are owned by root:root by default,
36# which prevents the non-root `codespace` user from writing into them
37# (e.g. `pnpm install` -> EACCES on ts/node_modules).
38echo "Fixing ownership of mounted volume directories..."
39VOLUME_PATHS=(
40 "/home/codespace/.local/share/pnpm"
41 "/home/codespace/.local/share/pnpm/store"
42 "/home/codespace/.claude"
43)
44# Discover the workspace ts/node_modules path dynamically (works for worktrees too)
45WS_TS_DIR=""
46if [[ -d "/workspaces/TypeAgent/ts" ]]; then
47 WS_TS_DIR="/workspaces/TypeAgent/ts"
48else
49 WS_TS_DIR=$(find /workspaces -maxdepth 2 -type d -name "ts" 2>/dev/null | head -1)
50fi
51if [[ -n "$WS_TS_DIR" ]]; then
52 VOLUME_PATHS+=("$WS_TS_DIR/node_modules")
53fi
54
55for p in "${VOLUME_PATHS[@]}"; do
56 if [[ -e "$p" ]]; then
57 sudo chown -R codespace:codespace "$p" 2>/dev/null \
58 && echo " chowned $p" \
59 || echo " warn: could not chown $p"
60 fi
61done
62echo ""
63
64# Navigate to TypeScript workspace
65echo "Looking for TypeScript workspace..."
66if [[ -d "/workspaces/TypeAgent/ts" ]]; then
67 cd /workspaces/TypeAgent/ts
68 echo "Found: /workspaces/TypeAgent/ts"
69else
70 # Try glob pattern
71 TS_DIR=$(find /workspaces -maxdepth 2 -type d -name "ts" 2>/dev/null | head -1)
72 if [[ -n "$TS_DIR" ]]; then
73 cd "$TS_DIR"
74 echo "Found: $TS_DIR"
75 else
76 echo "Warning: Could not find ts directory in /workspaces"
77 echo "Listing /workspaces contents:"
78 ls -la /workspaces/ 2>/dev/null || echo " /workspaces not accessible"
79 echo ""
80 echo "Skipping dependency installation. Run manually after container starts:"
81 echo " cd ts && pnpm install"
82 exit 0
83 fi
84fi
85
86# Enable pnpm
87echo ""
88echo "Enabling corepack and pnpm..."
89if command -v corepack &> /dev/null; then
90 corepack enable || echo "Warning: corepack enable failed"
91 corepack prepare pnpm@latest --activate || echo "Warning: corepack prepare failed"
92else
93 echo "Warning: corepack not found, checking for pnpm..."
94 if ! command -v pnpm &> /dev/null; then
95 echo "Installing pnpm via npm..."
96 npm install -g pnpm || { echo "Failed to install pnpm"; exit 1; }
97 fi
98fi
99
100# Verify pnpm is available
101if ! command -v pnpm &> /dev/null; then
102 echo "Error: pnpm is not available after setup"
103 exit 1
104fi
105
106echo "pnpm version: $(pnpm --version)"
107
108# Install dependencies
109echo ""
110echo "Installing pnpm dependencies..."
111echo "This may take a few minutes on first run..."
112pnpm install || {
113 echo ""
114 echo "Warning: pnpm install failed. You may need to run it manually."
115 echo "This is often due to network issues or missing system dependencies."
116}
117
118# Set up git hooks for lock file sync (non-critical)
119echo ""
120echo "Setting up git hooks for dependency synchronization..."
121
122HOOKS_DIR="../.git/hooks"
123if [[ -d "$HOOKS_DIR" ]]; then
124 # Post-checkout hook
125 cat > "$HOOKS_DIR/post-checkout" << 'EOF'
126#!/bin/bash
127PREV_HEAD=$1
128NEW_HEAD=$2
129BRANCH_CHECKOUT=$3
130
131if [ "$BRANCH_CHECKOUT" != "1" ]; then exit 0; fi
132
133LOCKFILE_CHANGED=$(git diff "$PREV_HEAD" "$NEW_HEAD" --name-only 2>/dev/null | grep -c "pnpm-lock.yaml" || true)
134
135if [ "$LOCKFILE_CHANGED" -gt 0 ]; then
136 echo "pnpm-lock.yaml changed. Running pnpm install..."
137 cd ts && pnpm install --frozen-lockfile
138 echo "Dependencies synchronized"
139fi
140EOF
141 chmod +x "$HOOKS_DIR/post-checkout"
142
143 # Post-merge hook
144 cat > "$HOOKS_DIR/post-merge" << 'EOF'
145#!/bin/bash
146LOCKFILE_CHANGED=$(git diff HEAD@{1} HEAD --name-only | grep -c "pnpm-lock.yaml" || true)
147
148if [ "$LOCKFILE_CHANGED" -gt 0 ]; then
149 echo "pnpm-lock.yaml changed after merge. Running pnpm install..."
150 cd ts && pnpm install --frozen-lockfile
151 echo "Dependencies synchronized"
152fi
153EOF
154 chmod +x "$HOOKS_DIR/post-merge"
155
156 echo "Git hooks installed for automatic dependency sync"
157else
158 echo "Note: .git/hooks directory not found, skipping git hooks setup"
159fi
160
161echo ""
162echo "╔══════════════════════════════════════════════════════════════╗"
163echo "║ Setup Complete! ║"
164echo "╚══════════════════════════════════════════════════════════════╝"
165echo ""
166echo "Next steps:"
167echo " cd ts"
168echo " pnpm run build"
169echo ""
170
171case $ENV in
172 wsl2-gui)
173 echo "GUI Support: WSLg detected - 'pnpm run shell' will work!"
174 ;;
175 codespaces)
176 echo "GUI Support: Use VNC at http://localhost:6080"
177 ;;
178 *)
179 echo "GUI Support: For Electron, use hybrid approach:"
180 echo " Container: pnpm run server"
181 echo " Host: pnpm run shell"
182 ;;
183esac
184
185echo ""
186