Documentation
¶
Overview ¶
Package shopts reports shell scripts that do not establish the shell options the gomatic shell standard requires, and `set` calls written in the short flag form the standard forbids.
Two rules, one subject. A script must enable errexit, nounset and pipefail — without them a failing command, an unset variable, and a failing stage of a pipeline are all silently survivable, which is how a script reports success over work it never did. And every option must be spelled in the LONG form, `set -o errexit`, never `set -e` or `set -euo pipefail`: the letters are unreadable to anyone who has not memorised them, and the standard names the long form precisely so a reader can see which property is being claimed.
The requirement is scoped to scripts that own their options — a file with a `#!` shebang, which is an entry point the shell executes on its own. A fragment meant to be sourced inherits the caller's options and cannot meaningfully set its own (`set -o errexit` inside a sourced file changes the CALLER's shell), so demanding them there would be both wrong and noisy. The short-form rule has no such scope: a short flag is unreadable wherever it is written.
Parsing is delegated to mvdan.cc/sh, the same parser shfmt uses, so `set -e` inside a string, a comment, or a heredoc is not a finding — a regex over the text cannot make that distinction, and the fleet's scripts contain all three.
Index ¶
- Constants
- func Diagnostics(path Path, source Source) ([]goyze.Diagnostic, error)
- func NamesShell(line ShebangLine) bool
- func ReadFailure(file Path, cause error) error
- func Report(read FileReader, files []string) goyze.Report
- func Unreadable(paths []string) []goyze.Diagnostic
- type FileReader
- type Path
- type ShebangLine
- type Source
Constants ¶
const Category = "shell"
Category is the language group this analyzer belongs to, used by the yze suite to run it only when processing shell.
const ErrNoPaths errs.Const = "no paths to analyze"
ErrNoPaths reports a run given nothing to analyze. A runner whose root placeholder expands to nothing would otherwise green the gate over a repository no analyzer ever looked at — indistinguishable, at the exit code, from a repository it read and found clean.
const ErrNotRegularFile = goyze.ErrNotRegularFile
ErrNotRegularFile reports an argument that is not a regular file. It IS the shared sentinel, not a second one beside it: the refusal is raised by the discovery this command walks with, and a caller holding this package's constant could only match it because the two happened to carry the same text. Coupling two sentinels by their message string means editing either message silently breaks `errors.Is` for everyone — which is the defect ErrTooLarge was already repaired for, one condition away.
const ErrParse errs.Const = "cannot parse shell source"
ErrParse reports a file that does not parse as shell.
const ErrReadFile errs.Const = "cannot read shell file"
ErrReadFile reports that a shell source file could not be read.
const ErrTooDeep errs.Const = "shell source is nested too deeply to parse"
ErrTooDeep reports a source nested past what the parser can descend safely.
const ErrTooLarge = goyze.ErrTooLarge
ErrTooLarge reports a file past the size this rule will read. It IS the shared sentinel, not a second one beside it: the bound is enforced in two places — here, and at the one place the command reads a file — and two sentinels for one condition meant `errors.Is` answered false for whichever layer the caller had not thought of.
const Name = "shopts"
Name is the analyzer's stable identifier — the suffix of its flat rule id and the key the yze suite catalogs it under.
const Rule = Tool + "/" + Name
Rule is the stable, flat rule id every diagnostic carries: "yze/" + Name.
const SizeLimit goyze.ByteCount = 128 << 10
sizeLimit bounds the source handed to the parser, in bytes.
It is the other half of the nesting guard, and the half [maxNesting] cannot provide. `if`, `while`, `case` and `time` nest the recursive parser without writing a single bracket, so counting brackets sees a depth of ZERO for a file made entirely of them — and 5.2 MB of nested `if` killed the process exactly as the parenthesis file had. The shortest such construct costs ten bytes a level, so a megabyte cannot reach a hundred and ten thousand levels, well under the three hundred thousand the parser survives. Brackets cost one byte a level and are bounded by the counter instead; between them every way of nesting is covered. No hand-written script comes near either limit. Two earlier bounds were each defeated by a construct they had not modelled. Counting brackets saw depth ZERO for `if`/`while`/`case`/`time`, which nest without writing one. A one-megabyte limit then admitted `$((!!!…1))`, which costs ONE byte per level and writes two brackets in total — slipping both bounds and overflowing not the parser but this analyzer's own recursive walk of the tree, which no bound on the parser models at all.
So the limit is set from the DENSEST nesting a byte can buy rather than from a convenient round number: at one byte per level, 128 KiB cannot build more than 131 072 levels, and both the parser and the walk survive several times that. Measured across the fleet on 2026-08-10, the largest shell script anywhere in it is 14 944 bytes — nine times under this limit — so nothing real is refused. SizeLimit is exported so the command can refuse a file from its directory entry, before opening it.
const Tool = "yze"
Tool is the suite name stamped on every diagnostic. The analyzer is bundled into the yze suite (the language group lives in the repo path and Category, not the rule id), so it reports as "yze", not a separate "yze-sh".
Variables ¶
This section is empty.
Functions ¶
func Diagnostics ¶
func Diagnostics(path Path, source Source) ([]goyze.Diagnostic, error)
Diagnostics reports the shell-option findings in source. path is stamped on each diagnostic's location. A source that does not parse as shell yields the parse error, so the caller surfaces a tool failure rather than a clean pass.
func NamesShell ¶
func NamesShell(line ShebangLine) bool
NamesShell reports whether an interpreter line names a shell this rule judges. It is exported because the command needs the same answer to decide which files to read, and the interpreter set must be defined ONCE: a second copy in the command drifted from this one, and four of the entries were tied together by nothing at all.
func ReadFailure ¶
ReadFailure is the error a file that could not be read earns, before it is rendered into the finding that carries it.
It is exported so the failure can be MATCHED rather than only read: Report answers with diagnostics and never an error, so with this inlined the sentinel could be swapped for any other and every test still passed.
func Report ¶
func Report(read FileReader, files []string) goyze.Report
Report runs the shell-option check over each file and aggregates the diagnostics into the lean stickler-json report. A read failure aborts with ErrReadFile, because a filesystem that will not answer is a tool failure rather than a property of any one file.
A file that does not PARSE is contained instead: it becomes one finding against that file and the run continues. Aborting was a real outage — a self-extracting installer, a compliant `#!/bin/bash` header followed by a binary payload, is claimed by discovery, fails the parser on its invalid UTF-8, and took every other file's findings in the run down with it. The file is still REPORTED, so nothing is quietly passed over; it is simply no longer able to silence its neighbours. This analyzer carries no run-level report limit, and that is measured rather than overlooked. Four of the six source analyzers bound one run at 10,000 findings because a single document can produce thousands: `keywordcase` reports per mis-cased keyword, and one file of 20,000 uppercase statements measured 20,000 findings. This rule reports a VERDICT PER FILE -- the same 20,000-line input measured ONE finding -- so its report is the size of the tree rather than a multiple of it, and a bound would be machinery for a shape the rule cannot produce.
func Unreadable ¶
func Unreadable(paths []string) []goyze.Diagnostic
Unreadable is the finding for each path the walk could not read: a directory it could not enter, and a file it could not have read had it tried — a FIFO, a device, a link resolving to nothing. Both are REPORTED rather than skipped, because a path the gate cannot open is where an unchecked one would hide, and the run still yields every other file's findings.
Types ¶
type FileReader ¶
FileReader reads a file's bytes; injected so aggregation is testable without the filesystem.