go: x/tools/gopls: symbol returns matches from outside the workspace

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

$ go version
go version devel +b7689f5aa3 Fri Jan 31 06:02:00 2020 +0000 linux/amd64
$ go list -m golang.org/x/tools
golang.org/x/tools v0.0.0-20200214144324-88be01311a71
$ go list -m golang.org/x/tools/gopls
golang.org/x/tools/gopls v0.1.8-0.20200214144324-88be01311a71

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/myitcv/.cache/go-build"
GOENV="/home/myitcv/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/myitcv/gostuff"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/myitcv/gos"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/myitcv/gos/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/myitcv/gostuff/src/github.com/myitcv/govim/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build235629906=/tmp/go-build -gno-record-gcc-switches"

What did you do?

With the following setup:

-- main.go --
package main

import (
	"fmt"

	"golang.org/x/tools/imports"
)

func main() {
	fmt.Println(imports.LocalPrefix)
}
-- go.mod --
module github.com/myitcv/playground

go 1.12

require golang.org/x/tools v0.0.0-20200214191634-99763790079f

Called ‘Symbol’ with a query of "main"

What did you expect to see?

A single result: the main function defined in main; because the workspace scope is defined (I think) as the main module.

What did you see instead?

Lots of results from outside the workspace (main module) within dependencies:

gopls.Symbol() return; err: <nil>; res:
[]protocol.SymbolInformation{
    {
        Name:       "main",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gostuff/src/github.com/myitcv/playground/main.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:8, Character:5},
                End:   protocol.Position{Line:8, Character:9},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "Remainder",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/math/remainder.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:36, Character:5},
                End:   protocol.Position{Line:36, Character:14},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "remainder",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/math/remainder.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:38, Character:5},
                End:   protocol.Position{Line:38, Character:14},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "main",
        Kind:       8,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gostuff/pkg/mod/golang.org/x/tools@v0.0.0-20200214191634-99763790079f/internal/imports/mod.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:32, Character:1},
                End:   protocol.Position{Line:32, Character:5},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "domainname",
        Kind:       8,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/defs_linux_amd64.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:274, Character:1},
                End:   protocol.Position{Line:274, Character:11},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "main_inittask",
        Kind:       13,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/proc.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:91, Character:4},
                End:   protocol.Position{Line:91, Character:17},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "main_init_done",
        Kind:       13,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/proc.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:97, Character:4},
                End:   protocol.Position{Line:97, Character:18},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "main_main",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/proc.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:100, Character:5},
                End:   protocol.Position{Line:100, Character:14},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "mainStarted",
        Kind:       13,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/proc.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:103, Character:4},
                End:   protocol.Position{Line:103, Character:15},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "main",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/proc.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:112, Character:5},
                End:   protocol.Position{Line:112, Character:9},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "funcID_runtime_main",
        Kind:       14,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/symtab.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:237, Character:1},
                End:   protocol.Position{Line:237, Character:20},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "hasmain",
        Kind:       8,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/runtime/symtab.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:293, Character:1},
                End:   protocol.Position{Line:293, Character:8},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "Setdomainname",
        Kind:       12,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/syscall/zsyscall_linux_amd64.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:715, Character:5},
                End:   protocol.Position{Line:715, Character:18},
            },
        },
        ContainerName: "",
    },
    {
        Name:       "Domainname",
        Kind:       8,
        Deprecated: false,
        Location:   protocol.Location{
            URI:   "file:///home/myitcv/gos/src/syscall/ztypes_linux_amd64.go",
            Range: protocol.Range{
                Start: protocol.Position{Line:574, Character:1},
                End:   protocol.Position{Line:574, Character:11},
            },
        },
        ContainerName: "",
    },
}

cc @stamblerre

FYI @leitzler

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 19 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks for pinging, @guettli.

I still prefer the current behavior, but may be in the minority. I suspect that effectiveness of the current symbol search also depends on whether your client reorders results (invalidating our down-ranking of non-workspace symbols). My client does not reorder results, but VS Code does.

I will add a symbolScope setting for v0.12.0 with two values: “workspace” and “all”. We can start with “workspace” as the default.

Is there a particular reason you would want to remove this behavior

The only reason I questioned whether this was the intended behaviour or not is the discrepancy, under a very strict reading at least, between the fact the method is defined as returning workspace symbols but in fact returns symbols from outside the workspace.

If this is working as intended that’s absolutely fine (and in many respects better). I’ll add another comment to #37237 to also request a module scope.