Pipeline runs use the workspace's directory environment

Resolve the run workspace's environment with workenv (direnv, parent
.envrc, mise; same account rule as Claude chats) at run start and resume,
and apply it to Pi stage processes, a per-environment Pipelines-owned
OpenCode stage service (keyed by a hash of the change, reused across runs,
idle ones stopped after PIPELINES_OPENCODE_STAGE_IDLE_TIMEOUT, revived per
registration), delivery-gate checks and merges, plain-stage opencode CLI
calls and file-change capture git reads. Record the directory, .envrc
files, sources and variable names (never values) on the run detail; a
blocked .envrc becomes a run warning.
This commit is contained in:
2026-11-04 18:08:42 +00:00
parent e0d7b720f5
commit cedfc22e45
26 changed files with 1173 additions and 84 deletions
+19 -6
View File
@@ -21,6 +21,7 @@ import (
"os/exec"
"path/filepath"
"regexp"
"slices"
"sort"
"strings"
"sync"
@@ -37,6 +38,8 @@ type Result struct {
Changes map[string]*string
// Sources names what contributed, for example "direnv" or "mise".
Sources []string
// Files are the .envrc (or dotenv) files direnv loaded, nearest first.
Files []string
// Warnings are operator-facing notices: a blocked .envrc, an untrusted
// mise config, or a tool that failed. Nothing from a warned source is applied.
Warnings []string
@@ -154,7 +157,7 @@ func (r *Resolver) Resolve(ctx context.Context, directory string, base []string)
}
}
if direnv != "" {
changes, watched, warning := r.runDirenv(ctx, direnv, directory, clean)
changes, watched, files, warning := r.runDirenv(ctx, direnv, directory, clean)
watches = watched
if warning != "" {
result.Warnings = append(result.Warnings, warning)
@@ -163,6 +166,7 @@ func (r *Resolver) Resolve(ctx context.Context, directory string, base []string)
result.Changes[key] = value
}
result.Sources = append(result.Sources, "direnv")
result.Files = files
}
}
// A cancelled lookup is not cached; the next start tries again.
@@ -233,30 +237,39 @@ func (r *Resolver) run(ctx context.Context, binary, directory string, env []stri
var blockedRC = regexp.MustCompile(`(\S+) is blocked`)
func (r *Resolver) runDirenv(ctx context.Context, binary, directory string, env []string) (map[string]*string, []string, string) {
func (r *Resolver) runDirenv(ctx context.Context, binary, directory string, env []string) (map[string]*string, []string, []string, string) {
stdout, stderr, err := r.run(ctx, binary, directory, env, "export", "json")
if match := blockedRC.FindStringSubmatch(stderr); match != nil {
return nil, nil, fmt.Sprintf("direnv: %s is not allowed, so its environment was not applied. Run `direnv allow` in %s to use it.", match[1], filepath.Dir(match[1]))
return nil, nil, nil, fmt.Sprintf("direnv: %s is not allowed, so its environment was not applied. Run `direnv allow` in %s to use it.", match[1], filepath.Dir(match[1]))
}
if err != nil {
return nil, nil, "direnv export failed in " + directory + ", so its environment was not applied: " + firstLine(stderr, err)
return nil, nil, nil, "direnv export failed in " + directory + ", so its environment was not applied: " + firstLine(stderr, err)
}
changes := map[string]*string{}
if text := bytes.TrimSpace(stdout); len(text) > 0 {
if err := json.Unmarshal(text, &changes); err != nil {
return nil, nil, "direnv export returned invalid output in " + directory + "; its environment was not applied"
return nil, nil, nil, "direnv export returned invalid output in " + directory + "; its environment was not applied"
}
}
var watches []string
if encoded := changes["DIRENV_WATCHES"]; encoded != nil {
watches = decodeWatches(*encoded)
}
var files []string
if file := changes["DIRENV_FILE"]; file != nil && *file != "" {
files = append(files, *file)
}
for _, path := range watches {
if name := filepath.Base(path); (name == ".envrc" || name == ".env") && !slices.Contains(files, path) {
files = append(files, path)
}
}
for key := range changes {
if strings.HasPrefix(key, "DIRENV_") {
delete(changes, key)
}
}
return changes, watches, ""
return changes, watches, files, ""
}
func (r *Resolver) runMise(ctx context.Context, binary, directory string, env []string) (map[string]*string, string) {