Skip to content

Commit c8936c6

Browse files
Merge pull request #554 from cloudnautique/add_envfile_support
enhance: add support for gptscript.env file in workspace
2 parents 7c6f5c3 + f1c0282 commit c8936c6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pkg/builtin/builtin.go

+42
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ func SysExec(_ context.Context, env []string, input string, progress chan<- stri
336336
}
337337
combined = io.MultiWriter(&out, &pw)
338338
)
339+
340+
if envvars, err := getWorkspaceEnvFileContents(env); err == nil {
341+
env = append(env, envvars...)
342+
}
343+
339344
cmd.Env = env
340345
cmd.Dir = params.Directory
341346
cmd.Stdout = combined
@@ -355,6 +360,43 @@ func (pw *progressWriter) Write(p []byte) (n int, err error) {
355360
return len(p), nil
356361
}
357362

363+
func getWorkspaceEnvFileContents(envs []string) ([]string, error) {
364+
dir, err := getWorkspaceDir(envs)
365+
if err != nil {
366+
return nil, err
367+
}
368+
369+
file := filepath.Join(dir, "gptscript.env")
370+
371+
// Lock the file to prevent concurrent writes from other tool calls.
372+
locker.RLock(file)
373+
defer locker.RUnlock(file)
374+
375+
// This is optional, so no errors are returned if the file does not exist.
376+
log.Debugf("Reading file %s", file)
377+
data, err := os.ReadFile(file)
378+
if errors.Is(err, fs.ErrNotExist) {
379+
log.Debugf("The file %s does not exist", file)
380+
return []string{}, nil
381+
} else if err != nil {
382+
log.Debugf("Failed to read file %s: %v", file, err.Error())
383+
return []string{}, nil
384+
}
385+
386+
lines := strings.Split(string(data), "\n")
387+
var envContents []string
388+
389+
for _, line := range lines {
390+
line = strings.TrimSpace(line)
391+
if strings.Contains(line, "=") {
392+
envContents = append(envContents, line)
393+
}
394+
}
395+
396+
return envContents, nil
397+
398+
}
399+
358400
func getWorkspaceDir(envs []string) (string, error) {
359401
for _, env := range envs {
360402
dir, ok := strings.CutPrefix(env, "GPTSCRIPT_WORKSPACE_DIR=")

0 commit comments

Comments
 (0)