Kurt Peek
Kurt Peek

Reputation: 57421

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

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?

Upvotes: 56

Views: 67405

Answers (14)

Hana
Hana

Reputation: 684

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.

Upvotes: 11

Sathish Ramani
Sathish Ramani

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

Alex
Alex

Reputation: 3821

Most of the answers here do not cover the buildtags 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

Poçi
Poçi

Reputation: 253

In my case, I just restarted VS Code and the error went away.

Upvotes: 1

Khoa Le
Khoa Le

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

Siva Prakash
Siva Prakash

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

Thomas
Thomas

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.

  • Files at Root: main.go, a.go, and go.mod
  • Sub-directory: nested/b.go

Problem: Attempting to label b.go as package main when it is under a different directory.

Solutions:

  1. Move b.go up to project root and retain package name; all works as expected,
  • or
  1. Change package name of 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")
}

Screen Shot

Upvotes: 1

junnyea
junnyea

Reputation: 640

  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

Upvotes: 9

SebastianSalvatore
SebastianSalvatore

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

iBug
iBug

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

Trapper Davis
Trapper Davis

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

Once Upon a Dev
Once Upon a Dev

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

Richard Varno
Richard Varno

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

Kisinga
Kisinga

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

Related Questions