userenv

package
v0.185.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package userenv gives the processes Caprock starts the environment the user's own terminal would give them.

The daemon's environment is the wrong one to hand down. Started at login by launchd it holds ten variables and PATH=/usr/bin:/bin:/usr/sbin:/sbin — no Homebrew, no gcloud, nothing the user exports from their shell profile — and started from a terminal it is a snapshot of that terminal, frozen for as long as the daemon runs. A session spawned from either one fails in ways a session typed into a terminal does not: `gcloud` is not found, a project variable is unset, a credential helper is missing. The user reads that as "Caprock can't reach BigQuery" when plain `claude` can.

So, the way VS Code resolves its shell environment, the user's login shell is run once, interactively, and asked to print its environment; that is what a spawned process inherits. It is cached and refreshed in the background, a failed refresh keeps the previous copy, a failed first resolution retries on its own, and a session that starts before any has succeeded waits briefly and then falls back to the daemon's environment — a slow or broken profile must never stop a session from starting.

Index

Constants

View Source
const (
	// Timeout bounds one resolution. An interactive zsh with a framework on
	// top starts in about a second at rest — but the first resolution runs as
	// the daemon starts, beside every importer's first pass, and on the
	// owner's machine at load 11 a 5s bound killed it: the cache stayed empty
	// and a session started then would have got the daemon's bare PATH, the
	// exact failure this package exists to prevent (FB-033). A resolution
	// runs in the background, so a generous bound costs nobody a wait.
	Timeout = 30 * time.Second
	// FirstWait is the longest a session start waits for an environment that
	// is being resolved but has never been had. Starting at once with the
	// daemon's would be the silent failure; waiting forever would be a start
	// button that hangs.
	FirstWait = 15 * time.Second
	// TTL is how long a resolved environment is served before a background
	// refresh. An export added to the profile reaches sessions started a
	// minute later, without restarting the daemon.
	TTL = time.Minute
	// ResolvingVar is set in the shell while it is being asked for its
	// environment, so a profile can skip work that only makes sense for a
	// person (a tmux attach, a greeting, an update check).
	ResolvingVar = "CAPROCK_RESOLVING_ENVIRONMENT"
)

Variables

This section is empty.

Functions

func Cached added in v0.70.0

func Cached() []string

Cached returns the login-shell environment if one has been resolved, and nil otherwise, without waiting and without starting a resolution. It is for questions asked on every status poll — "is codex installed?" — which must never hold a request for the user's shell profile. The caller owns the slice.

func CurrentPath added in v0.185.0

func CurrentPath(log *slog.Logger) (string, bool)

CurrentPath is the user's PATH as a program started now would get it, and whether it was read fresh. false means it is the daemon's own snapshot (the registry could not be read, or the login shell has not been resolved), so something installed after Caprock started may not be seen until Caprock restarts — the caller says so.

It never waits: on macOS and Linux a stale login environment is returned at once and refreshed in the background, so a checklist polled every few seconds sees a new install within about TTL.

func Dirs added in v0.185.0

func Dirs(path string) []string

Dirs splits a PATH into its absolute directories.

func EnsureProcessLocale added in v0.120.0

func EnsureProcessLocale()

EnsureProcessLocale gives the daemon's own environment a UTF-8 locale when it has none, so every child started from os.Environ() — the terminal host's fallback, a helper that never asks for the login shell's — inherits one too, not only the ones built through Environ.

func Environ

func Environ(log *slog.Logger) []string

Environ returns the environment for a process Caprock starts on the user's behalf: their login shell's, or the daemon's own when that cannot be had. The caller owns the returned slice.

Only the first call ever waits for the shell (Warm makes even that one free). After that a stale copy is returned at once and refreshed in the background: an interactive zsh with a framework takes one to two seconds, which is not a cost to put in front of every session start.

func SetCachedForTest added in v0.185.1

func SetCachedForTest(env []string) (restore func())

SetCachedForTest makes Cached answer env (nil: nothing resolved yet) until the returned function puts back what was there. For tests in other packages: a login environment an earlier test resolved is the developer's real one, and a binary that test is meant not to find (claude in ~/.local/bin) is then found on that PATH.

func TerminalPath added in v0.185.1

func TerminalPath(log *slog.Logger) (string, bool)

TerminalPath is the PATH a terminal the user opens now gets — the registry's system and user Path on Windows, the login shell's PATH on macOS and Linux — without the daemon's own, and whether it could be read. The setup checklist asks it whether an agent Caprock found is one a terminal finds too (readiness.OffPath): a folder only the daemon has is exactly what it must not count.

func Warm

func Warm(log *slog.Logger)

Warm resolves the environment in the background, so the first session the user starts does not wait for their shell. A failure retries on its own.

func WithUTF8Locale added in v0.120.0

func WithUTF8Locale(env []string, goos string) []string

WithUTF8Locale gives env a UTF-8 locale when it has none, as a terminal application does before it starts a shell.

Nothing else on the way sets one. launchd does not, and a login profile seldom does, because Terminal.app, iTerm2 and VS Code export LANG themselves; only a recent macOS's /etc/zprofile has a default, so bash, fish, an older macOS and a profile that failed to resolve start in the C locale. There the bytes of a pasted "καλημέρα" arrive intact and the program still takes each byte for a character: bash shows `\316\272\316\261…`, zsh `<00ad>` (user report, 2026-10-11: a paste of Russian text). And pbcopy, which an agent runs to copy an answer, reads its input as Mac Roman: a Russian line lands on the clipboard as "DNS-–∑–∞–ø‚Ķ" (user report, 2026-10-15). Caprock is the terminal here, so it sets LANG, and leaves a locale the user did set alone. en_US.UTF-8 exists on every macOS; C.UTF-8 is the one a minimal Linux has. ConPTY speaks UTF-8 whatever the environment says, so Windows gets nothing.

Types

type State added in v0.59.5

type State struct {
	// Source is "login-shell" once resolved, "resolving" before the first
	// resolution ends, "daemon" when it failed and nothing was ever had, and
	// "inherited" on Windows, which has no profile to replay.
	Source     string `json:"source"`
	Shell      string `json:"shell,omitempty"`
	ResolvedAt int64  `json:"resolved_at_ms,omitempty"`
	// Error is the last resolution's failure, kept while it is the reason.
	Error string `json:"error,omitempty"`
}

State says where the environment a started process gets comes from, for `caprock status`: a user whose session cannot see gcloud needs to be able to tell "Caprock never read my profile" from everything else.

func Current added in v0.59.5

func Current() State

Current reports the state of the environment cache.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL