|
| 1 | +// Copyright 2026 Synnax Labs, Inc. |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License included in the file |
| 4 | +// licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with the Business Source |
| 7 | +// License, use of this software will be governed by the Apache License, Version 2.0, |
| 8 | +// included in the file licenses/APL.txt. |
| 9 | + |
| 10 | +package leaklint |
| 11 | + |
| 12 | +import ( |
| 13 | + "go/ast" |
| 14 | + "go/token" |
| 15 | + |
| 16 | + "golang.org/x/tools/go/analysis" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // leakCheck is the per-scope goroutine-leak assertion that BeforeSuite and BeforeAll |
| 21 | + // nodes must call first. |
| 22 | + leakCheck = "ShouldNotLeakGoroutines" |
| 23 | + // perSpecCheck is the suite-wide per-spec goroutine-leak assertion that every suite |
| 24 | + // must register at package scope. |
| 25 | + perSpecCheck = "ShouldNotLeakGoroutinesPerSpec" |
| 26 | + // runSpecs is the Ginkgo entry point whose presence marks a package as a test suite. |
| 27 | + runSpecs = "RunSpecs" |
| 28 | +) |
| 29 | + |
| 30 | +// setupNodes are the Ginkgo lifecycle nodes whose fixtures persist across specs and so |
| 31 | +// must verify their own teardown with leakCheck. Per-spec nodes (BeforeEach, |
| 32 | +// JustBeforeEach) are intentionally excluded: they are covered by perSpecCheck. |
| 33 | +var setupNodes = map[string]struct{}{ |
| 34 | + "BeforeSuite": {}, |
| 35 | + "BeforeAll": {}, |
| 36 | +} |
| 37 | + |
| 38 | +var Analyzer = &analysis.Analyzer{ |
| 39 | + Name: "leaklint", |
| 40 | + Doc: `enforces goroutine-leak checks in Ginkgo suites. |
| 41 | +
|
| 42 | +This analyzer enforces two conventions from github.com/synnaxlabs/x/testutil: |
| 43 | +
|
| 44 | + 1. Every package that calls RunSpecs (a Ginkgo suite) must register the per-spec |
| 45 | + leak check at package scope, e.g. ` + "`var _ = ShouldNotLeakGoroutinesPerSpec()`" + `. |
| 46 | +
|
| 47 | + 2. Every BeforeSuite and BeforeAll node must call ShouldNotLeakGoroutines() as its |
| 48 | + first statement, so the goroutine snapshot is taken before the node creates its |
| 49 | + fixtures and the matching AfterSuite/AfterAll teardown is verified to release them. |
| 50 | +
|
| 51 | +A node that legitimately needs to opt out (e.g. a suite that deliberately leaves a |
| 52 | +process-global daemon running) can be exempted with a //nolint:leaklint comment.`, |
| 53 | + Run: run, |
| 54 | +} |
| 55 | + |
| 56 | +func run(pass *analysis.Pass) (any, error) { |
| 57 | + for _, file := range pass.Files { |
| 58 | + ast.Inspect(file, func(n ast.Node) bool { |
| 59 | + call, ok := n.(*ast.CallExpr) |
| 60 | + if !ok { |
| 61 | + return true |
| 62 | + } |
| 63 | + if _, isSetup := setupNodes[calleeName(call)]; !isSetup { |
| 64 | + return true |
| 65 | + } |
| 66 | + body := setupBody(call) |
| 67 | + if body == nil { |
| 68 | + // The node was given a named function rather than a literal; its body is |
| 69 | + // not visible here, so there is nothing to check. |
| 70 | + return true |
| 71 | + } |
| 72 | + if !firstStmtIsLeakCheck(body) { |
| 73 | + pass.Report(analysis.Diagnostic{ |
| 74 | + Pos: call.Pos(), |
| 75 | + End: call.End(), |
| 76 | + Message: calleeName(call) + " must call " + leakCheck + |
| 77 | + "() as its first statement so the goroutine snapshot is taken " + |
| 78 | + "before fixtures are created", |
| 79 | + }) |
| 80 | + } |
| 81 | + return true |
| 82 | + }) |
| 83 | + } |
| 84 | + checkSuiteHasPerSpec(pass) |
| 85 | + return nil, nil |
| 86 | +} |
| 87 | + |
| 88 | +// checkSuiteHasPerSpec reports every RunSpecs call in a package that does not also |
| 89 | +// register perSpecCheck at package scope. |
| 90 | +func checkSuiteHasPerSpec(pass *analysis.Pass) { |
| 91 | + if hasTopLevelPerSpec(pass) { |
| 92 | + return |
| 93 | + } |
| 94 | + for _, file := range pass.Files { |
| 95 | + ast.Inspect(file, func(n ast.Node) bool { |
| 96 | + call, ok := n.(*ast.CallExpr) |
| 97 | + if !ok || calleeName(call) != runSpecs { |
| 98 | + return true |
| 99 | + } |
| 100 | + pass.Report(analysis.Diagnostic{ |
| 101 | + Pos: call.Pos(), |
| 102 | + End: call.End(), |
| 103 | + Message: "test suite calls " + runSpecs + " but does not register " + |
| 104 | + perSpecCheck + "() at package scope; add `var _ = " + |
| 105 | + perSpecCheck + "()`", |
| 106 | + }) |
| 107 | + return true |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// hasTopLevelPerSpec reports whether any file in the package contains a package-scope |
| 113 | +// call to perSpecCheck, e.g. `var _ = ShouldNotLeakGoroutinesPerSpec()`. |
| 114 | +func hasTopLevelPerSpec(pass *analysis.Pass) bool { |
| 115 | + for _, file := range pass.Files { |
| 116 | + for _, decl := range file.Decls { |
| 117 | + gen, ok := decl.(*ast.GenDecl) |
| 118 | + if !ok || gen.Tok != token.VAR { |
| 119 | + continue |
| 120 | + } |
| 121 | + found := false |
| 122 | + ast.Inspect(gen, func(n ast.Node) bool { |
| 123 | + if call, ok := n.(*ast.CallExpr); ok && calleeName(call) == perSpecCheck { |
| 124 | + found = true |
| 125 | + return false |
| 126 | + } |
| 127 | + return true |
| 128 | + }) |
| 129 | + if found { |
| 130 | + return true |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + return false |
| 135 | +} |
| 136 | + |
| 137 | +// firstStmtIsLeakCheck reports whether the first statement of body is a bare call to |
| 138 | +// leakCheck. |
| 139 | +func firstStmtIsLeakCheck(body *ast.BlockStmt) bool { |
| 140 | + if len(body.List) == 0 { |
| 141 | + return false |
| 142 | + } |
| 143 | + exprStmt, ok := body.List[0].(*ast.ExprStmt) |
| 144 | + if !ok { |
| 145 | + return false |
| 146 | + } |
| 147 | + call, ok := exprStmt.X.(*ast.CallExpr) |
| 148 | + if !ok { |
| 149 | + return false |
| 150 | + } |
| 151 | + return calleeName(call) == leakCheck |
| 152 | +} |
| 153 | + |
| 154 | +// setupBody returns the body of the first function-literal argument of a |
| 155 | +// BeforeSuite/BeforeAll call, or nil if the call has no literal argument (e.g. it was |
| 156 | +// handed a named function, whose body cannot be inspected here). |
| 157 | +func setupBody(call *ast.CallExpr) *ast.BlockStmt { |
| 158 | + for _, arg := range call.Args { |
| 159 | + if lit, ok := arg.(*ast.FuncLit); ok { |
| 160 | + return lit.Body |
| 161 | + } |
| 162 | + } |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +// calleeName returns the called function's identifier for both unqualified F(...) (the |
| 167 | +// dot-imported case) and qualified pkg.F(...) calls. It returns "" for any other callee |
| 168 | +// expression. |
| 169 | +func calleeName(call *ast.CallExpr) string { |
| 170 | + switch fn := call.Fun.(type) { |
| 171 | + case *ast.Ident: |
| 172 | + return fn.Name |
| 173 | + case *ast.SelectorExpr: |
| 174 | + return fn.Sel.Name |
| 175 | + } |
| 176 | + return "" |
| 177 | +} |
0 commit comments