Reputation: 57421
I have installed the Go extension (version 0.11.4) in Visual Studio Code on MacOS:
However, I find that the linter does not 'pick up' functions defined in the same package, but in different files. For example, if I create in the same directory a file foo.go
with
package foobar
import "fmt"
func main() {
fmt.Println(SayHello())
}
and a file bar.go
with
package foobar
func SayHello() string {
return "Hello, world!"
}
then in foo.go
I get a linter error that SayHello
is an undeclared name
:
I've read about a similar issue here (https://github.com/golang/lint/issues/57), but since that issue is five years old I figured it might be fixed by now? Or does golint
simply not work across multiple files?
Upvotes: 56
Views: 67405
Reputation: 684
The plugin has changed a lot since 2019.
gopls
as the language server by default. Note that in 2019, there were two different language servers and gopls
was still in experimental mode.golint
was deprecated.If you still have a similar issue, it's likely that you are seeing a different problem.
Please check the followings:
go.mod
? Otherwise, initialize your working module and restart the language server or reload the window.go.mod
file in the root directory of your workspace? See gopls
workspace setup
guide for complex setup."go.buildTags"
or "go.buildFlags"
.staticcheck
, golangci-lint
, ..., check "go.lintOnSave"
is set to the right scope.If you notice that restarting the language server ("Go: Restart Language Server" command) fixes your issue, that's a gopls bug. Please consider to file an issue in github.com/golang/vscode-go following the troubleshooting guide.
Upvotes: 11
Reputation: 923
I faced same problem. I found that I got into this problem after enabling "Go language server" which is an experimental feature. I disabled it in VS code settings->Go Configuration and after that the problem went away.
Upvotes: 71
Reputation: 3821
Most of the answers here do not cover the buildtag
s scenario. The error messages do not do a good job pointing to this. If none of the other solutions work out, it might very well be that a module you're using uses a build tag. Search your codebase (cmd + shift + f
or the magnifying glass icon on the left bar) for //go:build
. In my case I had a:
//go:build linux
and this was the root of my problems. To make gopls
aware of this, you must add the following configuration to settings.json
{ //your settings.json
//...,
"gopls": {
"build.buildFlags": ["-tags=linux"],
},
//...
}
note your build flag may be different so replace linux
with whatever yours is. You may specify more than one tag eg ["-tags=linux,tag1,tag2,etc"]
.
This worked out for me but if it doesn't you may find other unfortunate souls dealing with this here and hope someone has a solution for a similar enough environment to yours.
Upvotes: 1
Reputation: 769
Update VS Code Go Tool might help.
Command + Shift + P
-> Go: Install/update tools
Install all tools and restart VS Code.
May 2022 update:
This solution only works if you haven't installed the helper tools
. Normally after you installed these packages it'll work right away with the default configuration
, if you still have a problem, take a look at the answer above.
Upvotes: 65
Reputation: 4997
One possible reason:
If you are referencing a function/variableis declared in a test file (*_test.go) from a non test file, this error would be thrown.
Upvotes: 2
Reputation: 726
I came across this issue by having the go extension installed, and attempting to utilize the same package name with a module under a different directory.
main.go
, a.go
, and go.mod
nested/b.go
Problem: Attempting to label b.go
as package main
when it is under a different directory.
Solutions:
b.go
up to project root and retain package name; all works as expected,b.go
from package main
to package nested
, then add imports for b.go
to main.go
via:// main.go
package main
import "example/nested"
func main() {
A()
nested.B()
}
and b.go
:
// b.go
package nested
import "fmt"
func B() {
fmt.Println("Hello from B")
}
Upvotes: 1
Reputation: 640
Single Project VS Code
**
Avoid Multiple Project in VS Code
**
Upvotes: 9
Reputation: 47
Make clean uninstall of vscode and then it's work fine again...
add sudo if you needed to
rm -rf $HOME/Library/Application\ Support/Code
rm -rf $HOME/.vscode
Remove vscode from application
Download vscode and install again
Upvotes: 1
Reputation: 37227
In my case it was a missing go.mod
file. I fixed with the following command:
go mod init example.com/myProject/myModule
Of course you should use a more reasonable module name.
Upvotes: 3
Reputation: 75
Another solution might be that you need to have the folder opened in VS Code with the go.mod file included. So you might have a folder structure that looks like workspace/application/modules/xyz.go. If you have the go.mod file in the application folder and modules is the folder you have open in VS Code it will complain.
Upvotes: 0
Reputation: 1108
The cause of this warning for me was the setting go.lintOnSave
, which was set to file
. Changing the value to package
made the linter correctly pick up the types defined in other files.
Upvotes: 36
Reputation: 547
If you run across this and are NOT using modules, then adding "go.useLanguageServer": false will disable gopls and return you to your former environment. (meaning vscode will now recognize functions and structures defined in multiple files in the same package)
Upvotes: 4
Reputation: 1729
After almost pulling my hair out, I found that linting was working but I had many files with errors. I haven't yet found the hierarchy followed but fixing problems in one file subsequently led to it correctly linting another file. I think it follows the execution tree, although I haven't validated this. I found this annoying as it can mistakenly lead you to think that linting is not working, while in fact, it's lining a file that you're not currently focussed on, especially if you have generated files that you're not interested in.
Upvotes: 0