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 #35337

Closed
lotsofone opened this issue Nov 4, 2019 · 4 comments
Closed

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

lotsofone opened this issue Nov 4, 2019 · 4 comments
Assignees
Milestone

Comments

@lotsofone
Copy link

@lotsofone lotsofone commented Nov 4, 2019

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

$ go version
go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes. I installed the latest stable vscode build and golang.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jingmaoli/Library/Caches/go-build"
GOENV="/Users/jingmaoli/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jingmaoli/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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=/var/folders/t7/5wks8wmd113bd2vmxyht_hz18wqkb5/T/go-build555277668=/tmp/go-build -gno-record-gcc-switches -fno-common"

The output of gopls version and gopls -rpc.trace -v check /path/to/file.go?

$ gopls version
golang.org/x/tools/gopls v0.1.7
    golang.org/x/tools/gopls@v0.1.7 h1:YwKf8t9h69++qCtVmc2q6fVuetFXmmu9LKoPMYLZid4=
$ cd newproject
$ gopls -rpc.trace -v check ./main.go
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 Build info
----------
golang.org/x/tools/gopls v0.1.7
    golang.org/x/tools/gopls@v0.1.7 h1:YwKf8t9h69++qCtVmc2q6fVuetFXmmu9LKoPMYLZid4=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
    golang.org/x/tools@v0.0.0-20190918214516-5a1a30219888 h1:ER45Jz0UDQ3e6em1lwXVwuPf96lvyQogb7m+gEbsoPg=
    golang.org/x/xerrors@v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=

Go info
-------
go version go1.13.4 darwin/amd64

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jingmaoli/Library/Caches/go-build"
GOENV="/Users/jingmaoli/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jingmaoli/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
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=/var/folders/t7/5wks8wmd113bd2vmxyht_hz18wqkb5/T/go-build468474295=/tmp/go-build -gno-record-gcc-switches -fno-common"
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 17.284317ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "env" "GOMOD", stderr: <<>>
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 17.996129ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "list" "-m" "-json" "all", stderr: <>
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 19.629816ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "builtin", stderr: <<>>
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 15.675241ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "env" "GOPATH", stderr: <<>>
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 15.213536ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "env" "GOMOD", stderr: <<>>
2019/11/04 16:37:45 Info:2019/11/04 16:37:45 15.515843ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "list" "-m" "-json" "all", stderr: <>
2019/11/04 16:37:46 Info:2019/11/04 16:37:46 16.533368ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "env" "GOPATH", stderr: <<>>
2019/11/04 16:37:46 Info:2019/11/04 16:37:46 16.029362ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "/Users/jingmaoli/Documents/newproject", stderr: <<>>
2019/11/04 16:37:46 Info:2019/11/04 16:37:46 67.525425ms for GOROOT= GOPATH= GO111MODULE= PWD=/Users/jingmaoli/Documents/newproject go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "/Users/jingmaoli/Documents/newproject/main.go", stderr: <<>>
2019/11/04 16:37:46 Info:2019/11/04 16:37:46 go/packages.Load
	packages = 1
2019/11/04 16:37:46 Info:2019/11/04 16:37:46 go/packages.Load
	package = command-line-arguments
	files = [/Users/jingmaoli/Documents/newproject/main.go]
/Users/jingmaoli/Documents/newproject/main.go:4:2-3: undeclared name: f

What did you do?

1.create a folder called "newproject" and use vscode to open it
2.create main.go

package main

func main() {
	f()
}

3.create f.go

package main

import "fmt"

func f() {
	fmt.Println("Hello")
}

What did you expect to see?

No error should show up

What did you see instead?

No error occurs when running the following command

cd newproject
go build
./newproject

it will output 'Hello'
but in the vscode editor, it shows "undeclared name: f"

@gopherbot gopherbot added this to the Unreleased milestone Nov 4, 2019
@gopherbot gopherbot added the Tools label Nov 4, 2019
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 4, 2019

Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.

@gopherbot gopherbot added the gopls label Nov 4, 2019
@stamblerre

This comment has been minimized.

Copy link
Contributor

@stamblerre stamblerre commented Nov 4, 2019

It looks like you are not working in your GOPATH, and you have not initialized a module. gopls will only work with multiple files in a package if you have initialized a module.

@stamblerre stamblerre self-assigned this Nov 7, 2019
@lotsofone

This comment has been minimized.

Copy link
Author

@lotsofone lotsofone commented Nov 8, 2019

It looks like you are not working in your GOPATH, and you have not initialized a module. gopls will only work with multiple files in a package if you have initialized a module.

Does it mean I need to put my 'newproject' into $GOPATH/src/ to let gopls work?

@lotsofone

This comment has been minimized.

Copy link
Author

@lotsofone lotsofone commented Nov 8, 2019

Sorry I just tried 'go mod init somename' and gopls works. This should not be an issue.

@lotsofone lotsofone closed this Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.