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

cmd/gofmt: should not update tool generated files #73181

Closed
firefart opened this issue Apr 5, 2025 · 8 comments
Closed

cmd/gofmt: should not update tool generated files #73181

firefart opened this issue Apr 5, 2025 · 8 comments
Labels
ToolProposal Issues describing a requested change to a Go tool or command-line program.

Comments

@firefart
Copy link

firefart commented Apr 5, 2025

Go version

1.24.2

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/firefart/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/firefart/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1818138722=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/home/firefart/code/xxx/go.mod'
GOMODCACHE='/home/firefart/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/firefart/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/firefart/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.24.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I have a lot of tool generated code in my project directory. If I run go fmt ./... it also touches these files

What did you see happen?

go fmt ./... also updates all generated files

What did you expect to see?

Go fmt should not touch auto generated files. According to https://pkg.go.dev/cmd/go#hdr-Generate_Go_files_by_processing_source these files should start with a string matching the regex ^// Code generated .* DO NOT EDIT\.$.

I think go fmt should not touch files matching this regex as they are generated by tools.

@mateusz834 mateusz834 changed the title go fmt should not update tool generated files cmd/gofmt: should not update tool generated files Apr 5, 2025
@mateusz834
Copy link
Member

I had it in the back of my mind to open a similar issue, notably this is necessary for generated code that generates //line directives. If such code is formatted, then //line directives might get moved, thus get out of sync. I somehow managed (in my project) to generate line directives in a way that in most cases would not get moved after another format, but i guess it is not always that easy to do so.

FYI: Gopls already does this #49555.

What about this case:

go fmt file.go

I think then is should format, as the user explicitly asked so (i think?)

@mateusz834
Copy link
Member

I think go fmt should not touch files matching this regex as they are generated by tools.

FYI: https://pkg.go.dev/go/ast#IsGenerated

@mateusz834
Copy link
Member

I have a lot of tool generated code in my project directory. If I run go fmt ./... it also touches these files

Does your tool generate source with //line directives? It's hard for me to imagine why a tool would not format a file when it does not use //line directives though. Or maybe there are other reasons for which it cannot format the file while generating. I am curious.

@gabyhelp gabyhelp added the ToolProposal Issues describing a requested change to a Go tool or command-line program. label Apr 5, 2025
@seankhliao
Copy link
Member

See #55157. We expect generated code to be formatted.
gopls ignores it because you may not want to change a file while looking at it, but we wouldn't want to block users from formatting code explicitly with go fmt.

@firefart
Copy link
Author

firefart commented Apr 5, 2025

@seankhliao @mateusz834 The tool that generates the code is https://github.com/microsoft/kiota . This generates code based on an openapi spec (for example https://github.com/microsoftgraph/msgraph-sdk-go). As it's based on dotnet and generates go code, there is no way to do a go fmt run after generation without calling out to a separate process. In my case I use kiota to generate a sdk based on a subset of microsoft graph api calls, as the https://github.com/microsoftgraph/msgraph-sdk-go package includes everything from the api and takes ages to build and eats a lot of memory.
Since the latest version it also includes a generated string at the top ( microsoft/kiota#6368 ) which helps with a lot of tools in the pipeline, but go fmt still needs to be handled differently as it tries to fmt these files.
The current workaround is to exlude the generated code like go fmt $(go list ./... | grep -v /internal/kiota)

@mateusz834
Copy link
Member

As it's based on dotnet and generates go code, there is no way to do a go fmt run after generation without calling out to a separate process

Thanks, but i still believe that such code should be indeed formatted. I think after every code generation using this tool you can just run go fmt ./..., right?

@seankhliao
Copy link
Member

In that case, we expect you to run gofmt as part of your code generation pipeline. it's only 3s for the entire msgraph-sdk-go repo.
Looking at the diffs, it's pretty much only 4 spaces instead of tabs, and some unnecessary spaces / semicolons. kiota should be able to fix those.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ToolProposal Issues describing a requested change to a Go tool or command-line program.
Projects
None yet
Development

No branches or pull requests

4 participants