From 6099975c72870e21b8adb187bda41a91768c7dac Mon Sep 17 00:00:00 2001 From: Jonathan Baldie Date: Thu, 14 May 2026 11:40:18 +0100 Subject: [PATCH] fix: replace panic with proper error returns for OS-level failures in main Five panic calls for routine OS errors (MkdirTemp, MkdirAll, CopyFile, RemoveAll, and exec.Run) crashed the tool with a raw stack trace instead of a readable message; they now call exitError or return a non-zero exit code so the user sees what went wrong. Co-Authored-By: Claude Sonnet 4.6 --- cmd/go-mutesting/main.go | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/cmd/go-mutesting/main.go b/cmd/go-mutesting/main.go index 5749905..d53b64b 100644 --- a/cmd/go-mutesting/main.go +++ b/cmd/go-mutesting/main.go @@ -191,7 +191,7 @@ MUTATOR: tmpDir, err := os.MkdirTemp("", "go-mutesting-") if err != nil { - panic(err) + return exitError("Could not create tmp dir: %v", err) } console.Verbose(opts, "Save mutations into %q", tmpDir) @@ -225,7 +225,7 @@ MUTATOR: err = os.MkdirAll(tmpDir+"/"+filepath.Dir(file), 0755) if err != nil { - panic(err) + return exitError("Could not create dir for mutation file: %v", err) } tmpFile := tmpDir + "/" + file @@ -233,7 +233,7 @@ MUTATOR: originalFile := fmt.Sprintf("%s.original", tmpFile) err = osutil.CopyFile(file, originalFile) if err != nil { - panic(err) + return exitError("Could not copy original file %q: %v", file, err) } console.Debug(opts, "Save original into %q", originalFile) @@ -258,7 +258,7 @@ MUTATOR: if !opts.General.DoNotRemoveTmpFolder { err = os.RemoveAll(tmpDir) if err != nil { - panic(err) + return exitError("Could not remove tmp dir %q: %v", tmpDir, err) } console.Debug(opts, "Remove %q", tmpDir) } @@ -532,21 +532,14 @@ func mutateExec( execCommand.Env = append(execCommand.Env, "TEST_RECURSIVE=true") } - err := execCommand.Start() - if err != nil { - panic(err) - } - - // TODO timeout here - - err = execCommand.Wait() + err := execCommand.Run() if err == nil { execExitCode = 0 } else if e, ok := err.(*exec.ExitError); ok { execExitCode = e.Sys().(syscall.WaitStatus).ExitStatus() } else { - panic(err) + return exitError("Exec command failed unexpectedly: %v", err) } return execExitCode