Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ethanndickson merged 1 commit intomainfromethan/improve-tfail-in-goroutine
Sep 4, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletioncoderd/files_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -9,6 +9,7 @@ import (
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/coder/coder/v2/archive"
Expand DownExpand Up@@ -88,7 +89,7 @@ func TestPostFiles(t *testing.T) {
data:=make([]byte,1024)
_,err:=client.Upload(ctx,codersdk.ContentTypeTar,bytes.NewReader(data))
end.Done()
require.NoError(t,err)
assert.NoError(t,err)
}()
}
wg.Done()
Expand Down
4 changes: 2 additions & 2 deletionsenterprise/tailnet/pgcoord_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -409,8 +409,8 @@ func TestPGCoordinatorSingle_SendsHeartbeats(t *testing.T) {
if len(heartbeats) < 2 {
return false
}
require.Greater(t, heartbeats[0].Sub(start), time.Duration(0))
require.Greater(t, heartbeats[1].Sub(start), time.Duration(0))
assert.Greater(t, heartbeats[0].Sub(start), time.Duration(0))
assert.Greater(t, heartbeats[1].Sub(start), time.Duration(0))
return assert.Greater(t, heartbeats[1].Sub(heartbeats[0]), tailnet.HeartbeatPeriod*3/4)
}, testutil.WaitMedium, testutil.IntervalMedium)
}
Expand Down
2 changes: 1 addition & 1 deletionscaletest/createworkspaces/run_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -257,7 +257,7 @@ func Test_Runner(t *testing.T) {
err := runner.Run(runnerCtx, "1", logs)
logsStr := logs.String()
t.Log("Runner logs:\n\n" + logsStr)
require.ErrorIs(t, err, context.Canceled)
assert.ErrorIs(t, err, context.Canceled)
close(done)
}()

Expand Down
12 changes: 4 additions & 8 deletionsscripts/rules.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -182,32 +182,28 @@ func doNotCallTFailNowInsideGoroutine(m dsl.Matcher) {
m.Match(`
go func($*_){
$*_
$require.$_($*_)
require.$_($*_)
$*_
}($*_)`).
At(m["require"]).
Where(m["require"].Text == "require").
Comment on lines 183 to -189
Copy link
MemberAuthor

@ethanndicksonethanndicksonSep 3, 2025
edited
Loading

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 therequire.* came after some otherselector.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 isrequires.

Report("Do not call functions that may call t.FailNow in a goroutine, as this can cause data races (see testing.go:834)")

// require.Eventually runs the function in a goroutine.
m.Match(`
require.Eventually(t, func() bool {
$*_
$require.$_($*_)
require.$_($*_)
$*_
}, $*_)`).
At(m["require"]).
Where(m["require"].Text == "require").
Report("Do not call functions that may call t.FailNow in a goroutine, as this can cause data races (see testing.go:834)")

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

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 othert.* function that's called before thet.Fail/whatever the rule won't fire. It's not perfect, but it's probably good enough.


Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp