Go: x/tools/gopls: undeclared name when accessing symbols from other files

Created on 14 Jul 2019  路  24Comments  路  Source: golang/go

What version of Go are you using (go version)?

$ go version
go version go1.12.7 linux/amd64

Does this issue reproduce with the latest release?

This is the latest release.

What operating system and processor architecture are you using (go env)?

go env Output

$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lumi/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/lumi/go"
GOPROXY=""
GORACE=""
GOROOT="/var/home/lumi/bin/_go"
GOTMPDIR=""
GOTOOLDIR="/var/home/lumi/bin/_go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
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=/tmp/go-build286957175=/tmp/go-build -gno-record-gcc-switches"

What did you do?

  1. go mod init $packagename
  2. create main.go:
package main

func main(){
  someFunc()
}
  1. create something.go:
package main

import ("fmt")

func someFunc(){
  fmt.Println("Hello")
}
  1. configure vscode as recommended at https://github.com/golang/go/wiki/gopls:
"go.useLanguageServer": true,
"[go]": {
    "editor.snippetSuggestions": "none",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
},
"gopls": {
    "usePlaceholders": true // add parameter placeholders when completing a function
},
"files.eol": "\n", // formatting only supports LF line endings
  1. look at vscode problems list, after restarting vscode and opening both files.

What did you expect to see?

Nothing. This code compiles without issues.

What did you see instead?

undeclared name: someFunc

Additional info

$ gopls version
version v0.1.3-cmd.gopls, built in $GOPATH mode

The issue occurs with variables, constants, functions and types. So basically everything.

NeedsInvestigation gopls

Most helpful comment

Thanks @stamblerre, that sounds consistent with what I've observed while using gopls.

I think I've found the root of the problem. I have a folder/workspace open in VSCode that isn't the root of my project. My workspace is something like ~/code/a/b/myProject. I just opened another instance of VSCode where I opened the folder directly, and gopls was able to discover the previously broken tokens.

All 24 comments

Does the problem persist after you re-load the VSCode window?

The problem disappears in the list, but as soon as I open that file back up
the issue comes back.

~Removing go.mod and go.sum and re-running go mod init in my project folder works for me, however this issue does need to be resolved.~

Removing since it only works temporarily and in some situations.

I tried what you listed @peterfraedrich, but that didn't seem to work for me.

This shouldn't be necessary - are either of you able to share your gopls logs? They can be found by going to "View: Debug Console" -> "Output" -> "Tasks" -> "gopls". Can you also try running gopls -rpc.trace -v check /path/to/file.go?

Here's some recent logs for gopls in VSCode (there's a lot, can provide more if you need it)

[Info  - 2:44:17 PM] 420.0003ms for GOROOT=C:\Go\ GOPATH=C:\Users\HeffnerCody\tools\go GO111MODULE=on PWD=c:\Users\HeffnerCody\code go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "c:\\Users\\HeffnerCody\\code\\bb\\ci\\bpkgversionbumper\\parser.go", stderr: <<>>

packages.Load: found 1 packages
packages.Load: package command-line-arguments with files [c:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\parser.go]
[Error - 2:44:17 PM] Request textDocument/documentLink failed.
  Message: GetAST: unable to check package for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go: no packages found for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go
  Code: 0 
[Error - 2:44:17 PM] Request textDocument/codeAction failed.
  Message: GetAST: unable to check package for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go: no packages found for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go
  Code: 0 
[Error - 2:44:32 PM] Request textDocument/codeAction failed.
  Message: GetAST: unable to check package for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go: no packages found for file:///c:/Users/HeffnerCody/code/bb/ci/bpkgversionbumper/main.go
  Code: 0 
[Info  - 2:56:38 PM] 136.996ms for GOROOT=C:\Go\ GOPATH=C:\Users\HeffnerCody\tools\go GO111MODULE=on PWD=c:\Users\HeffnerCody\code go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "c:\\Users\\HeffnerCody\\code\\bb\\ci\\bpkgversionbumper", stderr: <<go: cannot find main module; see 'go help modules'
>>

[Info  - 2:56:41 PM] 3.1020853s for GOROOT=C:\Go\ GOPATH=C:\Users\HeffnerCody\tools\go GO111MODULE=on PWD=c:\Users\HeffnerCody\code go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "c:\\Users\\HeffnerCody\\code\\bb\\ci\\bpkgversionbumper\\handler.go", stderr: <<go: finding github.com/google/go-github/v26/github latest
go: finding go.dev.bloomberg.com/bpkg/bpkgusvc/httperr latest
go: finding go.dev.bloomberg.com/bpkg/bpkgusvc/log latest
>>

packages.Load: found 1 packages
packages.Load: package command-line-arguments with files [c:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\handler.go]
[Error - 2:56:41 PM] no highlight for c:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\handler.go:33:33: c:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\handler.go:33:33 is not an identifier
[Error - 2:57:07 PM] Request textDocument/hover failed.
  Message: no object for ident gitClient
  Code: 0 

And the trace

$ gopls -rpc.trace -v check main.go
2019/07/18 14:49:45 Info:80.0723ms for GOROOT=C:\Go\ GOPATH=C:\Users\HeffnerCody\tools\go GO111MODULE= PWD=C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "builtin", stderr: <<>>
2019/07/18 14:49:46 Info:Build info
----------
version v0.1.3-cmd.gopls, built in $GOPATH mode

Go info
-------
go version go1.12.1 windows/amd64

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\HeffnerCody\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\HeffnerCody\tools\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\HEFFNE~1\AppData\Local\Temp\go-build883324078=/tmp/go-build -gno-record-gcc-switches
2019/07/18 14:49:46 Info:744.0315ms for GOROOT=C:\Go\ GOPATH=C:\Users\HeffnerCody\tools\go GO111MODULE= PWD=C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "C:\\Users\\HeffnerCody\\code\\bb\\ci\\bpkgversionbumper", stderr: <<>>
2019/07/18 14:49:46 Log:packages.Load: found 2 packages
2019/07/18 14:49:46 Log:packages.Load: package go.dev.bloomberg.com/bpkg/bpkgversionbumper with files [C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\git.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\handler.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\main.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\parser.go]
2019/07/18 14:49:47 Log:packages.Load: package go.dev.bloomberg.com/bpkg/bpkgversionbumper with files [C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\git.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\handler.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\main.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\parser.go C:\Users\HeffnerCody\code\bb\ci\bpkgversionbumper\parser_test.go]

having the same issue here too.

@chodyo: Looks like the gopls check command is working fine, but when you open your file it's being treated like an ad-hoc package - notice that the package path is listed as "command line arguments". How are you opening VSCode? Can you provide a log that starts from the beginning so that I can see the go env that gopls sees? What are your VSCode settings?

@ckim0419: Please provide logs so that I can help you debug.

Thanks @stamblerre, that sounds consistent with what I've observed while using gopls.

I think I've found the root of the problem. I have a folder/workspace open in VSCode that isn't the root of my project. My workspace is something like ~/code/a/b/myProject. I just opened another instance of VSCode where I opened the folder directly, and gopls was able to discover the previously broken tokens.

@chodyo: Yes, that would be the issue - glad you were able to resolve it. We're working on improving this experience and adding error reporting - see https://github.com/golang/go/issues/32394.

The workaround in #32394 fixed it for me.

For every go.mod file that you have, you should do "File" > "Add Folder to Workspace" and then add the module root.

Thank you!

That worked for me too. Thank you @stamblerre

I'm working with VSCode on Linux mint 19.2 with similar versions of go and gopls as in the first message in the thread. Whatever I do gopls give me the "undeclared name" error on a symbol that is defined in a different file, but in the same package. I don't get the error for symbols exported in other packages.

Any ideas of how I can workaround it?

@adir-ch: What is the output of gopls -v -rpc.trace check /path/to/file.go?

@adir-ch: What is the output of gopls -v -rpc.trace check /path/to/file.go?

My local problem solved!

This is the gopls debug output:

2019/07/23 14:15:57 Info:go/packages.Load packages = 1 2019/07/23 14:15:57 Info:go/packages.Load package = **command-line-arguments** files = [/home/adir/dev/repos/algotrader/pkg/trader/trade.go] /home/adir/dev/repos/algotrader/pkg/trader/trade.go:17:56-65: undeclared name: Validator

No matter how I tried using VSCode (as single folder or as the root folder of a workspace) with the repo, I got the "undeclared name" error.
However, when trying the same thing on windows everything was working perfect! not matter how I used VSCode (as a single folder or workspace).

Finally I've noticed in gopls logs shows the following errer:
_cgo_export.c:3:10: fatal error: stdlib.h: No such file or directory

What I did to resolve it, was to install libc-dev (apt-get install libc-dev), and gopls was able to work properly!

Glad to hear it! Thanks for sharing.

@lumi-sch: Are you still seeing this issue?

@lumi-sch: Are you still seeing this issue?

No, I've recently reset my PC and the issue didn't occur on the fresh install.

Ok, glad to hear it. I鈥檓 going to close this issue- if anyone is still experiencing this, please open a new issue.

same issue on mac ,
go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/xxx/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/xxx/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.7/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.7/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/xxx/git/golang/nbrtool/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/g4/b17y14y9753bsx3_f_03ggh80000gn/T/go-build831944615=/tmp/go-build -gno-record-gcc-switches -fno-common"

@fastfading: Please open a new issue, and please attach the information requested in the troubleshooting guide.

Thanks @stamblerre, that sounds consistent with what I've observed while using gopls.

I think I've found the root of the problem. I have a folder/workspace open in VSCode that isn't the root of my project. My workspace is something like ~/code/a/b/myProject. I just opened another instance of VSCode where I opened the folder directly, and gopls was able to discover the previously broken tokens.

thanks for the solution, same situation with you 馃槀

Thanks @stamblerre, that sounds consistent with what I've observed while using gopls.

I think I've found the root of the problem. I have a folder/workspace open in VSCode that isn't the root of my project. My workspace is something like ~/code/a/b/myProject. I just opened another instance of VSCode where I opened the folder directly, and gopls was able to discover the previously broken tokens.

Yeap, same solution for me, thanks for sharing it... Could any of you tell me why that happens when we open the folder in the way the breaks gopls?

gopls requires that your root directory is the root of the module, as it uses that information to determine the configuration it should use when it gets metadata about the packages in your workspace. We are working on improving this behavior.

I have a monorepo directory added to VSCode, with various sub-directories containing golang code and code in other languages. VSCode was not finding symbols in the same Golang package which are not in the same file. I ran this command in the root of the git repo, restarted VSCode, and the problem went away:

$ go mod init company.com/go/v2
go: creating new go.mod: module company.com/go/v2
$ go version
go version go1.14.4 darwin/amd64
$
Was this page helpful?
0 / 5 - 0 ratings