Go linter in VS code not working for packages across multiple files?

24,265

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 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:

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 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:

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

  1. Update Install/update tools for GO
  2. Open your code as main project in VS Code and avoid multiple projects/workspace in the same VS Code. **

Single Project VS Code

** Single Project VS Code

**

Avoid Multiple Project in VS Code

**

Avoid Multiple Project in VS Code

Share:
24,265
Kurt Peek
Author by

Kurt Peek

Hi, I'm Kurt Peek, a backend engineer at Apple.

Updated on July 09, 2022

Comments

  • Kurt Peek
    Kurt Peek almost 2 years

    I have installed the Go extension (version 0.11.4) in Visual Studio Code on MacOS:

    enter image description here

    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:

    enter image description here

    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?