From 444cf040a6303eb564abff0d2023b32743f70eea Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Tue, 28 Oct 2025 17:37:06 +0000 Subject: [PATCH] refactor(execute,code-repository): allow ExecOptions in Execute.executeCommand and use cwd instead of 'cd' - Extend Execute.executeCommand to accept ExecOptions and forward them to child_process.exec - Log stderr on error and debug-log stderr when present - Update CodeRepository to pass cwd to Execute.executeCommand instead of prefixing commands with "cd" --- Common/Server/Utils/Execute.ts | 29 +++++++++++++++++++++++++---- Copilot/Utils/CodeRepository.ts | 6 +++--- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/Common/Server/Utils/Execute.ts b/Common/Server/Utils/Execute.ts index 22cf866780..9b388bb3c6 100644 --- a/Common/Server/Utils/Execute.ts +++ b/Common/Server/Utils/Execute.ts @@ -1,26 +1,47 @@ import { PromiseRejectErrorFunction } from "../../Types/FunctionTypes"; -import { ExecException, exec, execFile } from "node:child_process"; +import { + ExecException, + ExecOptions, + exec, + execFile, +} from "node:child_process"; import logger from "./Logger"; import CaptureSpan from "./Telemetry/CaptureSpan"; export default class Execute { @CaptureSpan() - public static executeCommand(command: string): Promise { + public static executeCommand( + command: string, + options?: ExecOptions, + ): Promise { return new Promise( ( resolve: (output: string) => void, reject: PromiseRejectErrorFunction, ) => { - exec(`${command}`, (err: ExecException | null, stdout: string) => { + exec( + `${command}`, + { + ...options, + }, + (err: ExecException | null, stdout: string, stderr: string) => { if (err) { logger.error(`Error executing command: ${command}`); logger.error(err); logger.error(stdout); + if (stderr) { + logger.error(stderr); + } return reject(err); } + if (stderr) { + logger.debug(stderr); + } + return resolve(stdout); - }); + }, + ); }, ); } diff --git a/Copilot/Utils/CodeRepository.ts b/Copilot/Utils/CodeRepository.ts index 008421bbdc..3dc38b91a4 100644 --- a/Copilot/Utils/CodeRepository.ts +++ b/Copilot/Utils/CodeRepository.ts @@ -305,9 +305,9 @@ export default class CodeRepositoryUtil { for (const command of commands) { logger.info(`Executing command: ${command}`); - const commandResult: string = await Execute.executeCommand( - `cd ${this.getLocalRepositoryPath()} && ${command}`, - ); + const commandResult: string = await Execute.executeCommand(command, { + cwd: this.getLocalRepositoryPath(), + }); if (commandResult) { logger.info(`Command result: ${commandResult}`); results.push(commandResult);