Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 9e56e79

Browse files
committed
Enforce coder-cli in PATH on Windows
1 parent dcfd501 commit 9e56e79

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

internal/cmd/configssh.go

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import (
66
"io/ioutil"
77
"net/url"
88
"os"
9+
"os/exec"
910
"os/user"
1011
"path/filepath"
12+
"runtime"
1113
"sort"
1214
"strings"
1315

@@ -132,8 +134,11 @@ func configSSH(configpath *string, remove *bool, next *bool) func(cmd *cobra.Com
132134
}
133135
}
134136

135-
binPath, err := os.Executable()
137+
binPath, err := binPath()
136138
if err != nil {
139+
if runtime.GOOS == "windows" {
140+
return xerrors.Errorf("Failed to ensure `coder` is in $PATH, please move the `coder` binary to a location in $PATH (such as System32): %w", err)
141+
}
137142
return xerrors.Errorf("Failed to get executable path: %w", err)
138143
}
139144

@@ -165,6 +170,42 @@ func configSSH(configpath *string, remove *bool, next *bool) func(cmd *cobra.Com
165170
}
166171
}
167172

173+
// binPath returns the path to the coder binary suitable for use in ssh
174+
// ProxyCommand.
175+
func binPath() (string, error) {
176+
exePath, err := os.Executable()
177+
if err != nil {
178+
return "", xerrors.Errorf("get executable path: %w", err)
179+
}
180+
181+
// On Windows, the coder-cli executable must be in $PATH for Msys2 and Git
182+
// Bash to function correctly. To prevent weird behavior when people switch
183+
// between the two, we require this for all users.
184+
if runtime.GOOS == "windows" {
185+
binName := filepath.Base(exePath)
186+
pathPath, err := exec.LookPath(exePath)
187+
if err != nil {
188+
return "", xerrors.Errorf("locate %q in $PATH: %w", binName, err)
189+
}
190+
191+
// Warn the user if the current executable is not the same as the one in
192+
// $PATH.
193+
if filepath.Clean(pathPath) != filepath.Clean(exePath) {
194+
clog.LogWarn(
195+
"The current executable path does not match the executable path found in $PATH.",
196+
"This may lead to problems connecting to your workspace via SSH.",
197+
fmt.Sprintf("\t Current executable path: %q", exePath),
198+
fmt.Sprintf("\tExecutable path in $PATH: %q", pathPath),
199+
)
200+
}
201+
202+
return binName, nil
203+
}
204+
205+
// On platforms other than Windows we can use the full path to the binary.
206+
return exePath, nil
207+
}
208+
168209
// removeOldConfig removes the old ssh configuration from the user's sshconfig.
169210
// Returns true if the config was modified.
170211
func removeOldConfig(config string) (string, bool) {
@@ -230,7 +271,7 @@ func makeSSHConfig(binPath, host, userName, workspaceName, privateKeyFilepath st
230271
return fmt.Sprintf(
231272
`Host coder.%s
232273
HostName coder.%s
233-
ProxyCommand %s tunnel %s 12213 stdio
274+
ProxyCommand "%s" tunnel %s 12213 stdio
234275
StrictHostKeyChecking no
235276
ConnectTimeout=0
236277
IdentitiesOnly yes

0 commit comments

Comments
 (0)