Skip to content

x/tools/cmd/goimports: return non-zero exit code if there's an offense #39032

@khos2ow

Description

@khos2ow

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

$ go version
go version go1.14.2 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=""
GOARCH="amd64"
GOBIN=""
GOCACHE="~/.cache/go-build"
GOENV="~/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="~/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/lib/go/go-latest"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/lib/go/go-latest/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="~/go/path/to/project/go.mod"
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-build335622656=/tmp/go-build -gno-record-gcc-switches"

What did you do?

This is more of a feature request (or question) rather than bug reporting. I'm trying to check formatting of go files with goimports which works as expected, but if there's any offensive file it always exit with code 0. As a result I need to hack around with tee and similar tools to manually fail my make checkfmt target on CI.

I was wondering if this was by design or any specific reasons not to return non-zero exit code on list (-l). I briefly looked around and it seems there might be a really easy way to add this, something like adding:

if *list {
	fmt.Fprintln(out, filename)
+	exitCode = 1 # or any other reasonable exit code
}

right after line 156, here:

https://github.com/golang/tools/blob/a4cde3673cf4d442ef00a2df0d7a6f707d9e726b/cmd/goimports/goimports.go#L155-L157

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.ToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions