From 54d924b0a8d8c09717d893e3230dc8913d56b01f Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Fri, 20 Feb 2026 15:03:14 -0300 Subject: [PATCH] fix: more reliably detect windows drive (#2273) --- internal/agent/tools/bash.go | 9 ++------- internal/fsext/drive_other.go | 16 ++++++++++++++++ internal/fsext/drive_windows.go | 26 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 internal/fsext/drive_other.go create mode 100644 internal/fsext/drive_windows.go diff --git a/internal/agent/tools/bash.go b/internal/agent/tools/bash.go index ca3612e091f23235688a2a40006469e39093d6a5..8552880b99b7735172ed89c9b6be147c104dcb5d 100644 --- a/internal/agent/tools/bash.go +++ b/internal/agent/tools/bash.go @@ -7,7 +7,6 @@ import ( _ "embed" "fmt" "html/template" - "os" "path/filepath" "runtime" "strings" @@ -15,6 +14,7 @@ import ( "charm.land/fantasy" "github.com/charmbracelet/crush/internal/config" + "github.com/charmbracelet/crush/internal/fsext" "github.com/charmbracelet/crush/internal/permission" "github.com/charmbracelet/crush/internal/shell" ) @@ -431,12 +431,7 @@ func countLines(s string) int { func normalizeWorkingDir(path string) string { if runtime.GOOS == "windows" { - cwd, err := os.Getwd() - if err != nil { - cwd = "C:" - } - path = strings.ReplaceAll(path, filepath.VolumeName(cwd), "") + path = strings.ReplaceAll(path, fsext.WindowsWorkingDirDrive(), "") } - return filepath.ToSlash(path) } diff --git a/internal/fsext/drive_other.go b/internal/fsext/drive_other.go new file mode 100644 index 0000000000000000000000000000000000000000..0141c8baf0a640a6c3fc45500c9acb798664b16a --- /dev/null +++ b/internal/fsext/drive_other.go @@ -0,0 +1,16 @@ +//go:build !windows + +package fsext + +// WindowsWorkingDirDrive returns the drive letter of the current working +// directory, e.g. "C:". +// Falls back to the system drive if the current working directory cannot be +// determined. +func WindowsWorkingDirDrive() string { + panic("cannot call fsext.WindowsWorkingDirDrive() on non-Windows OS") +} + +// WindowsSystemDrive returns the drive letter of the system drive, e.g. "C:". +func WindowsSystemDrive() string { + panic("cannot call fsext.WindowsSystemDrive() on non-Windows OS") +} diff --git a/internal/fsext/drive_windows.go b/internal/fsext/drive_windows.go new file mode 100644 index 0000000000000000000000000000000000000000..7d0a798da27fb59d38bce5e5bec52560ff18b54b --- /dev/null +++ b/internal/fsext/drive_windows.go @@ -0,0 +1,26 @@ +//go:build windows + +package fsext + +import ( + "cmp" + "os" + "path/filepath" +) + +// WindowsWorkingDirDrive returns the drive letter of the current working +// directory, e.g. "C:". +// Falls back to the system drive if the current working directory cannot be +// determined. +func WindowsWorkingDirDrive() string { + if cwd, err := os.Getwd(); err == nil { + return filepath.VolumeName(cwd) + } + return WindowsSystemDrive() +} + +// WindowsSystemDrive returns the drive letter of the system drive, e.g. "C:". +func WindowsSystemDrive() string { + systemRoot := cmp.Or(os.Getenv("SYSTEMROOT"), os.Getenv("WINDIR")) + return filepath.VolumeName(systemRoot) +}