Skip to content

Commit 361ca9f

Browse files
authored
fix: resolve ~ in workspace path (#594)
Signed-off-by: Grant Linville <[email protected]>
1 parent a42c9e3 commit 361ca9f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pkg/gptscript/gptscript.go

+15-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"os/user"
78
"path/filepath"
89
"slices"
10+
"strings"
911

1012
"github.com/gptscript-ai/gptscript/pkg/builtin"
1113
"github.com/gptscript-ai/gptscript/pkg/cache"
@@ -167,7 +169,7 @@ func (g *GPTScript) getEnv(env []string) ([]string, error) {
167169
}
168170
} else if !filepath.IsAbs(g.WorkspacePath) {
169171
var err error
170-
g.WorkspacePath, err = filepath.Abs(g.WorkspacePath)
172+
g.WorkspacePath, err = makeAbsolute(g.WorkspacePath)
171173
if err != nil {
172174
return nil, err
173175
}
@@ -181,6 +183,18 @@ func (g *GPTScript) getEnv(env []string) ([]string, error) {
181183
}), nil
182184
}
183185

186+
func makeAbsolute(path string) (string, error) {
187+
if strings.HasPrefix(path, "~"+string(filepath.Separator)) {
188+
usr, err := user.Current()
189+
if err != nil {
190+
return "", err
191+
}
192+
193+
return filepath.Join(usr.HomeDir, path[2:]), nil
194+
}
195+
return filepath.Abs(path)
196+
}
197+
184198
func (g *GPTScript) Chat(ctx context.Context, prevState runner.ChatState, prg types.Program, envs []string, input string) (runner.ChatResponse, error) {
185199
envs, err := g.getEnv(envs)
186200
if err != nil {

0 commit comments

Comments
 (0)