Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jess-sch opened this issue Jul 14, 2019 · 24 comments
Closed

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

jess-sch opened this issue Jul 14, 2019 · 24 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@jess-sch
Copy link

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.

@gopherbot gopherbot added this to the Unreleased milestone Jul 14, 2019
@gopherbot gopherbot added the gopls Issues related to the Go language server, gopls. label Jul 14, 2019
@stamblerre stamblerre changed the title x/tools/gopls: Undeclared name when accessing stuff from other files x/tools/gopls: undeclared name when accessing stuff from other files Jul 14, 2019
@stamblerre stamblerre changed the title x/tools/gopls: undeclared name when accessing stuff from other files x/tools/gopls: undeclared name when accessing symbols from other files Jul 14, 2019
@stamblerre stamblerre added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Jul 14, 2019
@stamblerre
Copy link
Contributor

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

@jess-sch
Copy link
Author

jess-sch commented Jul 14, 2019 via email

@peterfraedrich
Copy link

peterfraedrich commented Jul 17, 2019

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.

@chodyo
Copy link

chodyo commented Jul 18, 2019

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

@stamblerre
Copy link
Contributor

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?

@chodyo
Copy link

chodyo commented Jul 18, 2019

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]

@gogochan
Copy link

having the same issue here too.

@stamblerre
Copy link
Contributor

@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.

@chodyo
Copy link

chodyo commented Jul 18, 2019

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.

@stamblerre
Copy link
Contributor

@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 #32394.

@chodyo
Copy link

chodyo commented Jul 18, 2019

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!

@gogochan
Copy link

That worked for me too. Thank you @stamblerre

@adir-ch
Copy link

adir-ch commented Jul 21, 2019

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?

@stamblerre
Copy link
Contributor

stamblerre commented Jul 21, 2019

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

@adir-ch
Copy link

adir-ch commented Jul 23, 2019

@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!

@stamblerre
Copy link
Contributor

Glad to hear it! Thanks for sharing.

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

@jess-sch
Copy link
Author

@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.

@stamblerre
Copy link
Contributor

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

@fastfading
Copy link

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"

@stamblerre
Copy link
Contributor

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

@gnodiah
Copy link

gnodiah commented Sep 8, 2019

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 😂

@jasondavides-zz
Copy link

jasondavides-zz commented Oct 28, 2019

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?

@stamblerre
Copy link
Contributor

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.

@mleonhard
Copy link

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
$

@golang golang locked and limited conversation to collaborators Jun 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests