@@ -18,6 +18,7 @@ import (
18
18
"os/user"
19
19
"path/filepath"
20
20
"reflect"
21
+ "regexp"
21
22
"runtime"
22
23
"sort"
23
24
"strconv"
@@ -205,9 +206,15 @@ func (a *agent) runLoop(ctx context.Context) {
205
206
}
206
207
}
207
208
209
+ var isPowershellRe = regexp .MustCompile (`^(powershell|pwsh)(\.exe)?$` )
210
+
208
211
func createMetadataCommand (ctx context.Context , script string ) (* exec.Cmd , error ) {
209
212
// This is largely copied from agentssh, but for some reason the command
210
- // generated there always returns exit status 1 in Windows.
213
+ // generated there always returns exit status 1 in Windows powershell.
214
+ //
215
+ // This function puts special PowerShell branching in place that fixes the issue,
216
+ // but I'm hesitant on porting it to agentssh before understanding exactly what's
217
+ // happening.
211
218
currentUser , err := user .Current ()
212
219
if err != nil {
213
220
return nil , xerrors .Errorf ("get current user: %w" , err )
@@ -219,19 +226,21 @@ func createMetadataCommand(ctx context.Context, script string) (*exec.Cmd, error
219
226
return nil , xerrors .Errorf ("get user shell: %w" , err )
220
227
}
221
228
222
- var caller string
229
+ var args [] string
223
230
switch {
224
- case filepath .Base (shell ) == "pwsh.exe" :
225
- caller = "-Command"
226
231
case runtime .GOOS == "windows" :
227
- caller = "/c"
232
+ if isPowershellRe .MatchString (filepath .Base (shell )) {
233
+ args = append (args , "-NoProfile" , "-NonInteractive" , "-Command" )
234
+ } else {
235
+ // Probably cmd.exe
236
+ args = append (args , "/c" )
237
+ }
238
+
228
239
default :
229
- caller = "-c"
240
+ args = append ( args , "-c" )
230
241
}
231
- // args := []string{caller, "Get-Process"}
232
- args := []string {"-NoProfile" , "-NonInteractive" }
233
- _ = caller
234
- return exec .CommandContext (ctx , "powershell" , args ... ), nil
242
+ args = append (args , script )
243
+ return exec .CommandContext (ctx , shell , args ... ), nil
235
244
}
236
245
237
246
func (* agent ) collectMetadata (ctx context.Context , md codersdk.WorkspaceAgentMetadataDescription ) * codersdk.WorkspaceAgentMetadataResult {
@@ -251,14 +260,6 @@ func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMet
251
260
return result
252
261
}
253
262
254
- // execPath, err := exec.LookPath("cmd.exe")
255
- // if err != nil {
256
- // result.Error = fmt.Sprintf("look path: %+v", err)
257
- // return result
258
- // }
259
-
260
- // cmd = exec.CommandContext(ctx, execPath, "/c", "echo hello")
261
-
262
263
cmd .Stdout = & out
263
264
cmd .Stderr = & out
264
265
cmd .Stdin = io .LimitReader (nil , 0 )
@@ -281,8 +282,6 @@ func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMet
281
282
out .Truncate (bufLimit )
282
283
}
283
284
284
- fmt .Printf ("ran %+v %+q: %v\n " , cmd .Path , cmd .Args , err )
285
-
286
285
if err != nil {
287
286
result .Error = fmt .Sprintf ("run cmd: %+v" , err )
288
287
}
0 commit comments