Skip to content

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

Closed
@jess-sch

Description

@jess-sch

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.

Activity

added this to the Unreleased milestone on Jul 14, 2019
added
goplsIssues related to the Go language server, gopls.
on Jul 14, 2019
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[/+] on Jul 14, 2019
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[/+] on Jul 14, 2019
added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Jul 14, 2019
stamblerre

stamblerre commented on Jul 14, 2019

@stamblerre
Contributor

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

jess-sch

jess-sch commented on Jul 14, 2019

@jess-sch
Author
peterfraedrich

peterfraedrich commented on Jul 17, 2019

@peterfraedrich

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

chodyo commented on Jul 18, 2019

@chodyo

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

stamblerre

stamblerre commented on Jul 18, 2019

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

chodyo commented on Jul 18, 2019

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

gogochan commented on Jul 18, 2019

@gogochan

having the same issue here too.

stamblerre

stamblerre commented on Jul 18, 2019

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

chodyo commented on Jul 18, 2019

@chodyo

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

stamblerre commented on Jul 18, 2019

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

20 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.goplsIssues related to the Go language server, gopls.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @mleonhard@gnodiah@chodyo@gogochan@adir-ch

        Issue actions

          x/tools/gopls: undeclared name when accessing symbols from other files · Issue #33107 · golang/go