From e79064c853268b51afd72c9db26ff3193ecbca2c Mon Sep 17 00:00:00 2001 From: Spencer Tang Date: Wed, 8 Apr 2026 17:06:00 -0400 Subject: [PATCH] fix(core): refactor shell parser initialization state and timeout --- packages/core/src/utils/shell-utils.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/core/src/utils/shell-utils.ts b/packages/core/src/utils/shell-utils.ts index 3e69fb4de3..b0329c6e05 100644 --- a/packages/core/src/utils/shell-utils.ts +++ b/packages/core/src/utils/shell-utils.ts @@ -108,6 +108,8 @@ export async function resolveExecutable( let bashLanguage: Language | null = null; let treeSitterInitialization: Promise | null = null; let treeSitterInitializationError: Error | null = null; +let parserState: 'uninitialized' | 'initializing' | 'initialized' | 'error' = + 'uninitialized'; class ShellParserInitializationError extends Error { constructor(cause: Error) { @@ -163,12 +165,13 @@ async function loadBashLanguage(): Promise { } export async function initializeShellParsers(): Promise { - if (!treeSitterInitialization) { + if (parserState === 'uninitialized') { + parserState = 'initializing'; let timerId: NodeJS.Timeout | undefined; const timeoutPromise = new Promise((_, reject) => { timerId = setTimeout( () => reject(new Error('Tree-sitter initialization timed out')), - 5000, + 30000, ); }); treeSitterInitialization = Promise.race([ @@ -178,7 +181,11 @@ export async function initializeShellParsers(): Promise { .finally(() => { if (timerId) clearTimeout(timerId); }) + .then(() => { + parserState = 'initialized'; + }) .catch((error) => { + parserState = 'error'; treeSitterInitialization = null; // Log the error but don't throw, allowing the application to fall back to safe defaults (ASK_USER) // or regex checks where appropriate. @@ -186,7 +193,9 @@ export async function initializeShellParsers(): Promise { }); } - await treeSitterInitialization; + if (treeSitterInitialization) { + await treeSitterInitialization; + } } export interface ParsedCommandDetail {