golang / vscode-go

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

"Go: Generate Unit Tests For Function" does not handle generics #3425

Closed haraldrudell closed 1 week ago

haraldrudell commented 1 week 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.22.3 darwin/arm64 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - golang.org/x/tools/gopls v0.15.3 golang.org/x/tools/gopls@v0.15.3 h1:zbdOidFrPTc8Bx0YrN5QKgJ0zCjyGi0L27sKQ/bDG5o= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/mod@v0.15.0 h1:SernR4v+D55NyBH2QiEQrlBAnj1ECL6AGrA5+dPaMY8= golang.org/x/sync@v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= golang.org/x/telemetry@v0.0.0-20240209200032-7b892fcb8a78 h1:vcVnuftN4J4UKLRcgetjzfU9FjjgXUUYUc3JhFplgV4= golang.org/x/text@v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/tools@v0.18.1-0.20240412183611-d92ae0781217 h1:uH9jJYgeLCvblH0S+03kFO0qUDxRkbLRLFiKVVDl7ak= golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/tools@v0.4.6 h1:oFEHCKeID7to/3autwsWfnuv69j3NsfcXbvJKuIcep8= mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo= mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8= go: go1.22.1 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - 1.90.0 * Check your installed extensions to get the version of the VS Code Go extension - Go v0.41.4 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. - # Tools Configuration ## Environment GOBIN: undefined toolsGopath: gopath: /Users/foxyboy/go GOROOT: /opt/homebrew/Cellar/go/1.22.3/libexec PATH: /Users/foxyboy/bin:/Users/foxyboy/go/bin:/opt/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/foxyboy/Library/Android/sdk/platform-tools:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin ## Tools go: /opt/homebrew/bin/go: go version go1.22.3 darwin/arm64 gopls: /Users/foxyboy/go/bin/gopls (version: v0.15.3 built with go: go1.22.1) gotests: /Users/foxyboy/go/bin/gotests (version: v1.6.0 built with go: go1.22.0) gomodifytags: /Users/foxyboy/go/bin/gomodifytags (version: v1.16.0 built with go: go1.22.0) impl: /Users/foxyboy/go/bin/impl (version: v1.1.0 built with go: go1.22.0) goplay: /Users/foxyboy/go/bin/goplay (version: v1.0.0 built with go: go1.22.0) dlv: /Users/foxyboy/go/bin/dlv (version: v1.22.0 built with go: go1.22.0) staticcheck: /Users/foxyboy/go/bin/staticcheck (version: v0.4.6 built with go: go1.22.0) ## Go env Workspace Folder (parl): /opt/sw/parl GO111MODULE='' GOARCH='arm64' GOBIN='' GOCACHE='/Users/foxyboy/Library/Caches/go-build' GOENV='/Users/foxyboy/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='arm64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/foxyboy/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/foxyboy/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/opt/homebrew/Cellar/go/1.22.3/libexec' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/opt/homebrew/Cellar/go/1.22.3/libexec/pkg/tool/darwin_arm64' GOVCS='' GOVERSION='go1.22.3' GCCGO='gccgo' AR='ar' CC='cc' CXX='c++' CGO_ENABLED='1' GOMOD='/opt/sw/parl/go.mod' GOWORK='/opt/sw/parl/go.work' CGO_CFLAGS='-O2 -g' CGO_CPPFLAGS='' CGO_CXXFLAGS='-O2 -g' CGO_FFLAGS='-O2 -g' CGO_LDFLAGS='-O2 -g' PKG_CONFIG='pkg-config' GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sq/0x1_9fyn1bv907s7ypfryt1c0000gn/T/go-build2682538169=/tmp/go-build -gno-record-gcc-switches -fno-common'

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

A clear and concise description of what the bug. A clear and concise description of what you expected to happen.

Steps to reproduce the behavior:

  1. In the below program, right-click the “return” line and select “Go: Generate Unit Test For Function”
  2. BUG: toaster: “No tests generated for ….go”

EXPECTED: a *_test.go file to be created

It has been requested by others to explain why tests are not generated, and the statement has been that gopls does not provide why it is not creating tests

Regardless, tests should be created

/*
© 2024–present Harald Rudell <harald.rudell@gmail.com> https://haraldrudell.github.io/haraldrudell/)
ISC License
*/

package parl

type AnyCount[T any] struct{}

func (a AnyCount[T]) Count() (count int) {
    return
}
adonovan commented 1 week ago

This VS Code command invokes the gotests command as a subprocess. The generateTests function in vscode-go/src/goGenerateTests.ts doesn't use the stderr value returned by child_process.execFile, so the explanation is discarded. It should at least be printed to the log.

hyangah commented 1 week ago

The following is the exact command the extension uses to call gotests, and the message shown in the prompt is the only output/feedback coming from the gotests tool for this specific case

$ gotests -w -only ^AnyCount[T]Count$ /Users/hakim/xx/main.go
No tests generated for /Users/hakim/xx/main.go

The pattern passed to the -only flag param is wrong, but also, gotests nor the extension's integration doesn't support generics.

Related: https://github.com/cweill/gotests/issues/165 Proposed plan: migrate the functionality to gopls - https://github.com/golang/go/issues/47479 and https://github.com/golang/vscode-go/issues/1594

hyangah commented 1 week ago

Thanks for the report @haraldrudell Closing since the root cause is in the upstream tool, and we hope to provide better error messages when gopls takes over the functionality.