A package other than main ends the program instead of returning an error

tx0/go:exit-outside-mainstrictwarningmaintainability/designdepth fastsmell

What it finds

log.Fatalf("bad config: %v", err) in a library: the call ends the whole program, so a caller cannot handle the error, deferred functions do not run, and tests of the package stop. Libraries return errors and leave the decision to main.

Reported are calls of os.Exit and of Fatal, Fatalf and Fatalln of log in files other than tests whose package is not main.

From the rule's source, exit_outside_main.rs.

Example from the rule's tests
package config import (	"log"	"os") func Load(path string) {	if _, err := os.Stat(path); err != nil {		log.Fatalf("no config: %v", err)tx0 reports: log.Fatalf ends the program from a library	}}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/go:exit-outside-main" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/go:exit-outside-main] reason="..."

tx0 explain tx0/go:exit-outside-main shows the same in the terminal. Found a false positive? Report it.