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/gopls: don't run gofmt on generated files #49555
Comments
My personal opinion is a better approach is to fix the code generator to format the go code. But we will discuss this in our triage meeting. |
Well, you can't fix all code generators in the world so it isn't really a solution. Generated code isn't supposed to be edited anyway and it really has nothing to do with formatting. It could be deliberate choice even. Code generation shouldn't be pretty and in many cases generated code will contain some weird tricks to boost performance, optimize memory layout, etc. So even in formatted form it might be quite ugly looking. Even if generated code is poorly formatted editor shouldn't violate "DO NOT EDIT" rule without explicit command from user. It could mess up git diff very badly, for example. And you can't do anything to prevent that except by disabling formatting for every go file. |
I don't disagree, but @hyangah thinking about this a bit more I'm wondering if gopls is the correct layer to solve this problem. It's a bit strange for vscode-go to ask gopls to format a file, only to have gopls refuse. With that said, it is probably easiest to implement this behavior in gopls. Putting this in the backlog for now, as I don't think we have resources to prioritize this for the next minor release. |
Change https://golang.org/cl/365295 mentions this issue: |
Well @ShoshinNikita picked this up -- thanks! @hyangah in that CL, we're returning nil error. I feel like it would be better to surface an error, but will that disrupt vscode? |
What version of Go, VS Code & VS Code Go extension are you using?
Version Information
go version
to get version of Go from the VS Code integrated terminal.gopls -v version
to get version of Gopls from the VS Code integrated terminal.code -v
orcode-insiders -v
to get version of VS Code or VS Code Insiders.Go: Locate Configured Go Tools
command.go-outline: /home/gudvin/tools/go/path/bin/go-outline: go1.17.1
gotests: /home/gudvin/tools/go/path/bin/gotests: go1.17.1
gomodifytags: /home/gudvin/tools/go/path/bin/gomodifytags: go1.17.1
impl: /home/gudvin/tools/go/path/bin/impl: go1.17.1
goplay: not installed
dlv: /home/gudvin/tools/go/path/bin/dlv: go1.17.1
dlv-dap: /home/gudvin/tools/go/path/bin/dlv-dap: go1.17.1
gopls: /home/gudvin/tools/go/path/bin/gopls: go1.17.1
Share the Go related settings you have added/edited
Run
Preferences: Open Settings (JSON)
command to open your settings.json file.Share all the settings with the
go.
or["go"]
orgopls
prefixes.Describe the bug
When save action is called with generated file open in editor it then become formatted.
Generated code has comment matched by this regex:
^// Code generated .* DO NOT EDIT\.$
In fact, extension warns that file should not be edited. But then proceeds to edit it by itself.
Steps to reproduce the behavior:
The text was updated successfully, but these errors were encountered: