fatih / vim-go

Go development plugin for Vim
https://www.patreon.com/bhcleek
Other
15.98k stars 1.45k forks source link

GoDiagnostics cannot determine package name if the package only has a test file that uses build tags #3562

Closed alcortesm closed 1 year ago

alcortesm commented 1 year ago

What did you do? (required: The issue will be closed when not provided)

I ran :GoDiagnostics on a Go test file. The file has a build tag and the package doesn't have any other go files.

Steps to reproduce, starting on an empty directory:

$ go mod init foo
$ mkdir foo
$ cd foo
$ cat << EOF > foo_test.go
//go:build integration

package foo_test

import "testing"

func TestHelloWorld(t *testing.T) {
        t.Fatal("not implemented")
}
EOF
$ vim foo_test.go
:GoBuildTags integration
:GoDiagnostics

What did you expect to happen?

Vim will run the diagnostics in the file and retun success:

vim-go: [diagnostics] PASS

What happened instead?

A vim error:

vim-go: could not determine package name

Configuration (MUST fill this out):

$ cat ~/.vim/vimrc
call plug#begin('~/.vim/plugged')
Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' }
call plug#end()

vim-go version:

I guess v1.28, since it is the current release and I'm installing vim-go using vim-plug, and I run :PlugUpdate prior to this report.

But how can I know for sure? maybe it would be good to have a :GoVersion command that returns the vim-go version.

vimrc you used to reproduce:

vimrc ```vim call plug#begin('~/.vim/plugged') Plug 'fatih/vim-go', { 'do': ':GoUpdateBinaries' } call plug#end() ```

Vim version (first three lines from :version):

:version
VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 04 2023 10:24:44)
Included patches: 1-1378, 1499
Modified by team+vim@tracker.debian.org

Go version (go version):

$ go version
go version go1.20 linux/amd64

Go environment

go env Output:


```
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/alcortes/.cache/go-build"
GOENV="/home/alcortes/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/alcortes/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/alcortes/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/alcortes/opt/go/go1.20"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/alcortes/opt/go/go1.20/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/tmp/blah/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3522321768=/tmp/go-build -gno-record-gcc-switches"
```

gopls version

gopls version Output:

$ gopls version
golang.org/x/tools/gopls v0.12.4
    golang.org/x/tools/gopls@v0.12.4 h1:nce5etAamR46d9oNGxop1aRK5rDQ0NqcY/SHIcyfEKY=

My Own Investigation

If I add a simple regular Go file to the package, the :GoDiagnostics starts working fine again:

$ cat << EOF > foo.go
package foo
EOF
$ vim foo_test.go
:GoBuildTags integration
:GoDiagnostics
vim-go: [diagnostics] PASS

If I remove the non-test go file, :GoDiagnostics fails again.

$ rm foo.go
$ vim foo_test.go
:GoBuildTags integration
:GoDiagnostics
vim-go: could not determine package name
bhcleek commented 1 year ago

Build constraints effectively hide the Go file from the go tool and many related tools when the constraints are not satisfied with build tags. With only one file and that file has a build constraint, but not tags are being used, that effectively tells the tooling that there are no Go files to check.

But how can I know for sure? maybe it would be good to have a :GoVersion command that returns the vim-go version.

That's not quite as easy as you might think for a variety of reasons. What you've provided here is enough to know the version of vim-go that you're using.

alcortesm commented 1 year ago

thanks!