-
Notifications
You must be signed in to change notification settings - Fork 1k
ci: improve 'tfail in goroutine' ruleguard rule #19682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
go func($*_){ | ||
$*_ | ||
$require.$_($*_) | ||
require.$_($*_) | ||
$*_ | ||
}($*_)`). | ||
At(m["require"]). | ||
Where(m["require"].Text == "require"). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The prior version would only match on the first line in the goroutine of the form <A>.<B>(<C>)
, meaning the rule wouldn't fire a violation if the require.*
came after some other selector.Func
. Ruleguard's pretty limited, and neither I nor Blink could find a way to make the matching not greedy.
To fix, we're just going to only match on lines where the selector is requires
.
m.Match(` | ||
go func($*_){ | ||
$*_ | ||
$t.$fail($*_) | ||
t.$fail($*_) | ||
$*_ | ||
}($*_)`). | ||
At(m["fail"]). | ||
Where(m["t"].Type.Implements("testing.TB") && m["fail"].Text.Matches("^(FailNow|Fatal|Fatalf)$")). | ||
Where(m["fail"].Text.Matches("^(FailNow|Fatal|Fatalf)$")). | ||
Report("Do not call functions that may call t.FailNow in a goroutine, as this can cause data races (see testing.go:834)") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We apply the same idea here, but with a caveat that if there's some other t.*
function that's called before the t.Fail/whatever
the rule won't fire. It's not perfect, but it's probably good enough.
7eba8dd
to
10d0f7f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for improving this! Ruleguard DSL is tricky at the best of times.
This PR improves the ruleguard rule for detecting
t.Fail
calls in goroutines. It picks up additional violations, of which are fixed in this PR.See self-review for details.
The motivation for fixing this comes from a flake I fixed in #19599, where tests would fail from a
require
in anEventually
.