Compare commits
1 Commits
main
...
fix/shell-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90fda1400e |
@ -108,6 +108,8 @@ export async function resolveExecutable(
|
|||||||
let bashLanguage: Language | null = null;
|
let bashLanguage: Language | null = null;
|
||||||
let treeSitterInitialization: Promise<void> | null = null;
|
let treeSitterInitialization: Promise<void> | null = null;
|
||||||
let treeSitterInitializationError: Error | null = null;
|
let treeSitterInitializationError: Error | null = null;
|
||||||
|
let parserState: 'uninitialized' | 'initializing' | 'initialized' | 'error' =
|
||||||
|
'uninitialized';
|
||||||
|
|
||||||
class ShellParserInitializationError extends Error {
|
class ShellParserInitializationError extends Error {
|
||||||
constructor(cause: Error) {
|
constructor(cause: Error) {
|
||||||
@ -163,16 +165,37 @@ async function loadBashLanguage(): Promise<void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function initializeShellParsers(): Promise<void> {
|
export async function initializeShellParsers(): Promise<void> {
|
||||||
if (!treeSitterInitialization) {
|
if (parserState === 'uninitialized') {
|
||||||
treeSitterInitialization = loadBashLanguage().catch((error) => {
|
parserState = 'initializing';
|
||||||
treeSitterInitialization = null;
|
let timerId: NodeJS.Timeout | undefined;
|
||||||
// Log the error but don't throw, allowing the application to fall back to safe defaults (ASK_USER)
|
const timeoutPromise = new Promise<void>((_, reject) => {
|
||||||
// or regex checks where appropriate.
|
timerId = setTimeout(
|
||||||
debugLogger.debug('Failed to initialize shell parsers:', error);
|
() => reject(new Error('Tree-sitter initialization timed out')),
|
||||||
|
30000,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
treeSitterInitialization = Promise.race([
|
||||||
|
loadBashLanguage(),
|
||||||
|
timeoutPromise,
|
||||||
|
])
|
||||||
|
.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.
|
||||||
|
debugLogger.debug('Failed to initialize shell parsers:', error);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
await treeSitterInitialization;
|
if (treeSitterInitialization) {
|
||||||
|
await treeSitterInitialization;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ParsedCommandDetail {
|
export interface ParsedCommandDetail {
|
||||||
@ -847,34 +870,40 @@ export const spawnAsync = async (
|
|||||||
|
|
||||||
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
|
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
try {
|
||||||
const child = spawn(finalCommand, finalArgs, {
|
return await new Promise((resolve, reject) => {
|
||||||
...options,
|
const child = spawn(finalCommand, finalArgs, {
|
||||||
env: finalEnv,
|
...options,
|
||||||
});
|
env: finalEnv,
|
||||||
let stdout = '';
|
});
|
||||||
let stderr = '';
|
let stdout = '';
|
||||||
|
let stderr = '';
|
||||||
|
|
||||||
child.stdout.on('data', (data) => {
|
child.stdout.on('data', (data) => {
|
||||||
stdout += data.toString();
|
stdout += data.toString();
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr.on('data', (data) => {
|
child.stderr.on('data', (data) => {
|
||||||
stderr += data.toString();
|
stderr += data.toString();
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('close', (code) => {
|
child.on('close', (code) => {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
resolve({ stdout, stderr });
|
resolve({ stdout, stderr });
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Command failed with exit code ${code}:\n${stderr}`));
|
reject(
|
||||||
}
|
new Error(`Command failed with exit code ${code}:\n${stderr}`),
|
||||||
});
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
child.on('error', (err) => {
|
child.on('error', (err) => {
|
||||||
reject(err);
|
reject(err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
} finally {
|
||||||
|
prepared.cleanup?.();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -902,109 +931,115 @@ export async function* execStreaming(
|
|||||||
env: options?.env ?? process.env,
|
env: options?.env ?? process.env,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
|
|
||||||
|
|
||||||
const child = spawn(finalCommand, finalArgs, {
|
|
||||||
...options,
|
|
||||||
env: finalEnv,
|
|
||||||
// ensure we don't open a window on windows if possible/relevant
|
|
||||||
windowsHide: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const rl = readline.createInterface({
|
|
||||||
input: child.stdout,
|
|
||||||
terminal: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
const errorChunks: Buffer[] = [];
|
|
||||||
let stderrTotalBytes = 0;
|
|
||||||
const MAX_STDERR_BYTES = 20 * 1024; // 20KB limit
|
|
||||||
|
|
||||||
child.stderr.on('data', (chunk) => {
|
|
||||||
if (stderrTotalBytes < MAX_STDERR_BYTES) {
|
|
||||||
errorChunks.push(chunk);
|
|
||||||
stderrTotalBytes += chunk.length;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let error: Error | null = null;
|
|
||||||
child.on('error', (err) => {
|
|
||||||
error = err;
|
|
||||||
});
|
|
||||||
|
|
||||||
const onAbort = () => {
|
|
||||||
// If manually aborted by signal, we kill immediately.
|
|
||||||
if (!child.killed) child.kill();
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options?.signal?.aborted) {
|
|
||||||
onAbort();
|
|
||||||
} else {
|
|
||||||
options?.signal?.addEventListener('abort', onAbort);
|
|
||||||
}
|
|
||||||
|
|
||||||
let finished = false;
|
|
||||||
try {
|
try {
|
||||||
for await (const line of rl) {
|
const { program: finalCommand, args: finalArgs, env: finalEnv } = prepared;
|
||||||
if (options?.signal?.aborted) break;
|
|
||||||
yield line;
|
|
||||||
}
|
|
||||||
finished = true;
|
|
||||||
} finally {
|
|
||||||
rl.close();
|
|
||||||
options?.signal?.removeEventListener('abort', onAbort);
|
|
||||||
|
|
||||||
// Ensure process is killed when the generator is closed (consumer breaks loop)
|
const child = spawn(finalCommand, finalArgs, {
|
||||||
let killedByGenerator = false;
|
...options,
|
||||||
if (!finished && child.exitCode === null && !child.killed) {
|
env: finalEnv,
|
||||||
try {
|
// ensure we don't open a window on windows if possible/relevant
|
||||||
child.kill();
|
windowsHide: true,
|
||||||
} catch {
|
});
|
||||||
// ignore error if process is already dead
|
|
||||||
|
const rl = readline.createInterface({
|
||||||
|
input: child.stdout,
|
||||||
|
terminal: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
const errorChunks: Buffer[] = [];
|
||||||
|
let stderrTotalBytes = 0;
|
||||||
|
const MAX_STDERR_BYTES = 20 * 1024; // 20KB limit
|
||||||
|
|
||||||
|
child.stderr.on('data', (chunk) => {
|
||||||
|
if (stderrTotalBytes < MAX_STDERR_BYTES) {
|
||||||
|
errorChunks.push(chunk);
|
||||||
|
stderrTotalBytes += chunk.length;
|
||||||
}
|
}
|
||||||
killedByGenerator = true;
|
});
|
||||||
|
|
||||||
|
let error: Error | null = null;
|
||||||
|
child.on('error', (err) => {
|
||||||
|
error = err;
|
||||||
|
});
|
||||||
|
|
||||||
|
const onAbort = () => {
|
||||||
|
// If manually aborted by signal, we kill immediately.
|
||||||
|
if (!child.killed) child.kill();
|
||||||
|
};
|
||||||
|
|
||||||
|
if (options?.signal?.aborted) {
|
||||||
|
onAbort();
|
||||||
|
} else {
|
||||||
|
options?.signal?.addEventListener('abort', onAbort);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we wait for the process to exit to check codes
|
let finished = false;
|
||||||
await new Promise<void>((resolve, reject) => {
|
try {
|
||||||
// If an error occurred before we got here (e.g. spawn failure), reject immediately.
|
for await (const line of rl) {
|
||||||
if (error) {
|
if (options?.signal?.aborted) break;
|
||||||
reject(error);
|
yield line;
|
||||||
return;
|
}
|
||||||
|
finished = true;
|
||||||
|
} finally {
|
||||||
|
rl.close();
|
||||||
|
options?.signal?.removeEventListener('abort', onAbort);
|
||||||
|
|
||||||
|
// Ensure process is killed when the generator is closed (consumer breaks loop)
|
||||||
|
let killedByGenerator = false;
|
||||||
|
if (!finished && child.exitCode === null && !child.killed) {
|
||||||
|
try {
|
||||||
|
child.kill();
|
||||||
|
} catch {
|
||||||
|
// ignore error if process is already dead
|
||||||
|
}
|
||||||
|
killedByGenerator = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkExit(code: number | null) {
|
// Ensure we wait for the process to exit to check codes
|
||||||
// If we aborted or killed it manually, we treat it as success (stop waiting)
|
await new Promise<void>((resolve, reject) => {
|
||||||
if (options?.signal?.aborted || killedByGenerator) {
|
// If an error occurred before we got here (e.g. spawn failure), reject immediately.
|
||||||
resolve();
|
if (error) {
|
||||||
|
reject(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const allowed = options?.allowedExitCodes ?? [0];
|
function checkExit(code: number | null) {
|
||||||
if (code !== null && allowed.includes(code)) {
|
// If we aborted or killed it manually, we treat it as success (stop waiting)
|
||||||
resolve();
|
if (options?.signal?.aborted || killedByGenerator) {
|
||||||
} else {
|
resolve();
|
||||||
// If we have an accumulated error or explicit error event
|
return;
|
||||||
if (error) reject(error);
|
}
|
||||||
else {
|
|
||||||
const stderr = Buffer.concat(errorChunks).toString('utf8');
|
const allowed = options?.allowedExitCodes ?? [0];
|
||||||
const truncatedMsg =
|
if (code !== null && allowed.includes(code)) {
|
||||||
stderrTotalBytes >= MAX_STDERR_BYTES ? '...[truncated]' : '';
|
resolve();
|
||||||
reject(
|
} else {
|
||||||
new Error(
|
// If we have an accumulated error or explicit error event
|
||||||
`Process exited with code ${code}: ${stderr}${truncatedMsg}`,
|
if (error) reject(error);
|
||||||
),
|
else {
|
||||||
);
|
const stderr = Buffer.concat(errorChunks).toString('utf8');
|
||||||
|
const truncatedMsg =
|
||||||
|
stderrTotalBytes >= MAX_STDERR_BYTES ? '...[truncated]' : '';
|
||||||
|
reject(
|
||||||
|
new Error(
|
||||||
|
`Process exited with code ${code}: ${stderr}${truncatedMsg}`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (child.exitCode !== null) {
|
if (child.exitCode !== null) {
|
||||||
checkExit(child.exitCode);
|
checkExit(child.exitCode);
|
||||||
} else {
|
} else {
|
||||||
child.on('close', (code) => checkExit(code));
|
child.on('close', (code) => checkExit(code));
|
||||||
child.on('error', (err) => reject(err));
|
child.on('error', (err) => {
|
||||||
}
|
reject(err);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
prepared.cleanup?.();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user