Skip to content

Commit 59b4bb8

Browse files
tadasantclaude
andcommitted
feat(gw): add robust npm installation with automatic cleanup on failure
- Added install_deps_robust() function that retries with rm -rf node_modules on failure - Enhanced monorepo handling to clean all node_modules directories before installation - Prevents module resolution issues mentioned in CLAUDE.md learnings - Provides clear feedback during installation process 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dea18b4 commit 59b4bb8

File tree

1 file changed

+55
-4
lines changed

1 file changed

+55
-4
lines changed

bin/gw.sh

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,62 @@ if [ $? -eq 0 ]; then
219219
fi
220220
}
221221
222+
# Function to robustly install dependencies with cleanup if needed
223+
install_deps_robust() {
224+
local dir="$1"
225+
local name="$2"
226+
227+
cd "$dir"
228+
229+
# First attempt: try normal installation
230+
if [ -f "package-lock.json" ]; then
231+
echo " 📦 Installing dependencies in $name..."
232+
if npm ci --no-audit --no-fund >/dev/null 2>&1; then
233+
echo "$name dependencies installed"
234+
return 0
235+
fi
236+
else
237+
echo " 📦 Installing dependencies in $name..."
238+
if npm install --no-audit --no-fund >/dev/null 2>&1; then
239+
echo "$name dependencies installed"
240+
return 0
241+
fi
242+
fi
243+
244+
# If first attempt failed, clean and retry
245+
echo " ⚠️ Initial install failed, cleaning node_modules and retrying..."
246+
rm -rf node_modules
247+
248+
if [ -f "package-lock.json" ]; then
249+
if npm ci --no-audit --no-fund >/dev/null 2>&1; then
250+
echo "$name dependencies installed after cleanup"
251+
return 0
252+
fi
253+
else
254+
if npm install --no-audit --no-fund >/dev/null 2>&1; then
255+
echo "$name dependencies installed after cleanup"
256+
return 0
257+
fi
258+
fi
259+
260+
echo " ❌ Failed to install dependencies in $name even after cleanup"
261+
return 1
262+
}
263+
222264
# Check if it's a monorepo by looking for workspaces in package.json
223265
cd "$NEW_WORKTREE_PATH"
224266
if grep -q '"workspaces"' package.json 2>/dev/null; then
225267
echo " 🔍 Detected monorepo with workspaces"
226268
269+
# For monorepos, we need to be extra careful about module resolution
270+
# Clean install from root to avoid module resolution issues
271+
echo " 🧹 Performing clean installation for monorepo..."
272+
echo " (This ensures proper module resolution across workspaces)"
273+
274+
# Remove all node_modules directories to ensure clean state
275+
echo " 📦 Cleaning existing node_modules..."
276+
find . -name "node_modules" -type d -prune -exec rm -rf {} \; 2>/dev/null || true
277+
227278
# Use the dedicated monorepo install script
228279
# First try the main worktree, then try the current worktree
229280
INSTALL_SCRIPT="$MAIN_WORKTREE/scripts/install-monorepo-deps.sh"
@@ -246,12 +297,12 @@ if [ $? -eq 0 ]; then
246297
echo " 💡 Tip: Run 'tail -f .gw-install.log' in the new worktree to monitor progress"
247298
else
248299
echo " ⚠️ Monorepo install script not found at: $INSTALL_SCRIPT"
249-
echo " Running standard npm install instead..."
250-
install_deps "$NEW_WORKTREE_PATH" "root"
300+
echo " Running robust npm install instead..."
301+
install_deps_robust "$NEW_WORKTREE_PATH" "root"
251302
fi
252303
else
253-
# Not a monorepo, install normally
254-
install_deps "$NEW_WORKTREE_PATH" "root"
304+
# Not a monorepo, install normally with robust fallback
305+
install_deps_robust "$NEW_WORKTREE_PATH" "root"
255306
fi
256307
257308
cd "$CURRENT_ROOT"

0 commit comments

Comments
 (0)