Skip to content

Commit 3973dc7

Browse files
committed
Tidy up remove env assignments impl
1 parent 6e2ba5d commit 3973dc7

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/commandLineAutoApprover.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,35 +100,32 @@ export class CommandLineAutoApprover extends Disposable {
100100
return { result: 'noMatch', reason: `Command line '${commandLine}' has no matching auto approve entries` };
101101
}
102102

103-
private _extractCommandFromEnvAssignments(command: string, shell: string, os: OperatingSystem): string {
104-
// Trim leading/trailing whitespace
105-
const trimmedCommand = command.trim();
106-
103+
private _removeEnvAssignments(command: string, shell: string, os: OperatingSystem): string {
104+
const trimmedCommand = command.trimStart();
105+
106+
// PowerShell environment variable syntax is `$env:VAR='value';` and treated as a different
107+
// command
107108
if (isPowerShell(shell, os)) {
108-
// PowerShell environment variable syntax: $env:VAR='value'; command
109-
// We'll keep the current behavior as PowerShell has different patterns
110109
return trimmedCommand;
111110
}
112-
111+
113112
// For bash/sh/bourne shell and unknown shells (fallback to bourne shell syntax)
114113
// Handle environment variable assignments like: VAR=value VAR2=value command
115114
// This regex matches one or more environment variable assignments at the start
116115
const envVarPattern = /^(\s*[A-Za-z_][A-Za-z0-9_]*=(?:[^\s'"]|'[^']*'|"[^"]*")*\s+)+/;
117116
const match = trimmedCommand.match(envVarPattern);
118-
117+
119118
if (match) {
120-
// Remove the environment variable assignments from the beginning
121-
const actualCommand = trimmedCommand.slice(match[0].length).trim();
119+
const actualCommand = trimmedCommand.slice(match[0].length).trimStart();
122120
return actualCommand || trimmedCommand; // Fallback to original if nothing left
123121
}
124-
122+
125123
return trimmedCommand;
126124
}
127125

128126
private _commandMatchesRegex(regex: RegExp, command: string, shell: string, os: OperatingSystem): boolean {
129-
// First extract the actual command from any environment variable assignments
130-
const actualCommand = this._extractCommandFromEnvAssignments(command, shell, os);
131-
127+
const actualCommand = this._removeEnvAssignments(command, shell, os);
128+
132129
if (regex.test(actualCommand)) {
133130
return true;
134131
} else if (isPowerShell(shell, os) && actualCommand.startsWith('(')) {

0 commit comments

Comments
 (0)