staleness

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: BSD-3-Clause Imports: 19 Imported by: ? 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 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

View Source
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.

View Source
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

View Source
var ErrNotFound = errors.New("module unknown to the proxy")

ErrNotFound means the proxy does not know the module (404 or 410).

Functions

func Latest

func Latest(ctx context.Context, modules []string, p Proxy) map[string]Release

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

func ReadsGoSource(deps []string) bool

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

func Read(r io.ReaderAt, name, dir string) (Binary, error)

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

func Scan(dirs []string) ([]Binary, error)

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

func (b Binary) BuiltLocally() bool

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.

func (Release) Has

func (r Release) Has(v string) int

Has returns the position of v in Versions, or -1.

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.

func CheckSelf

func CheckSelf(running *debug.BuildInfo, exe string, r Release) SelfVerdict

CheckSelf compares running (debug.ReadBuildInfo of this process) with the executable at exe and with r. A nil running yields NotMeasured.

type State

type State int

State is what one installed binary's version means. Each state is distinct; none is folded into Current.

const (
	Current      State = iota // the newest release
	Behind                    // an older release of itself
	Unreleased                // a version that is not a release (pseudo-version)
	BuiltLocally              // no module sum: built from a tree, cannot be compared
	NotMeasured               // the proxy could not be asked, or knows no release
)

func JudgeVersion

func JudgeVersion(installed string, r Release) State

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.

func (State) String

func (s State) String() string

type Verdict

type Verdict struct {
	State     State
	Installed string // the version string the binary carries, shown as is
	Latest    string // "" when unknown
	Behind    int    // releases back, for Behind only
}

Verdict is a State with the facts that justify it.

func Judge

func Judge(b Binary, r Release) Verdict

Judge classifies one binary. A binary built locally is reported as such whatever the proxy says: its version string cannot be compared.

Directories

Path Synopsis
Package proc finds processes that still run a binary which has since been replaced on disk.
Package proc finds processes that still run a binary which has since been replaced on disk.

Jump to

Keyboard shortcuts

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