Go linter in VS code not working for packages across multiple files?
Solution 1
[The original answer is outdated; here is up-to-date information provided by the vscode-go
maintainers. The updated answer is now marked as "Recommended" in the Go collective]
The plugin has changed a lot since 2019.
- In 2021, Go Modules became the default which may have changed how the program is built and analyzed.
- The vscode-go plugin uses
gopls
as the language server by default. Note that in 2019, there were two different language servers andgopls
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:
- Do you have
go.mod
? Otherwise, initialize your working module and restart the language server or reload the window. - Is the
go.mod
file in the root directory of your workspace? Seegopls
workspace setup guide for complex setup. - Do you use build tags or other build constraints? Then see issue 29202. You may need to configure
"go.buildTags"
or"go.buildFlags"
. - If you expect lint errors from linters like
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.
Otherwise, please open a new question with details.
----- Original answer -------
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.
Solution 2
Update VS Code Go Tool might help.
Command + Shift + P
-> Go: Install/update tools
Install all tools and restart VS Code.
Solution 3
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.
Solution 4
For people who ended up here:
The plugin has changed a lot since 2019.
- In 2021, Go Module became the default which may have changed how the program is built and analyzed.
- The vscode-go plugin uses
gopls
as the language server by default. Note that in 2019, there were two different language servers andgopls
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:
- Do you have
go.mod
? Otherwise, initialize your working module and restart the language server or reload the window. - Is the
go.mod
file in the root directory of your workspace? Seegopls
workspace setup guide for complex setup. - Do you use build tags or other build constraints? Then see issue 29202. You may need to configure
"go.buildTags"
or"go.buildFlags"
. - If you expect lint errors from linters like
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.
Otherwise, please open a new question with details.
Solution 5
- Update Install/update tools for GO
- Open your code as main project in VS Code and avoid multiple projects/workspace in the same VS Code. **
Single Project VS Code
**
Avoid Multiple Project in VS Code
**
Comments
-
Kurt Peek almost 2 years
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
withpackage foobar import "fmt" func main() { fmt.Println(SayHello()) }
and a file
bar.go
withpackage foobar func SayHello() string { return "Hello, world!" }
then in
foo.go
I get a linter error thatSayHello
is anundeclared 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?