golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.89k stars 754 forks source link

file on save feature makes working with go templates in vscode virtually impossible when generating go code #2975

Closed plastikfan closed 1 year ago

plastikfan commented 1 year ago

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.19.11 linux/amd64 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - Build info ---------- golang.org/x/tools/gopls v0.13.2 golang.org/x/tools/gopls@v0.13.2 h1:Pyvx6MKvatbX3zzZmdGiFRfQZl0ohPlt2sFxO/5j6Ro= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/exp@v0.0.0-20220722155223-a9213eeb770e h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA= golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/mod@v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= golang.org/x/sync@v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= golang.org/x/sys@v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= golang.org/x/text@v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4= golang.org/x/tools@v0.11.2-0.20230810185051-cc6b5804b8cf h1:Oush7UwPamr2/iNeNFBuNFj89YyHn0YY69EKDdvANnk= golang.org/x/vuln@v0.0.0-20230110180137-6ad3e3d07815 h1:A9kONVi4+AnuOr1dopsibH6hLi1Huy54cbeJxnq4vmU= honnef.co/go/tools@v0.4.2 h1:6qXr+R5w+ktL5UkwEbPp+fEvfyoMPche6GkOpGHZcLc= mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc= go: go1.19.11 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - 1.82.1 6509174151d557a81c9d0b5f8a5a1e9274db5585 x64 * Check your installed extensions to get the version of the VS Code Go extension - v0.39.1 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. - Checking configured tools.... GOBIN: undefined toolsGopath: gopath: /home/plastikfan/go GOROOT: /usr/local/go PATH: /home/plastikfan/.vscode-server/bin/6509174151d557a81c9d0b5f8a5a1e9274db5585/bin/remote-cli:/home/plastikfan/.nvm/versions/node/v18.13.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Program Files/ImageMagick-7.1.1-Q16-HDRI:/mnt/c/Program Files (x86)/Intel/iCLS Client/:/mnt/c/Program Files/Intel/iCLS Client/:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/DAL:/mnt/c/Program Files/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files (x86)/Intel/Intel(R) Management Engine Components/IPT:/mnt/c/Program Files/Microsoft VS Code/bin:/mnt/c/Program Files/Git/cmd:/mnt/c/Program Files/GitHub CLI/:/mnt/c/Program Files (x86)/oh-my-posh/bin:/mnt/c/Shared/go/bin:/mnt/c/Program Files/PowerShell/7/:/mnt/c/Users/Plastikfan/AppData/Local/Microsoft/WindowsApps:/mnt/c/Users/Plastikfan/AppData/Local/Programs/oh-my-posh/bin:/mnt/c/Users/Plastikfan/go/bin:/home/plastikfan/.local/bin:/usr/local/go/bin:/home/plastikfan/go/bin go: /usr/local/go/bin/go: go version go1.19.11 linux/amd64 gotests: /home/plastikfan/go/bin/gotests (version: v1.6.0 built with go: go1.20) gomodifytags: /home/plastikfan/go/bin/gomodifytags (version: v1.16.0 built with go: go1.20) impl: /home/plastikfan/go/bin/impl (version: v1.1.0 built with go: go1.20) goplay: /home/plastikfan/go/bin/goplay (version: v1.0.0 built with go: go1.20) dlv: /home/plastikfan/go/bin/dlv (version: v1.20.1 built with go: go1.20) golangci-lint: /home/plastikfan/go/bin/golangci-lint (version: (devel) built with go: go1.20.2) gopls: /home/plastikfan/go/bin/gopls (version: v0.13.2 built with go: go1.19.11) go env Workspace Folder (cobrass): /home/plastikfan/dev/github/go/snivilised/cobrass GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/home/plastikfan/.cache/go-build" GOENV="/home/plastikfan/.config/go/env" GOEXE="" GOEXPERIMENT="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="linux" GOINSECURE="" GOMODCACHE="/home/plastikfan/go/pkg/mod" GONOPROXY="" GONOSUMDB="" GOOS="linux" GOPATH="/home/plastikfan/go" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/go" GOSUMDB="sum.golang.org" GOTMPDIR="" GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64" GOVCS="" GOVERSION="go1.19.11" GCCGO="gccgo" GOAMD64="v1" AR="ar" CC="gcc" CXX="g++" CGO_ENABLED="1" GOMOD="/home/plastikfan/dev/github/go/snivilised/cobrass/go.mod" GOWORK="" 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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1568779222=/tmp/go-build -gno-record-gcc-switches"

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.

Describe the bug

I have tried to solicit help with this issue (see: vscode go discussion)

I am trying to compose quite a complicated suite of go templates (used to generate go code), but the format on save feature invoked by vscode/go-extension makes this a difficult undertaking. A couple of points I need to mention first are:

  "files.associations": {
    "**/*.tmpl": "go"
  }

However, there is a really big problem now (possibly because of the go file association). Consider the following template definition:

package assistant

import (
    "net"
    "time"

    "github.com/spf13/pflag"
)

// ----> auto generated(Build-Validators/gen-ov)

The purpose of this template is to provide static header content for a go source file to be generated. I also have a footer. You will notice that there is no go template syntax here, that is because the header (and the footer), have static content. The dynamic content is in another file.

Whenever I go to save the above file (within the go project), the format on save feature is activated that proceeds to remove the import statement (as there appears to be no other content that is making use of these imports), breaking my code generation. The dynamic content I mentioned before relies on these imports. There needs to be a way to bypass format on save for a specific file type, namely in my case .go.tmpl. When using go templates to generate code into the file system within the go module, there need to be exclusions applied to the go extension that help to take account of this. I do have a very awkward workaround, but really it would be nice if the user could apply exceptions to the format on save. I clearly, still need format on save for my regular go development, so turning it off is not an option.

When I look at the vscode settings, there is only a single option: Editor:Format On Save, which allows to to turn on or off the feature. Clearly, I do not want to turn off this feature, for the relative minor requirement of go template file editing, rather, I would have preferred if there was an option to limit this feature to .go file types only, or allow other suffix exclusions.

Thanks.

Steps to reproduce the behavior:

  1. create a go template file as indicated previously (with a suffix, that does not end in .go)
  2. create an import statement, without making use of the imports
  3. Save the file and see the import statement be removed

Screenshots or recordings

If applicable, add screenshots or recordings to help explain your problem.

findleyr commented 1 year ago

Hi, is it feasible for you to add the standard // Code generated by <tool>. DO NOT EDIT. (documentation) comment to your files? If present, gopls will refuse to format the file, which I believe would resolve your issue.

gopherbot commented 1 year ago

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

plastikfan commented 1 year ago

Hi, is it feasible for you to add the standard // Code generated by <tool>. DO NOT EDIT. (documentation) comment to your files? If present, gopls will refuse to format the file, which I believe would resolve your issue.

Hi @findleyr, thanks for that nugget of wisdom. I never knew you could do this. I actually found another work-around that resolved my issue and that was to embed that content inside of a go template, so that example I gave in my original request became this:

{{- define "option-validator-auto-header.go.tmpl" -}}
package assistant

import (
  "net"
  "time"

  "github.com/spf13/pflag"
)

// ----> auto generated(option-validator-auto/gen-ov)

{{ end -}}

which is not subject to auto formatting.

Thanks anways, and I'll heed that advice about using that DO NOT EDIT comment.