Documentation
¶
Overview ¶
Package staleness answers, read-only, two questions about the Go tools installed on a machine: is this binary an older release of itself, and is anything still running a file that has since been replaced.
It was `package main` of staletools; agentscope's "tools" diagnostic needs the same answers without shelling out.
What it touches ¶
- The filesystem: only the directories it is given, regular files only, files over MaxBinarySize skipped, symlinks followed only when their target stays under the user's home.
- The network: the Go module proxy only ($GOPROXY, default proxy.golang.org), see Latest. Never GitHub.
- Nothing is written except the proxy cache, and nothing is signalled.
Build information is read with debug/buildinfo (https://pkg.go.dev/debug/buildinfo): the same record `go version -m` prints.
Index ¶
Constants ¶
const DefaultTTL = 6 * time.Hour
DefaultTTL is how long a module's answer is reused from the on-disk cache: at most one proxy round trip per module per six hours.
const MaxBinarySize = 256 << 20
MaxBinarySize is the largest file Scan will open. A bound, not a judgement: nothing in a bin directory that holds a Go tool is this large, and a directory that is not what it seems must not make the scan read gigabytes.
Variables ¶
var ErrNotFound = errors.New("module unknown to the proxy")
ErrNotFound means the proxy does not know the module (404 or 410).
Functions ¶
func Latest ¶
Latest asks the Go module proxy for every module, concurrently up to the cap, and returns one Release per module (an unanswerable one carries Err; nothing is dropped). It speaks the GOPROXY protocol (https://go.dev/ref/mod#goproxy-protocol): `$base/$module/@v/list` for the versions and `$base/$module/@latest` for what `@latest` installs, which honours retractions.
func ReadsGoSource ¶
ReadsGoSource reports whether a dependency list belongs to a tool that parses Go source. A prefix match WITH the slash: "golang.org/x/tools" matches "golang.org/x/tools/gopls" and not "golang.org/x/toolsomething".
Types ¶
type Binary ¶
type Binary struct {
Name string // file name, as it is run
Dir string // the directory it was found in, as given to Scan
Path string // import path of the main package (bi.Path)
GoVersion string // toolchain that built it
// Module and ModVersion are the MODULE the command came from, which is
// what the proxy can be asked about; Path names no version. Sum is the
// go.sum hash of that module version and is EMPTY when the binary was
// built from a local checkout rather than installed from the proxy.
Module, ModVersion, Sum string
ReadsSource bool // imports a Go source reader (x/tools and kin)
ModTime time.Time // of the file on disk, after symlinks
Size int64
}
Binary is one installed Go executable.
func Read ¶
Read extracts the Binary fields from an executable's contents. It is the parsing surface a fuzzer drives: whatever the bytes are, it returns an error or a Binary, never panics. name and dir are carried through.
func Scan ¶
Scan reads the build info of every Go executable in each directory, sorted by name then directory.
A file that is not a Go binary is skipped in silence: these directories hold shell scripts too, and complaining about each would bury the answer. A directory that does not exist is skipped: not every machine has both $GOBIN and ~/.local/bin. Any other failure to list a directory is an error.
func ScanWith ¶
func ScanWith(dirs []string, opt ScanOptions) ([]Binary, error)
ScanWith is Scan with explicit bounds.
func (Binary) BuiltLocally ¶
BuiltLocally reports that the binary carries no module sum, or a version the proxy cannot know: it was built from a working tree, not installed from a release. Its version string cannot be compared with anything.
type Proxy ¶
type Proxy struct {
// Bases are the proxy base URLs, tried in order. Empty means GOPROXY, then
// https://proxy.golang.org. Entries that are not http(s) URLs ("direct",
// "off", file://) are skipped: this package never contacts a VCS host.
Bases []string
// Client defaults to one with a 10 s timeout.
Client *http.Client
// CacheDir, when set, holds one small file per module and base. Cache
// failures are never fatal.
CacheDir string
// TTL defaults to [DefaultTTL].
TTL time.Duration
// Concurrency caps in-flight requests; default 4.
Concurrency int
// Now is the clock, for tests.
Now func() time.Time
}
Proxy configures Latest. The zero value works: GOPROXY from the environment, no cache, four requests at a time, ten seconds each.
func ProxyFromEnv ¶
func ProxyFromEnv() Proxy
ProxyFromEnv reads GOPROXY and puts the cache under os.UserCacheDir.
type Release ¶
type Release struct {
Mod string
Latest string // the newest released version, "" when there is none
Versions []string // every released version, oldest first (semver order)
Err error // non-nil: the proxy could not be asked
}
Release is what the module proxy says about one module.
type ScanOptions ¶
type ScanOptions struct {
// Home is the directory symlink targets must stay under. Empty means no
// symlink is followed.
Home string
// MaxSize overrides [MaxBinarySize] when positive.
MaxSize int64
}
ScanOptions bounds a scan.
func DefaultScanOptions ¶
func DefaultScanOptions() ScanOptions
DefaultScanOptions uses the user's home directory.
type SelfVerdict ¶
type SelfVerdict struct {
Running string // module version of the running build
OnDisk string // module version of the file at exe, "" if unreadable
// Replaced is true when the file on disk is a different build from the
// one running (different module version or main-module sum).
Replaced bool
Verdict Verdict // the RUNNING build against Latest
}
SelfVerdict compares the running program with the file now on disk and with the latest release, so a UI can say "restart me" when it is the stale one.
type State ¶
type State int
State is what one installed binary's version means. Each state is distinct; none is folded into Current.
func JudgeVersion ¶
JudgeVersion classifies an installed version against a Release, without looking at how the binary was built. It is NOT a string comparison against the newest: a pseudo-version appears nowhere in the list, and calling that "behind" would be a mislabel.