// Package workenv computes the environment a shell would have after `cd` into // a directory, so a harness the server starts there behaves as if the // operator had opened a terminal in that directory and started it. // // Two per-directory mechanisms are applied when their tool is installed: // direnv (`direnv export json`, only for an .envrc the operator already // allowed) and mise (`mise env --json`, only for trusted config). A plain // .env file is deliberately not read: direnv decides whether it applies. // Results are cached per directory until a watched file changes. package workenv import ( "bytes" "compress/zlib" "context" "encoding/base64" "encoding/json" "fmt" "io" "os" "os/exec" "path/filepath" "regexp" "slices" "sort" "strings" "sync" "time" ) // DisableEnvKey turns per-directory environments off when set to "off" in the // server's environment. const DisableEnvKey = "PIPELINES_DIRECTORY_ENV" // Result is the change a directory makes to the server's base environment. type Result struct { // Changes maps a variable to its new value; a nil value unsets it. 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 } // Touches reports whether the directory sets or unsets key. func (r Result) Touches(key string) bool { _, ok := r.Changes[key] return ok } // Value returns the directory's value for key; ok is false when it does not // set it (an unset also reports ok false). func (r Result) Value(key string) (string, bool) { value, ok := r.Changes[key] if !ok || value == nil { return "", false } return *value, true } // Apply returns base with the directory's changes merged on top. func (r Result) Apply(base []string) []string { if len(r.Changes) == 0 { return append([]string{}, base...) } out := make([]string, 0, len(base)+len(r.Changes)) for _, item := range base { key, _, _ := strings.Cut(item, "=") if _, changed := r.Changes[key]; changed { continue } out = append(out, item) } keys := make([]string, 0, len(r.Changes)) for key := range r.Changes { keys = append(keys, key) } sort.Strings(keys) for _, key := range keys { if value := r.Changes[key]; value != nil { out = append(out, key+"="+*value) } } return out } // Resolver runs and caches the per-directory tools. type Resolver struct { // SearchDirs are tried after the base environment's PATH when looking for // direnv and mise (a login service often has a short PATH). SearchDirs []string Timeout time.Duration mu sync.Mutex cache map[string]cacheEntry running map[string]*sync.Mutex } type cacheEntry struct { fingerprint string watches []string result Result } // New returns a resolver that also looks in the usual install locations. func New() *Resolver { dirs := []string{"/opt/homebrew/bin", "/usr/local/bin"} if home, err := os.UserHomeDir(); err == nil { dirs = append(dirs, filepath.Join(home, ".local", "bin"), filepath.Join(home, ".nix-profile", "bin")) } return &Resolver{SearchDirs: dirs, Timeout: 30 * time.Second} } // Default is the server-wide resolver. var Default = New() // Resolve returns the environment changes for directory relative to base. // It never fails: problems are reported as warnings and nothing from the // failing source is applied. func (r *Resolver) Resolve(ctx context.Context, directory string, base []string) Result { directory = filepath.Clean(directory) if strings.EqualFold(strings.TrimSpace(lookup(base, DisableEnvKey)), "off") || !filepath.IsAbs(directory) { return Result{} } lock := r.dirLock(directory) lock.Lock() defer lock.Unlock() direnv := r.find(base, "direnv") mise := r.find(base, "mise") if direnv == "" && mise == "" { return Result{} } r.mu.Lock() previous, cached := r.cache[directory] r.mu.Unlock() fingerprint := r.fingerprint(directory, base, direnv, mise, previous.watches) if cached && previous.fingerprint == fingerprint { return previous.result } result := Result{Changes: map[string]*string{}} var watches []string clean := withoutDirenvState(base) if mise != "" { changes, warning := r.runMise(ctx, mise, directory, clean) if warning != "" { result.Warnings = append(result.Warnings, warning) } else if len(changes) > 0 { for key, value := range changes { result.Changes[key] = value } result.Sources = append(result.Sources, "mise") } } if direnv != "" { changes, watched, files, warning := r.runDirenv(ctx, direnv, directory, clean) watches = watched if warning != "" { result.Warnings = append(result.Warnings, warning) } else if len(changes) > 0 { for key, value := range changes { result.Changes[key] = value } result.Sources = append(result.Sources, "direnv") result.Files = files } } // A cancelled lookup is not cached; the next start tries again. if ctx.Err() == nil { entry := cacheEntry{result: result, watches: watches} entry.fingerprint = r.fingerprint(directory, base, direnv, mise, watches) r.mu.Lock() if r.cache == nil { r.cache = map[string]cacheEntry{} } r.cache[directory] = entry r.mu.Unlock() } return result } func (r *Resolver) dirLock(directory string) *sync.Mutex { r.mu.Lock() defer r.mu.Unlock() if r.running == nil { r.running = map[string]*sync.Mutex{} } lock := r.running[directory] if lock == nil { lock = &sync.Mutex{} r.running[directory] = lock } return lock } func (r *Resolver) find(base []string, name string) string { dirs := filepath.SplitList(lookup(base, "PATH")) dirs = append(dirs, r.SearchDirs...) for _, dir := range dirs { if dir == "" || !filepath.IsAbs(dir) { continue } candidate := filepath.Join(dir, name) if info, err := os.Stat(candidate); err == nil && !info.IsDir() && info.Mode()&0o111 != 0 { return candidate } } return "" } func (r *Resolver) timeout() time.Duration { if r.Timeout > 0 { return r.Timeout } return 30 * time.Second } // run executes one tool in directory and returns stdout, a bounded stderr // and the error. func (r *Resolver) run(ctx context.Context, binary, directory string, env []string, args ...string) ([]byte, string, error) { ctx, cancel := context.WithTimeout(ctx, r.timeout()) defer cancel() cmd := exec.CommandContext(ctx, binary, args...) cmd.Dir = directory cmd.Env = WithPWD(env, directory) var stdout, stderr bytes.Buffer cmd.Stdout = &stdout cmd.Stderr = &stderr cmd.WaitDelay = 2 * time.Second err := cmd.Run() return stdout.Bytes(), stderr.String(), err } var blockedRC = regexp.MustCompile(`(\S+) is blocked`) 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, 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, 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, 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, files, "" } func (r *Resolver) runMise(ctx context.Context, binary, directory string, env []string) (map[string]*string, string) { stdout, stderr, err := r.run(ctx, binary, directory, env, "env", "--json") if err != nil { lower := strings.ToLower(stderr) if strings.Contains(lower, "not trusted") { return nil, "mise: config for " + directory + " is not trusted, so its environment was not applied. Run `mise trust` there to use it." } return nil, "mise env failed in " + directory + ", so its environment was not applied: " + firstLine(stderr, err) } values := map[string]string{} if text := bytes.TrimSpace(stdout); len(text) > 0 { if err := json.Unmarshal(text, &values); err != nil { return nil, "mise env returned invalid output in " + directory + "; its environment was not applied" } } changes := map[string]*string{} for key, value := range values { if current, ok := lookupOK(env, key); ok && current == value { continue } value := value changes[key] = &value } return changes, "" } // fingerprint changes when anything that can change the tools' answers // changes: the candidate config files from directory up to /, the files // direnv reported watching, direnv's allow/deny records, and the binaries. func (r *Resolver) fingerprint(directory string, base []string, direnv, mise string, watches []string) string { var paths []string paths = append(paths, direnv, mise) for dir := directory; ; dir = filepath.Dir(dir) { for _, name := range configNames { paths = append(paths, filepath.Join(dir, name)) } if parent := filepath.Dir(dir); parent == dir { break } } paths = append(paths, watches...) data := lookup(base, "XDG_DATA_HOME") config := lookup(base, "XDG_CONFIG_HOME") if home := lookup(base, "HOME"); home != "" { if data == "" { data = filepath.Join(home, ".local", "share") } if config == "" { config = filepath.Join(home, ".config") } } if data != "" { paths = append(paths, filepath.Join(data, "direnv", "allow"), filepath.Join(data, "direnv", "deny"), filepath.Join(data, "mise", "trusted-configs")) } if config != "" { paths = append(paths, filepath.Join(config, "direnv", "direnv.toml"), filepath.Join(config, "mise", "config.toml")) } var b strings.Builder for _, path := range paths { if path == "" { continue } b.WriteString(path) if info, err := os.Stat(path); err == nil { fmt.Fprintf(&b, ":%d:%d:%o", info.ModTime().UnixNano(), info.Size(), info.Mode()) } else { b.WriteString(":-") } b.WriteByte('\n') } return b.String() } // configNames are the per-directory files direnv and mise read. var configNames = []string{ ".envrc", ".env", "mise.toml", ".mise.toml", "mise.local.toml", ".mise.local.toml", filepath.Join(".config", "mise.toml"), filepath.Join(".config", "mise", "config.toml"), filepath.Join("mise", "config.toml"), filepath.Join(".mise", "config.toml"), ".tool-versions", } // decodeWatches reads direnv's DIRENV_WATCHES (zlib-compressed JSON in URL // base64). An unreadable value only means less precise invalidation. func decodeWatches(encoded string) []string { raw, err := base64.URLEncoding.DecodeString(encoded) if err != nil { if raw, err = base64.RawURLEncoding.DecodeString(encoded); err != nil { return nil } } reader, err := zlib.NewReader(bytes.NewReader(raw)) if err != nil { return nil } defer reader.Close() plain, err := io.ReadAll(io.LimitReader(reader, 1<<20)) if err != nil { return nil } var entries []struct { Path string `json:"path"` } if json.Unmarshal(plain, &entries) != nil { return nil } paths := make([]string, 0, len(entries)) for _, entry := range entries { if entry.Path != "" { paths = append(paths, entry.Path) } } return paths } // WithPWD returns env with PWD naming directory, as a shell that changed into // it would have (the logical path, not a resolved symlink). func WithPWD(env []string, directory string) []string { out := make([]string, 0, len(env)+1) for _, item := range env { if !strings.HasPrefix(item, "PWD=") { out = append(out, item) } } return append(out, "PWD="+directory) } // withoutDirenvState drops direnv's own bookkeeping so the export is computed // from the server's environment, not a shell the server was started from. func withoutDirenvState(base []string) []string { out := make([]string, 0, len(base)) for _, item := range base { if strings.HasPrefix(item, "DIRENV_") { continue } out = append(out, item) } return out } func firstLine(stderr string, err error) string { for _, line := range strings.Split(stderr, "\n") { if line = strings.TrimSpace(line); line != "" { if len(line) > 300 { line = line[:300] + "..." } return line } } return err.Error() } func lookup(env []string, key string) string { value, _ := lookupOK(env, key) return value } func lookupOK(env []string, key string) (string, bool) { for i := len(env) - 1; i >= 0; i-- { if k, v, ok := strings.Cut(env[i], "="); ok && k == key { return v, true } } return "", false }