Skip to content

Commit 77d1d9e

Browse files
committed
Works on my Windows desktop?
1 parent 56a45cc commit 77d1d9e

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

agent/agent.go

+19-20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"os/user"
1919
"path/filepath"
2020
"reflect"
21+
"regexp"
2122
"runtime"
2223
"sort"
2324
"strconv"
@@ -205,9 +206,15 @@ func (a *agent) runLoop(ctx context.Context) {
205206
}
206207
}
207208

209+
var isPowershellRe = regexp.MustCompile(`^(powershell|pwsh)(\.exe)?$`)
210+
208211
func createMetadataCommand(ctx context.Context, script string) (*exec.Cmd, error) {
209212
// 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.
211218
currentUser, err := user.Current()
212219
if err != nil {
213220
return nil, xerrors.Errorf("get current user: %w", err)
@@ -219,19 +226,21 @@ func createMetadataCommand(ctx context.Context, script string) (*exec.Cmd, error
219226
return nil, xerrors.Errorf("get user shell: %w", err)
220227
}
221228

222-
var caller string
229+
var args []string
223230
switch {
224-
case filepath.Base(shell) == "pwsh.exe":
225-
caller = "-Command"
226231
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+
228239
default:
229-
caller = "-c"
240+
args = append(args, "-c")
230241
}
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
235244
}
236245

237246
func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMetadataDescription) *codersdk.WorkspaceAgentMetadataResult {
@@ -251,14 +260,6 @@ func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMet
251260
return result
252261
}
253262

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-
262263
cmd.Stdout = &out
263264
cmd.Stderr = &out
264265
cmd.Stdin = io.LimitReader(nil, 0)
@@ -281,8 +282,6 @@ func (*agent) collectMetadata(ctx context.Context, md codersdk.WorkspaceAgentMet
281282
out.Truncate(bufLimit)
282283
}
283284

284-
fmt.Printf("ran %+v %+q: %v\n", cmd.Path, cmd.Args, err)
285-
286285
if err != nil {
287286
result.Error = fmt.Sprintf("run cmd: %+v", err)
288287
}

agent/agent_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ func TestAgent_Metadata(t *testing.T) {
10311031
}, testutil.WaitShort, testutil.IntervalMedium)
10321032

10331033
collectedAt1 := gotMd["greeting"].CollectedAt
1034-
if !assert.Equal(t, "hello\n", gotMd["greeting"].Value) {
1034+
if !assert.Equal(t, "hello", strings.TrimSpace(gotMd["greeting"].Value)) {
10351035
t.Logf("got: %+v", gotMd)
10361036
}
10371037

0 commit comments

Comments
 (0)