-
Notifications
You must be signed in to change notification settings - Fork 56
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
(WIP) 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check #1599
base: main
Are you sure you want to change the base?
(WIP) 🐛 Fix: Prevent nil errors in setupLog.Error to ensure proper logging and add sanity check #1599
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,12 +128,12 @@ func main() { | |
} | ||
|
||
if (certFile != "" && keyFile == "") || (certFile == "" && keyFile != "") { | ||
setupLog.Error(nil, "unable to configure TLS certificates: tls-cert and tls-key flags must be used together") | ||
setupLog.Error(fmt.Errorf("unable to configure TLS certificates"), "tls-cert and tls-key flags must be used together") | ||
os.Exit(1) | ||
} | ||
|
||
if metricsAddr != "" && certFile == "" && keyFile == "" { | ||
setupLog.Error(nil, "metrics-bind-address requires tls-cert and tls-key flags to be set") | ||
setupLog.Error(fmt.Errorf("unable to configure metrics-bind-address"), "metrics-bind-address requires tls-cert and tls-key flags to be set") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: use |
||
os.Exit(1) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/github.com/operator-framework/operator-controller/custom-linters/setuplognilerrorcheck" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/unitchecker" | ||
) | ||
|
||
// Define the custom Linters implemented in the project | ||
var customLinters = []*analysis.Analyzer{ | ||
setuplognilerrorcheck.SetupLogNilErrorCheck, | ||
} | ||
|
||
func main() { | ||
unitchecker.Main(customLinters...) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module github.com/github.com/operator-framework/operator-controller/custom-linters | ||
|
||
go 1.23.4 | ||
|
||
require golang.org/x/tools v0.29.0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= | ||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= | ||
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= | ||
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY= | ||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= | ||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= | ||
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE= | ||
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package setuplognilerrorcheck | ||
|
||
import ( | ||
"go/ast" | ||
"golang.org/x/tools/go/analysis" | ||
) | ||
|
||
var SetupLogNilErrorCheck = &analysis.Analyzer{ | ||
Name: "setuplognilerrorcheck", | ||
Doc: "check for setupLog.Error(nil, ...) calls with nil as the first argument", | ||
Run: runSetupLogNilErrorCheck, | ||
} | ||
|
||
func runSetupLogNilErrorCheck(pass *analysis.Pass) (interface{}, error) { | ||
for _, f := range pass.Files { | ||
ast.Inspect(f, func(n ast.Node) bool { | ||
expr, ok := n.(*ast.CallExpr) | ||
if !ok { | ||
return true | ||
} | ||
|
||
selectorExpr, ok := expr.Fun.(*ast.SelectorExpr) | ||
if !ok || selectorExpr.Sel.Name != "Error" { | ||
return true | ||
} | ||
|
||
i, ok := selectorExpr.X.(*ast.Ident) | ||
if !ok || i.Name != "setupLog" { | ||
return true | ||
} | ||
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check should:
That way, we'll find all cases where this is an issue, and we'll protect ourselves in the case that we decide to change the name of |
||
|
||
if len(expr.Args) > 0 { | ||
if arg, ok := expr.Args[0].(*ast.Ident); ok && arg.Name == "nil" { | ||
pass.Reportf(expr.Pos(), "Avoid using 'setupLog.Error(nil, ...)'. Instead, use 'errors.New()' "+ | ||
"or 'fmt.Errorf()' to ensure logs are created. Using 'nil' for errors can result in silent "+ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most likely, we should just say "pass a non-nil error". If we suggest l.Error(errors.New("could not do thing"), "thing", foo, "otherThing", bar) |
||
"failures, making bugs harder to detect.") | ||
} | ||
} | ||
return true | ||
}) | ||
} | ||
return nil, nil | ||
} |
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.
golangci-lint
should already be runninggo vet
(assuming thegovet
linter is enabled). So I think we get this part automatically if we add the directory where we build the linter binary to the path for thegolangci-lint run
command.