userenv

package
v0.78.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: Apache-2.0 Imports: 11 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 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 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.

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