Skip to content
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

x/tools/analysis: printf pass should only allow %w in fmt.Errorf #37792

Closed
GiedriusS opened this issue Mar 11, 2020 · 2 comments
Closed

x/tools/analysis: printf pass should only allow %w in fmt.Errorf #37792

GiedriusS opened this issue Mar 11, 2020 · 2 comments
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@GiedriusS
Copy link

Seems like currently all functions which are called Errorf are created in the same way: https://github.com/golang/tools/blob/aafaee8bce8c38f7ce09907ff7430abcb6f58132/go/analysis/passes/printf/printf.go#L493-L495. However, only fmt.Errorf accepts %w in the standard library.

The way it is causes problems with exceptions to this rule. One notable one is github.com/pkg/errors. There %w doesn't make sense in the call to errors.Errorf: https://godoc.org/github.com/pkg/errors#Errorf.

Consider making that check more specific and treat fmt.Errorf differently from regular functions called Errorf.

I guess golang/tools@1d17272 should be extended.

What version of Go are you using (go version)?

$ go version
go version go1.13.7 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"              
GOARCH="amd64"                                       
GOBIN=""                                                                                                  
GOCACHE="/home/gstatkevicius/.cache/go-build"                                                             
GOENV="/home/gstatkevicius/.config/go/env"                                                                                                                                                                           
GOEXE=""                                                                                                  
GOFLAGS=""                         
GOHOSTARCH="amd64"
GOHOSTOS="linux"                              
GONOPROXY=""                                
GONOSUMDB=""                   
GOOS="linux"                   
GOPATH="/home/gstatkevicius/go"
GOPRIVATE=""  
GOPROXY="https://goproxy.io"
GOROOT="/usr/local/go"                       
GOSUMDB="sum.golang.org"                  
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"     
AR="ar"         
CC="gcc"    
CXX="g++"   
CGO_ENABLED="1"
GOMOD="/dev/null"              
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""             
CGO_CXXFLAGS="-g -O2" 
CGO_FFLAGS="-g -O2"     
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"                       
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build587596761=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Wrote a program that uses github.com/pkg/errors.Errorf and used the %w verb in there. Ran go vet on it and expected the printf analysis to report errors but there were none.

What did you expect to see?

Errors that %w verb is unsupported with that function.

What did you see instead?

Success of go vet.

@gopherbot gopherbot added this to the Unreleased milestone Mar 11, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Mar 11, 2020
@smasher164
Copy link
Member

I do not see this behavior with my version of vet. Are you sure you are running the latest version of vet?

main.go

package main

import "github.com/pkg/errors"

func main() {
	_ = errors.Errorf("%w", nil)
}

I see

./main.go:6:6: Errorf call has error-wrapping directive %w

@smasher164 smasher164 added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Mar 11, 2020
@GiedriusS
Copy link
Author

I do not see this behavior with my version of vet. Are you sure you are running the latest version of vet?

main.go

package main

import "github.com/pkg/errors"

func main() {
	_ = errors.Errorf("%w", nil)
}

I see

./main.go:6:6: Errorf call has error-wrapping directive %w

Yes, you are correct - I should've made a minimal test case and have tested it on the newest version before creating this. I created this issue because I couldn't remember anything from the change-log of Go about this and only quickly glanced at the source code. As it is evident now, I have missed some crucial things about it. As reported in the original issue, I have tried this on 1.13.x and got no such error. Updated now myself and got the same error that you've pasted. Thanks for the quick response and sorry for the noise. I guess it's one more reason to update to 1.14.

@golang golang locked and limited conversation to collaborators Mar 11, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Tools This label describes issues relating to any tools in the x/tools repository. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants