on main help sign in
fix: small improvements around command execution
commit1ad34859239311c5b9d0cd7f193cfae9d230b099 authorCarlos Alexandro Becker <caarlos0@users.noreply.github.com> dateFri 20 Jun 2025, 10:07 (1 year ago) parenta4a9297
4 files changed+25−29
internal/fsext/fileutil.go+15−5 internal/llm/tools/glob.go+1−1 internal/llm/tools/grep.go+3−11 internal/llm/tools/shell/shell.go+6−12
internal/fsext/fileutil.go+15−5
@@ -25,12 +25,10 @@ func init() {
2525 rgPath, err = exec.LookPath("rg")
2626 if err != nil {
2727 logging.Warn("Ripgrep (rg) not found in $PATH. Some features might be limited or slower.")
28− rgPath = ""
2928 }
3029 fzfPath, err = exec.LookPath("fzf")
3130 if err != nil {
3231 logging.Warn("FZF not found in $PATH. Some features might be limited or slower.")
33− fzfPath = ""
3432 }
3533}
3634
@@ -49,9 +47,21 @@ func GetRgCmd(globPattern string) *exec.Cmd {
4947 }
5048 rgArgs = append(rgArgs, "--glob", globPattern)
5149 }
52− cmd := exec.Command(rgPath, rgArgs...)
53− cmd.Dir = "."
54− return cmd
50+ return exec.Command(rgPath, rgArgs...)
51+}
52+
53+func GetRgSearchCmd(pattern, path, include string) *exec.Cmd {
54+ if rgPath == "" {
55+ return nil
56+ }
57+ // Use -n to show line numbers and include the matched line
58+ args := []string{"-n", pattern}
59+ if include != "" {
60+ args = append(args, "--glob", include)
61+ }
62+ args = append(args, path)
63+
64+ return exec.Command("rg", args...)
5565}
5666
5767type FileInfo struct {
internal/llm/tools/glob.go+1−1
@@ -150,7 +150,7 @@ func runRipgrep(cmd *exec.Cmd, searchRoot string, limit int) ([]string, error) {
150150 }
151151
152152 var matches []string
153− for _, p := range bytes.Split(out, []byte{0}) {
153+ for p := range bytes.SplitSeq(out, []byte{0}) {
154154 if len(p) == 0 {
155155 continue
156156 }
internal/llm/tools/grep.go+3−11
@@ -257,19 +257,11 @@ func searchFiles(pattern, rootPath, include string, limit int) ([]grepMatch, boo
257257}
258258
259259func searchWithRipgrep(pattern, path, include string) ([]grepMatch, error) {
260− _, err := exec.LookPath("rg")
261− if err != nil {
262− return nil, fmt.Errorf("ripgrep not found: %w", err)
263− }
264−
265− // Use -n to show line numbers and include the matched line
266− args := []string{"-n", pattern}
267− if include != "" {
268− args = append(args, "--glob", include)
260+ cmd := fsext.GetRgSearchCmd(pattern, path, include)
261+ if cmd == nil {
262+ return nil, fmt.Errorf("ripgrep not found in $PATH")
269263 }
270− args = append(args, path)
271264
272− cmd := exec.Command("rg", args...)
273265 output, err := cmd.Output()
274266 if err != nil {
275267 if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
internal/llm/tools/shell/shell.go+6−12
@@ -4,6 +4,7 @@ import (
44 "context"
55 "errors"
66 "fmt"
7+ "io"
78 "os"
89 "os/exec"
910 "path/filepath"
@@ -17,7 +18,7 @@ import (
1718
1819type PersistentShell struct {
1920 cmd *exec.Cmd
20− stdin *os.File
21+ stdin io.WriteCloser
2122 isAlive bool
2223 cwd string
2324 mu sync.Mutex
@@ -39,22 +40,15 @@ type commandResult struct {
3940 err error
4041}
4142
42−var (
43− shellInstance *PersistentShell
44− shellInstanceOnce sync.Once
45−)
43+var shellInstance *PersistentShell
4644
4745func GetPersistentShell(workingDir string) *PersistentShell {
48− shellInstanceOnce.Do(func() {
49− shellInstance = newPersistentShell(workingDir)
50− })
51−
5246 if shellInstance == nil {
5347 shellInstance = newPersistentShell(workingDir)
54− } else if !shellInstance.isAlive {
48+ }
49+ if !shellInstance.isAlive {
5550 shellInstance = newPersistentShell(shellInstance.cwd)
5651 }
57−
5852 return shellInstance
5953}
6054
@@ -100,7 +94,7 @@ func newPersistentShell(cwd string) *PersistentShell {
10094
10195 shell := &PersistentShell{
10296 cmd: cmd,
103− stdin: stdinPipe.(*os.File),
97+ stdin: stdinPipe,
10498 isAlive: true,
10599 cwd: cwd,
106100 commandQueue: make(chan *commandExecution, 10),