Moved a code base from dep to go mods. Opened the code and then attempted to use GoDef on a structure to go to the definition of the struct in the same package.
Vim should open the buffer of the struct we are trying to get the definition for.
I receive an error in the status bar:
vim-go: [searching declaration] FAIL
vim-go version:
master
vimrc you used to reproduce (use a minimal vimrc with other plugins disabled; do not link to a 2,000 line vimrc):
Vim version (first three lines from :version):
NVIM v0.3.1
Build type: Release
LuaJIT 2.0.5
Go version (go version):
go version go1.11.4 darwin/amd64
Go environment (go env):
GOARCH="amd64"
GOBIN="/Users/louis/git/go/bin"
GOCACHE="/Users/louis/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/louis/git/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/louis/git/paperspace/ps_dc/go.mod"
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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/ld/r2llmym51mz9khb8h_3wvrcr0000gp/T/go-build775485816=/tmp/go-build -gno-record-gcc-switches -fno-common"
Have you run :GoUpdatBinaries godef?
@bhcleek
vim-go: Updating godef. Reinstalling github.com/rogpeppe/godef to folder /Users/louis/git/go/bin
vim-go: updating finished!
Still the same issue, have my curser over struct type in method signature, go to use "GoDef" and receive:
vim-go: [searching declaration] FAIL
Also here is my .vimrc with vim-go config section
```
" vim-go configuration
" let g:go_gocode_propose_source = 0 " parse binary for code completion
let g:go_disable_autoinstall = 0
let g:go_highlight_functions = 1
let g:go_highlight_structs = 1
let g:go_highlight_operators = 1
let g:go_highlight_methods = 1
let g:go_highlight_fields = 1
let g:go_highlight_types = 1
let g:go_highlight_operators = 1
let g:go_highlight_build_constraints = 1
let g:go_highlight_build_constraints = 1
let g:go_term_enabled = 1
let g:go_term_mode = "split"
let g:go_term_height = 13
" let g:go_metalinter_autosave = 1
let g:go_metalinter_deadline = "5s"
let g:neocomplete#enable_at_startup = 1
let g:go_fmt_command = "goimports"
let g:go_list_type = "quickfix"
let g:go_test_timeout = "600s"
let g:go_decls_mode = 'fzf'
let g:go_test_show_name = 1
let g:go_fmt_experimental = 1
````
This maybe relevant also:
" vim-go key bindings and autocommands
augroup go
autocmd!
" emulate autobuild
au bufwritepost *.go silent! :GoInstall
I did this because go-code stopped building from package cache. Maybe this causes an issue ?
You're using guru, then, not godef. Guru isn't aware of modules.
hmmm, I changed my config too:
let g:go_def_mode = 'godef'
and now I get:
vim-go: godef: There must be at least one package that contains the file
Is your cwd within the module directory?
Not exactly, we use a monorepo so my cwd is:
/git/paperspace/PS_DC
While the code I'm building is in
/git/paperspace/PS_DC/docker-agent/main.go
@bhcleek, seems like you got it, if I CD to /docker-agent/ then it works correctly. Any nice way around this?
Not as-is. vim-go would have to be modified to change to the directory temporarily by changing https://github.com/fatih/vim-go/blob/master/autoload/go/def.vim#L22-L28 to use go#tool#ExecuteInDir instead of go#util#Exec.
@bhcleek think this is optimal use case? Or do you have hesitations around that change?
I don't have any hesitation around that change; it's the right thing to do. If you want to take a crack at it, I'll gladly review the PR. Note that go#tool#ExecuteInDir will have to be extended to accept an optional second argument. Let me know if you want to pick it up; otherwise, I can get to it this weekend.
@bhcleek
https://github.com/fatih/vim-go/pull/2150
I have not written vimscript before so let me know if anything is off.
I also got this error in the eclipse IDE while using go modules in the project: "godef there must be at least one package that contains the file".
golang 1.12.6
any idea how to fix it?
i have switch to guru, and find guru is also fast. and the godef 's problem not sovled
I have the same problem: I've created a sample Cobra app using
cobra init myCobraApp --pkg-name=github.com/khpeek/myCobraApp
which has the following structure:
.
├── LICENSE
├── cmd
│  └── root.go
└── main.go
where main.go reads
package main
import "github.com/khpeek/myCobraApp/cmd"
func main() {
cmd.Execute()
}
and cmd/root.go reads
/*
Copyright © 2019 NAME HERE <EMAIL ADDRESS>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/viper"
)
var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "myCobraApp",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
}
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.myCobraApp.yaml)")
// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" {
// Use config file from the flag.
viper.SetConfigFile(cfgFile)
} else {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Search config in home directory with name ".myCobraApp" (without extension).
viper.AddConfigPath(home)
viper.SetConfigName(".myCobraApp")
}
viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}
However, when I right-click on Execute() in main.go and select "Go To Definition", I get "No definition found":

I've tried using godef from the command line like so:
kurt@Kurts-MacBook-Pro-13 ~/g/s/g/k/myCobraApp> godef -f main.go 'cmd.Execute()'
godef: There must be at least one package that contains the file
I don't understand this "There must be at least one package that contains the file" error message; isn't the file in the main package?
same problem here
switched to let g:go_def_mode = 'gopls' and everything works
Most helpful comment
hmmm, I changed my config too:
let g:go_def_mode = 'godef'and now I get: