Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4543,7 +4543,12 @@ func addToWindowsUserPATH(cmd *cli.Command, binaryDir string) error {
out, err := exec.Command("powershell", "-Command",
`[Environment]::GetEnvironmentVariable("Path", "User")`).Output()
if err != nil {
return errors.Errorf("failed to read user PATH: %v", err)
// Surface PowerShell's stderr (stashed on the ExitError) instead of just "exit status 1".
var exitErr *exec.ExitError
if errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {
return errors.Errorf("failed to read user PATH:\n%s", strings.TrimSpace(string(exitErr.Stderr)))
}
return errors.Wrap(err, "failed to read user PATH")
}
currentPath := strings.TrimSpace(string(out))

Expand All @@ -4562,11 +4567,15 @@ func addToWindowsUserPATH(cmd *cli.Command, binaryDir string) error {
newPath += cleanDir

//nolint:gosec
if err := exec.Command("powershell", "-Command",
setCmd := exec.Command("powershell", "-Command",
fmt.Sprintf(`[Environment]::SetEnvironmentVariable("Path", "%s", "User")`,
strings.ReplaceAll(newPath, `"`, `\"`)),
).Run(); err != nil {
return errors.Errorf("failed to update user PATH: %v", err)
strings.ReplaceAll(newPath, `"`, `\"`)))
// CombinedOutput (not Run) so PowerShell's error is surfaced instead of just "exit status 1".
if out, err := setCmd.CombinedOutput(); err != nil {
if trimmed := strings.TrimSpace(string(out)); trimmed != "" {
return errors.Errorf("failed to update user PATH:\n%s", trimmed)
}
return errors.Wrap(err, "failed to update user PATH")
}

infof(cmd.Root().Writer, "Added %s to your user PATH. Restart your terminal to use 'viam' from anywhere.", cleanDir)
Expand Down
Loading