Avoid using require in goroutines with go-require
masterThe go-require checker flags the use of require (or any function that calls t.FailNow) inside a goroutine.
Why this is a problem:
Functions that call t.FailNow (like require.NoError) terminate the current goroutine using runtime.GoExit. If called inside a goroutine, they will only terminate that specific goroutine and not the main test function, leading to resource leaks and tests that pass even when they should fail.
Best Practices:
- Perform assertions in the main test goroutine.
- If you must check something in a goroutine, use
assertand then signal the failure back to the main goroutine. - Avoid using
requireinside HTTP handlers (which run in separate service goroutines). You can ignore this specific check using--go-require.ignore-http-handlers.
Autofix: Not supported.
// ❌ Avoid (require in goroutine)
go func() {
conn, err = lis.Accept()
require.NoError(t, err)
}()
// ✅ Use assert and handle failure in main goroutine