From 1a2625555311b7b416dd00ee6f92ce2e5e01f992 Mon Sep 17 00:00:00 2001 From: Eran Jalink Date: Tue, 6 Jan 2026 10:23:36 +0100 Subject: [PATCH] fix: gracefully handle unavailable directories Instead of crashing when a configured directory is unavailable (e.g., unmounted external drive, disconnected network share), the server now: - Skips unavailable directories with a warning log - Continues operating with the remaining available directories - Only exits if ALL specified directories are unavailable This allows users with external drives or network shares to configure all their directories without the server failing when some are temporarily unavailable. Fixes #2815 --- src/filesystem/index.ts | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/src/filesystem/index.ts b/src/filesystem/index.ts index 48a599fae1..398e005344 100644 --- a/src/filesystem/index.ts +++ b/src/filesystem/index.ts @@ -56,20 +56,44 @@ let allowedDirectories = await Promise.all( }) ); -// Validate that all directories exist and are accessible +// Validate directories and filter out unavailable ones (graceful handling) +// Instead of crashing when a directory is unavailable, we skip it with a warning +const unavailableDirectories: string[] = []; +const validatedDirectories: string[] = []; + await Promise.all(allowedDirectories.map(async (dir) => { try { const stats = await fs.stat(dir); if (!stats.isDirectory()) { - console.error(`Error: ${dir} is not a directory`); - process.exit(1); + console.error(`Warning: Skipping ${dir} - not a directory`); + unavailableDirectories.push(dir); + } else { + validatedDirectories.push(dir); } } catch (error) { - console.error(`Error accessing directory ${dir}:`, error); - process.exit(1); + const errorMessage = error instanceof Error ? error.message : String(error); + console.error(`Warning: Skipping unavailable directory ${dir}: ${errorMessage}`); + unavailableDirectories.push(dir); } })); +// Update allowedDirectories to only include validated ones +allowedDirectories = validatedDirectories; + +// Log summary of directory validation +if (unavailableDirectories.length > 0) { + console.error(`Skipped ${unavailableDirectories.length} unavailable director${unavailableDirectories.length === 1 ? 'y' : 'ies'}: ${unavailableDirectories.join(', ')}`); +} + +if (validatedDirectories.length > 0) { + console.error(`Server starting with ${validatedDirectories.length} available director${validatedDirectories.length === 1 ? 'y' : 'ies'}: ${validatedDirectories.join(', ')}`); +} else if (args.length > 0) { + // Only exit if directories were provided but none are available + // If no args were provided, the server might still get roots from the client + console.error("Error: None of the specified directories are available. Server cannot start."); + process.exit(1); +} + // Initialize the global allowedDirectories in lib.ts setAllowedDirectories(allowedDirectories);