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

flag: Error wrapping is missing from the flag package #55097

Closed
VoyTechnology opened this issue Sep 15, 2022 · 6 comments
Closed

flag: Error wrapping is missing from the flag package #55097

VoyTechnology opened this issue Sep 15, 2022 · 6 comments

Comments

@VoyTechnology
Copy link

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

$ go version
go version go1.19.1 darwin/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=""
GOARCH="amd64"
GOBIN="/Users/voy/.go/bin"
GOCACHE="/Users/voy/Library/Caches/go-build"
GOENV="/Users/voy/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/voy/.go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/voy/.go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/Cellar/go/1.19.1/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.19.1/libexec/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.19.1"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/voy/.go/src/REDACTED/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/var/folders/sj/5vkqw5217xn221ywb9d95bzc0000gn/T/go-build832936302=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://go.dev/play/p/SVo8u-vFr3T

What did you expect to see?

No output as the underlying error is the same.

What did you see instead?

The errors do not match as the error returned from Value.Set is not wrapped using %w in (*FlagSet).parseOne

return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)

@ianlancetaylor
Copy link
Contributor

That's not how errors.Is works. errors.Is doesn't report whether an error prints the same. It reports whether the error is the same. errors.Is will never report true when called with an error returned from the flag package and an error that you've defined yourself. See the docs for errors.New: "Each call to New returns a distinct error value even if the text is identical."

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 16, 2022
@VoyTechnology
Copy link
Author

Hi ianlancetaylor, thank you for your response. I am aware that the errors.Is does not compare the error string representation. The problem is that the error returned by Set is not wrapped in the error chain by the flag package. As shown in

return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)

I would expect that the error was wrapped by using %w rather than converted into a string using %v.

@D1CED
Copy link

D1CED commented Sep 16, 2022

Consider opening a new issue requesting that the flag package shall return a new kind of error in case a flag.Value.Set returns an error so that one is able to inspect it.

@ianlancetaylor
Copy link
Contributor

Today I can't see any reason for the flag package to use %w, because the wrapped error is not exported.

@seankhliao
Copy link
Member

There are cases like flag.Func where the user can return an error that's later surfaced through flag.Parse.
Wrapping those errors would allow users to test for their own validation errors.

@tsuna
Copy link
Contributor

tsuna commented Sep 19, 2022

Isn't the code sample posted by OP on the Go Playground another example of a user-controller error type returned eventually through flag.Parse()?

@golang golang locked and limited conversation to collaborators Sep 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants