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/go/analysis/passes/fieldalignment/cmd/fieldalignment: ignore generated files #60509

Open
ainar-g opened this issue May 30, 2023 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@ainar-g
Copy link
Contributor

ainar-g commented May 30, 2023

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

$ go version
go version go1.20.4 linux/amd64
$ go version -m $( which fieldalignment )
…/bin/fieldalignment: go1.20.4
        path    golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment
        mod     golang.org/x/tools      v0.8.1-0.20230421161920-b9619ee54b47    h1:fQlOhMJ24apqitZX8S4hbCbHU1Z9AvyWkN3BYI55Le4=
        dep     golang.org/x/mod        v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=
        dep     golang.org/x/sys        v0.7.0  h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
        build   -buildmode=exe
        build   -compiler=gc
        build   CGO_ENABLED=1
        build   CGO_CFLAGS=
        build   CGO_CPPFLAGS=
        build   CGO_CXXFLAGS=
        build   CGO_LDFLAGS=
        build   GOARCH=amd64
        build   GOOS=linux
        build   GOAMD64=v1

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="/home/ainar/.cache/go-build"
GOENV="/home/ainar/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ainar/go/pkg/mod"
GONOPROXY="REMOVED"
GONOSUMDB="REMOVED"
GOOS="linux"
GOPATH="/home/ainar/go"
GOPRIVATE="REMOVED"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/ainar/go/go1.20"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ainar/go/go1.20/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.4"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="REMOVED"
GOWORK="REMOVED"
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3548065178=/tmp/go-build -gno-record-gcc-switches"

What did you do?

fieldalignment /path/to/pkg/

(The package containing generated code in generated.pb.go.)

What did you expect to see?

Nothing, exit code 0.

What did you see instead?

/path/to/pkg/generated.pb.go:25:16: struct of size 104 could be 96
/path/to/pkg/generated.pb.go:96:14: struct with 168 pointer bytes could be 144
[…]

Exit code 3.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label May 30, 2023
@gopherbot gopherbot added this to the Unreleased milestone May 30, 2023
@mknyszek
Copy link
Contributor

@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label May 30, 2023
@timothy-king
Copy link
Contributor

My intuition is that a lot of users would have the opposite desire from cmd/fieldalignment, i.e. 'I definitely expect to reports from fieldalignment in generated files'. They would want to be warned on alignment issues on files they may never have looked at. So I think reporting in generated files is probably the right default here, but we can consider mechanisms for optionally suppressing some reports.

I am slightly unsure about the technical feasibility. IIUC whether a file is generated is not a part of the x/tools/go/packages output. Is there an existing Go wide convention that is encoded in the files to let us know whether a file is considered "generated"?

We could get this from the user, but at the point that is roughly equivalent to just having a flag for which files to exclude from reports. FWIW it is relatively straightforward to postprocess the -json output to exclude files. So it is unclear if we x/tools/go/analysis really is the thing to be updated.

@ainar-g
Copy link
Contributor Author

ainar-g commented May 31, 2023

A lot of people use code generators they have little control over, protoc-gen-go being the most obvious example. So yes, having an option to either ignore all generated files or a per-file ignore option would be nice to have.

Is there an existing Go wide convention that is encoded in the files to let us know whether a file is considered "generated"?

I'm not sure if I understand the question correctly, but the convention is documented in go help generate:

To convey to humans and machine tools that code is generated,
generated source should have a line that matches the following
regular expression (in Go syntax):

        ^// Code generated .* DO NOT EDIT\.$
This line must appear before the first non-comment, non-blank
text in the file.

This is to be added in Go 1.21 with go/ast.IsGenerated, and there is an analyzer available in Staticcheck's repo: https://github.com/dominikh/go-tools/blob/bc668a17ee05a79bb9fdc320018de7503f0d9646/analysis/facts/generated/generated.go.

@timothy-king
Copy link
Contributor

This is to be added in Go 1.21 with go/ast.IsGenerated,

TIL! Great to know there is a standard. (This function would need to be copied for x/tools to use this until after the release of 1.23. Nothing here does not look copiable though.) Given that there is a standard, a bool flag for the analyzer to disable reports for generated files seems fairly reasonable.

I think there is a question of whether such a flag should be on a checker or if it should be generalized to the driver level, e.g. unitchecker, singlechecker or gopls. @adonovan @findleyr thoughts?

@adonovan
Copy link
Member

adonovan commented May 31, 2023

TIL! Great to know there is a standard. (This function would need to be copied for x/tools

In fact, this function evolved from the version in x/tools (see gopls/internal/lsp/source/util.go) though the go/ast version is much better (in large part due to @dmitshur's careful review).

whether such a flag should be on a checker or if it should be generalized to the driver level

Applying edits is certainly a driver responsibility. Our drivers should not apply edits to generated files, or, for that matter, to read-only files such as those in the module cache, nor apply edits twice due to file-system level aliasing such as symbolic and hard links.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants