@@ -100,35 +100,32 @@ export class CommandLineAutoApprover extends Disposable {
100
100
return { result : 'noMatch' , reason : `Command line '${ commandLine } ' has no matching auto approve entries` } ;
101
101
}
102
102
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
107
108
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
110
109
return trimmedCommand ;
111
110
}
112
-
111
+
113
112
// For bash/sh/bourne shell and unknown shells (fallback to bourne shell syntax)
114
113
// Handle environment variable assignments like: VAR=value VAR2=value command
115
114
// This regex matches one or more environment variable assignments at the start
116
115
const envVarPattern = / ^ ( \s * [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * = (?: [ ^ \s ' " ] | ' [ ^ ' ] * ' | " [ ^ " ] * " ) * \s + ) + / ;
117
116
const match = trimmedCommand . match ( envVarPattern ) ;
118
-
117
+
119
118
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 ( ) ;
122
120
return actualCommand || trimmedCommand ; // Fallback to original if nothing left
123
121
}
124
-
122
+
125
123
return trimmedCommand ;
126
124
}
127
125
128
126
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
+
132
129
if ( regex . test ( actualCommand ) ) {
133
130
return true ;
134
131
} else if ( isPowerShell ( shell , os ) && actualCommand . startsWith ( '(' ) ) {
0 commit comments