Skip to content

Commit 1921a5d

Browse files
committed
fix(vscode): improve shell execution and error handling
- Fixes issue: #2 - Extract correct exit code from shell output - Remove unnecessary comment in shell execution - Improve abort handling in shell execution - Add shell execution tests - Bump version to 1.69.0+4
1 parent 476c141 commit 1921a5d

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

vscode/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "cody-ai",
44
"private": true,
55
"displayName": "Cody: AI Code Assistant",
6-
"version": "1.69.0+3",
6+
"version": "1.69.0+4",
77
"publisher": "sourcegraph",
88
"license": "Apache-2.0",
99
"icon": "resources/sourcegraph.png",

vscode/src/commands/context/shell.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ export class PersistentShell {
5656
): Promise<{ output: string; exitCode: string }> {
5757
this.stdoutBuffer = ''
5858
this.stderrBuffer = ''
59-
let exitCode = 0
6059

6160
return new Promise((resolve, reject) => {
6261
abortSignal?.throwIfAborted()
@@ -68,11 +67,11 @@ export class PersistentShell {
6867

6968
// Add exit code capture
7069
const getExitCodeCommand = process.platform === 'win32' ? 'echo %errorlevel%' : 'echo $?'
71-
this.shell.stdin?.write(`${command}` + '\n')
72-
this.shell.stdin?.write(`${getExitCodeCommand}\n`)
70+
this.shell.stdin?.write(`${command}` + os.EOL)
71+
this.shell.stdin?.write(`${getExitCodeCommand}` + os.EOL)
7372

7473
const endMarker = `__END_OF_COMMAND_${Date.now()}__`
75-
this.shell.stdin?.write(`echo "${endMarker}"\n`)
74+
this.shell.stdin?.write(`echo "${endMarker}"` + os.EOL)
7675

7776
const timeout = 30000
7877

@@ -85,37 +84,38 @@ export class PersistentShell {
8584
const abortListener = () => {
8685
clearTimeout(timeoutId)
8786
this.dispose()
88-
reject(new Error('Command execution aborted'))
8987
abortSignal?.removeEventListener('abort', abortListener)
88+
reject(new Error('Command execution aborted'))
9089
}
9190
abortSignal?.addEventListener('abort', abortListener)
9291

9392
const checkBuffer = () => {
9493
if (this.stdoutBuffer.includes(endMarker)) {
95-
//const sliceStart = process.platform === 'win32' ? 1 : 0
94+
const sliceStart = process.platform === 'win32' ? 1 : 0
95+
const sliceEnd = process.platform === 'win32' ? -4 : -2
96+
9697
clearTimeout(timeoutId)
9798
const outputParts = this.stdoutBuffer
9899
.split(`echo "${endMarker}"`)[0]
99100
.trim()
100101
.split('\n')
101-
102102
// Extract exit code from the last line
103-
exitCode = Number.parseInt(outputParts[outputParts.length - 2], 10)
104-
103+
const exitCode = Number.parseInt(
104+
outputParts[outputParts.length - (2 + sliceStart)],
105+
10
106+
)
105107
// Remove exit code line from output
106108
const output = outputParts
107-
.slice(0, -2)
109+
//.slice(0, -2)
108110
.filter(chunk => {
109111
if (chunk.includes('(c) Microsoft Corporation.')) return false
110112
if (chunk.includes('Microsoft Windows')) return false
111113
if (chunk.match(/^[A-Za-z]:\\.*>.*$/)) return false
112114
return true
113115
})
114-
.join('\n')
115-
116+
.slice(sliceStart, sliceEnd)
117+
.join(os.EOL)
116118
abortSignal?.removeEventListener('abort', abortListener)
117-
118-
// Return stderr if command failed, stdout otherwise
119119
resolve({
120120
output: exitCode !== 0 ? this.stderrBuffer : output,
121121
exitCode: `${exitCode}`,

0 commit comments

Comments
 (0)