Skip to content

Conversation

ethanndickson
Copy link
Member

@ethanndickson ethanndickson commented Sep 3, 2025

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 an Eventually.

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Comment on lines 183 to -189
go func($*_){
$*_
$require.$_($*_)
require.$_($*_)
$*_
}($*_)`).
At(m["require"]).
Where(m["require"].Text == "require").
Copy link
Member Author

@ethanndickson ethanndickson Sep 3, 2025

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.

Comment on lines 199 to 208
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)")
}
Copy link
Member Author

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.

@ethanndickson ethanndickson force-pushed the ethan/improve-tfail-in-goroutine branch from 7eba8dd to 10d0f7f Compare September 3, 2025 05:31
@ethanndickson ethanndickson marked this pull request as ready for review September 3, 2025 05:32
Copy link
Member

@johnstcn johnstcn left a 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.

@ethanndickson ethanndickson merged commit 50704a5 into main Sep 4, 2025
34 checks passed
@ethanndickson ethanndickson deleted the ethan/improve-tfail-in-goroutine branch September 4, 2025 04:28
@github-actions github-actions bot locked and limited conversation to collaborators Sep 4, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants