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/gopls: don't run gofmt on generated files #49555

Closed
gudvinr opened this issue Nov 6, 2021 · 5 comments
Closed

x/tools/gopls: don't run gofmt on generated files #49555

gudvinr opened this issue Nov 6, 2021 · 5 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@gudvinr
Copy link

gudvinr commented Nov 6, 2021

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
    • go version go1.17.3 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • golang.org/x/tools/gopls v0.7.3
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
    • 1.61.0 (x64)
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.29.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
    • gopkgs: /home/gudvin/tools/go/path/bin/gopkgs: go1.17.1
      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"] or gopls prefixes.

{
    "editor.formatOnPaste": false,
    "editor.formatOnSave": false,
    "go.toolsManagement.autoUpdate": true,
    "go.vetOnSave": "workspace",
    "go.lintOnSave": "workspace",
    "go.toolsGopath": "~/path/to/go/",
    "gopls": {
        "build.experimentalWorkspaceModule": false,
        "ui.documentation.linksInHover": false,
        "usePlaceholders": false,
        "analyses": {
            "composites": false,
            "shadow": true,
            "ST1000": false,
            "ST1003": false
        },
        "ui.diagnostic.staticcheck": true
    }
}

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:

  1. Open generated file
  2. Press Ctr+S
@hyangah
Copy link
Contributor

hyangah commented Nov 8, 2021

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.

@gudvinr
Copy link
Author

gudvinr commented Nov 8, 2021

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.

@hyangah hyangah changed the title Don't run gofmt on generated files x/tools/gopls: don't run gofmt on generated files Nov 12, 2021
@hyangah hyangah transferred this issue from golang/vscode-go Nov 12, 2021
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 12, 2021
@gopherbot gopherbot added this to the Unreleased milestone Nov 12, 2021
@findleyr
Copy link
Contributor

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.

@findleyr findleyr modified the milestones: Unreleased, gopls/unplanned Nov 18, 2021
@findleyr findleyr added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. help wanted NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Nov 18, 2021
ShoshinNikita added a commit to ShoshinNikita/tools that referenced this issue Nov 18, 2021
ShoshinNikita added a commit to ShoshinNikita/tools that referenced this issue Nov 18, 2021
@gopherbot
Copy link

Change https://golang.org/cl/365295 mentions this issue: internal/lsp/source: don't format generated files

@findleyr
Copy link
Contributor

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?

ShoshinNikita added a commit to ShoshinNikita/tools that referenced this issue Dec 3, 2021
@golang golang locked and limited conversation to collaborators Dec 15, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. help wanted NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants