Skip to content

Commit a9d02ed

Browse files
committed
fix: address edge-case with using <me> with GPTSCRIPT_BIN
Before this change, the following command would not allow running of gptscripts: GPTSCRIPT_BIN="<me>/gptscript" ./click-chats This change address this edge-case. Signed-off-by: Donnie Adams <[email protected]>
1 parent f6b0615 commit a9d02ed

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ go get github.com/gptscript-ai/go-gptscript
1414

1515
To use the module, you need to first set the OPENAI_API_KEY environment variable to your OpenAI API key.
1616

17-
Additionally, you need the `gptscript` binary. You can install it on your system using the [installation instructions](https://github.com/gptscript-ai/gptscript?tab=readme-ov-file#1-install-the-latest-release).
17+
Additionally, you need the `gptscript` binary. You can install it on your system using the [installation instructions](https://github.com/gptscript-ai/gptscript?tab=readme-ov-file#1-install-the-latest-release). The binary can be on the PATH, or the `GPTSCRIPT_BIN` environment variable can be used to specify its location.
1818

1919
## Options
2020

exec.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"log/slog"
910
"os"
1011
"os/exec"
1112
"path/filepath"
@@ -315,16 +316,30 @@ func concatTools(tools []fmt.Stringer) string {
315316

316317
func getCommand() string {
317318
if gptScriptBin := os.Getenv("GPTSCRIPT_BIN"); gptScriptBin != "" {
318-
if !strings.HasPrefix(gptScriptBin, relativeToBinaryPath) || len(os.Args) == 0 {
319+
if len(os.Args) == 0 {
319320
return gptScriptBin
320321
}
321-
322-
return filepath.Join(filepath.Dir(os.Args[0]), strings.TrimPrefix(gptScriptBin, relativeToBinaryPath))
322+
return determineProperCommand(filepath.Dir(os.Args[0]), gptScriptBin)
323323
}
324324

325325
return "gptscript"
326326
}
327327

328+
// determineProperCommand is for testing purposes. Users should use getCommand instead.
329+
func determineProperCommand(dir, bin string) string {
330+
if !strings.HasPrefix(bin, relativeToBinaryPath) {
331+
return bin
332+
}
333+
334+
bin = filepath.Join(dir, strings.TrimPrefix(bin, relativeToBinaryPath))
335+
if !filepath.IsAbs(bin) {
336+
bin = "." + string(os.PathSeparator) + bin
337+
}
338+
339+
slog.Debug("Using gptscript binary: " + bin)
340+
return bin
341+
}
342+
328343
func setupForkCommand(ctx context.Context, input string, args []string) (*exec.Cmd, io.Reader, io.Reader, error) {
329344
if input != "" {
330345
args = append(args, input)

exec_test.go

+38-22
Original file line numberDiff line numberDiff line change
@@ -518,42 +518,58 @@ func TestExecWithWorkspace(t *testing.T) {
518518
}
519519
}
520520

521-
func TestGetCommand(t *testing.T) {
522-
currentEnvVar := os.Getenv("GPTSCRIPT_BIN")
523-
t.Cleanup(func() {
524-
_ = os.Setenv("GPTSCRIPT_BIN", currentEnvVar)
525-
})
526-
521+
func TestDetermineProperCommand(t *testing.T) {
527522
tests := []struct {
528-
name string
529-
envVar string
530-
want string
523+
name string
524+
dir, bin string
525+
want string
531526
}{
532527
{
533-
name: "no env var set",
528+
name: "no dir",
529+
bin: "gptscript",
534530
want: "gptscript",
535531
},
536532
{
537-
name: "env var set to absolute path",
538-
envVar: "/usr/local/bin/gptscript",
539-
want: "/usr/local/bin/gptscript",
533+
name: "bin set to absolute path",
534+
bin: "/usr/local/bin/gptscript",
535+
dir: "/usr/local",
536+
want: "/usr/local/bin/gptscript",
537+
},
538+
{
539+
name: "bin set to relative path",
540+
bin: "../bin/gptscript",
541+
dir: "/usr/local",
542+
want: "../bin/gptscript",
543+
},
544+
{
545+
name: "bin set to relative 'to me' path with os.Args[0]",
546+
bin: "<me>/../bin/gptscript",
547+
dir: filepath.Dir(os.Args[0]),
548+
want: filepath.Join(filepath.Dir(os.Args[0]), "../bin/gptscript"),
549+
},
550+
{
551+
name: "env var set to relative 'to me' path with extra and dir is current",
552+
bin: "<me>/../bin/gptscript",
553+
dir: "./",
554+
want: "./../bin/gptscript",
540555
},
541556
{
542-
name: "env var set to relative path",
543-
envVar: "../bin/gptscript",
544-
want: "../bin/gptscript",
557+
name: "env var set to relative 'to me' path and dir is current",
558+
bin: "<me>/gptscript",
559+
dir: "./",
560+
want: "./gptscript",
545561
},
546562
{
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"),
563+
name: "env var set to relative 'to me' path with extra and dir is current",
564+
bin: "<me>/../bin/gptscript",
565+
dir: "./",
566+
want: "./../bin/gptscript",
550567
},
551568
}
552569
for _, tt := range tests {
553570
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)
571+
if got := determineProperCommand(tt.dir, tt.bin); got != tt.want {
572+
t.Errorf("determineProperCommand() = %v, want %v", got, tt.want)
557573
}
558574
})
559575
}

0 commit comments

Comments
 (0)