@@ -8,11 +8,13 @@ import (
88 "io"
99 "os"
1010 "os/exec"
11+ "runtime"
1112 "sort"
1213 "strings"
1314 "sync/atomic"
1415
1516 "github.com/google/shlex"
17+ "github.com/gptscript-ai/gptscript/pkg/env"
1618 "github.com/gptscript-ai/gptscript/pkg/types"
1719 "github.com/gptscript-ai/gptscript/pkg/version"
1820)
@@ -103,6 +105,7 @@ func envAsMapAndDeDup(env []string) (sortedEnv []string, _ map[string]string) {
103105
104106var ignoreENV = map [string ]struct {}{
105107 "PATH" : {},
108+ "Path" : {},
106109 "GPTSCRIPT_TOOL_DIR" : {},
107110}
108111
@@ -146,8 +149,8 @@ func appendInputAsEnv(env []string, input string) []string {
146149}
147150
148151func (e * Engine ) newCommand (ctx context.Context , extraEnv []string , tool types.Tool , input string ) (* exec.Cmd , func (), error ) {
149- env := append (e .Env [:], extraEnv ... )
150- env = appendInputAsEnv (env , input )
152+ envvars := append (e .Env [:], extraEnv ... )
153+ envvars = appendInputAsEnv (envvars , input )
151154
152155 interpreter , rest , _ := strings .Cut (tool .Instructions , "\n " )
153156 interpreter = strings .TrimSpace (interpreter )[2 :]
@@ -157,18 +160,22 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
157160 return nil , nil , err
158161 }
159162
160- env , err = e .getRuntimeEnv (ctx , tool , args , env )
163+ envvars , err = e .getRuntimeEnv (ctx , tool , args , envvars )
161164 if err != nil {
162165 return nil , nil , err
163166 }
164167
165- env , envMap := envAsMapAndDeDup (env )
168+ envvars , envMap := envAsMapAndDeDup (envvars )
166169 for i , arg := range args {
167170 args [i ] = os .Expand (arg , func (s string ) string {
168171 return envMap [s ]
169172 })
170173 }
171174
175+ if runtime .GOOS == "windows" && (args [0 ] == "/usr/bin/env" || args [0 ] == "/bin/env" ) {
176+ args = args [1 :]
177+ }
178+
172179 var (
173180 cmdArgs = args [1 :]
174181 stop = func () {}
@@ -192,7 +199,7 @@ func (e *Engine) newCommand(ctx context.Context, extraEnv []string, tool types.T
192199 cmdArgs = append (cmdArgs , f .Name ())
193200 }
194201
195- cmd := exec .CommandContext (ctx , args [0 ], cmdArgs ... )
196- cmd .Env = env
202+ cmd := exec .CommandContext (ctx , env . Lookup ( envvars , args [0 ]) , cmdArgs ... )
203+ cmd .Env = envvars
197204 return cmd , stop , nil
198205}
0 commit comments