on main help sign in
fix: more reliably detect windows drive (#2273)
54d924bAndrey Nering
internal/agent/tools/bash.go+2−7
@@ -7,7 +7,6 @@ import (
7 _ "embed"
7 _ "embed"
8 "fmt"
8 "fmt"
9 "html/template"
9 "html/template"
10− "os"
11 "path/filepath"
10 "path/filepath"
12 "runtime"
11 "runtime"
13 "strings"
12 "strings"
@@ -15,6 +14,7 @@ import (
15
14
16 "charm.land/fantasy"
15 "charm.land/fantasy"
17 "github.com/charmbracelet/crush/internal/config"
16 "github.com/charmbracelet/crush/internal/config"
17+ "github.com/charmbracelet/crush/internal/fsext"
18 "github.com/charmbracelet/crush/internal/permission"
18 "github.com/charmbracelet/crush/internal/permission"
19 "github.com/charmbracelet/crush/internal/shell"
19 "github.com/charmbracelet/crush/internal/shell"
20)
20)
@@ -431,12 +431,7 @@ func countLines(s string) int {
431
431
432func normalizeWorkingDir(path string) string {
432func normalizeWorkingDir(path string) string {
433 if runtime.GOOS == "windows" {
433 if runtime.GOOS == "windows" {
434− cwd, err := os.Getwd()
435− if err != nil {
436− cwd = "C:"
437− }
438− path = strings.ReplaceAll(path, filepath.VolumeName(cwd), "")
434+ path = strings.ReplaceAll(path, fsext.WindowsWorkingDirDrive(), "")
439 }
435 }
440−
441 return filepath.ToSlash(path)
436 return filepath.ToSlash(path)
442}
437}
internal/fsext/drive_other.gonew file+16
@@ -0,0 +1,16 @@
1+//go:build !windows
2+
3+package fsext
4+
5+// WindowsWorkingDirDrive returns the drive letter of the current working
6+// directory, e.g. "C:".
7+// Falls back to the system drive if the current working directory cannot be
8+// determined.
9+func WindowsWorkingDirDrive() string {
10+ panic("cannot call fsext.WindowsWorkingDirDrive() on non-Windows OS")
11+}
12+
13+// WindowsSystemDrive returns the drive letter of the system drive, e.g. "C:".
14+func WindowsSystemDrive() string {
15+ panic("cannot call fsext.WindowsSystemDrive() on non-Windows OS")
16+}
internal/fsext/drive_windows.gonew file+26
@@ -0,0 +1,26 @@
1+//go:build windows
2+
3+package fsext
4+
5+import (
6+ "cmp"
7+ "os"
8+ "path/filepath"
9+)
10+
11+// WindowsWorkingDirDrive returns the drive letter of the current working
12+// directory, e.g. "C:".
13+// Falls back to the system drive if the current working directory cannot be
14+// determined.
15+func WindowsWorkingDirDrive() string {
16+ if cwd, err := os.Getwd(); err == nil {
17+ return filepath.VolumeName(cwd)
18+ }
19+ return WindowsSystemDrive()
20+}
21+
22+// WindowsSystemDrive returns the drive letter of the system drive, e.g. "C:".
23+func WindowsSystemDrive() string {
24+ systemRoot := cmp.Or(os.Getenv("SYSTEMROOT"), os.Getenv("WINDIR"))
25+ return filepath.VolumeName(systemRoot)
26+}