Skip to content

Commit f6b0615

Browse files
committed
fix: allow GPTSCRIPT_BIN to be absolute, relatative, or relative-to-me
Signed-off-by: Donnie Adams <[email protected]>
1 parent c6ed652 commit f6b0615

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

exec.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313
)
1414

15+
const relativeToBinaryPath = "<me>"
16+
1517
// Opts represents options for the gptscript tool or file.
1618
type Opts struct {
1719
DisableCache bool `json:"disableCache"`
@@ -313,11 +315,11 @@ func concatTools(tools []fmt.Stringer) string {
313315

314316
func getCommand() string {
315317
if gptScriptBin := os.Getenv("GPTSCRIPT_BIN"); gptScriptBin != "" {
316-
if filepath.IsAbs(gptScriptBin) || len(os.Args) == 0 {
318+
if !strings.HasPrefix(gptScriptBin, relativeToBinaryPath) || len(os.Args) == 0 {
317319
return gptScriptBin
318320
}
319321

320-
return filepath.Join(filepath.Dir(os.Args[0]), gptScriptBin)
322+
return filepath.Join(filepath.Dir(os.Args[0]), strings.TrimPrefix(gptScriptBin, relativeToBinaryPath))
321323
}
322324

323325
return "gptscript"

exec_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8+
"path/filepath"
89
"runtime"
910
"strings"
1011
"testing"
@@ -516,3 +517,44 @@ func TestExecWithWorkspace(t *testing.T) {
516517
t.Errorf("Unexpected output: %s", out)
517518
}
518519
}
520+
521+
func TestGetCommand(t *testing.T) {
522+
currentEnvVar := os.Getenv("GPTSCRIPT_BIN")
523+
t.Cleanup(func() {
524+
_ = os.Setenv("GPTSCRIPT_BIN", currentEnvVar)
525+
})
526+
527+
tests := []struct {
528+
name string
529+
envVar string
530+
want string
531+
}{
532+
{
533+
name: "no env var set",
534+
want: "gptscript",
535+
},
536+
{
537+
name: "env var set to absolute path",
538+
envVar: "/usr/local/bin/gptscript",
539+
want: "/usr/local/bin/gptscript",
540+
},
541+
{
542+
name: "env var set to relative path",
543+
envVar: "../bin/gptscript",
544+
want: "../bin/gptscript",
545+
},
546+
{
547+
name: "env var set to relative 'to me' path",
548+
envVar: "<me>/../bin/gptscript",
549+
want: filepath.Join(filepath.Dir(os.Args[0]), "../bin/gptscript"),
550+
},
551+
}
552+
for _, tt := range tests {
553+
t.Run(tt.name, func(t *testing.T) {
554+
_ = os.Setenv("GPTSCRIPT_BIN", tt.envVar)
555+
if got := getCommand(); got != tt.want {
556+
t.Errorf("getCommand() = %v, want %v", got, tt.want)
557+
}
558+
})
559+
}
560+
}

0 commit comments

Comments
 (0)