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: symbol returns matches from outside the workspace #37236

Closed
myitcv opened this issue Feb 15, 2020 · 19 comments
Closed

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

myitcv opened this issue Feb 15, 2020 · 19 comments
Assignees
Labels
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. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@myitcv
Copy link
Member

myitcv commented Feb 15, 2020

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

@myitcv myitcv added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. gopls Issues related to the Go language server, gopls. labels Feb 15, 2020
@gopherbot gopherbot added this to the Unreleased milestone Feb 15, 2020
@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 15, 2020
@stamblerre
Copy link
Contributor

This is intentional - I think it's useful to be able to go to the a symbol in a dependency of your workspace packages. Is there a particular reason you would want to remove this behavior?

@stamblerre stamblerre modified the milestones: Unreleased, gopls/v1.0.0 Feb 18, 2020
@myitcv
Copy link
Member Author

myitcv commented Feb 19, 2020

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.

@myitcv myitcv closed this as completed Feb 19, 2020
@stamblerre stamblerre modified the milestones: gopls/v1.0.0, gopls/v0.4.0 Jul 22, 2020
@golang golang locked and limited conversation to collaborators Jul 22, 2021
@findleyr
Copy link
Contributor

This has come up a few times. Several users do not like seeing results outside their workspace.

Given that this is a superficial change, I would not be opposed to adding e.g. a "symbolScope" setting that alters this behavior.

@findleyr findleyr reopened this Jan 26, 2023
@findleyr findleyr modified the milestones: gopls/v0.4.0, gopls/later Jan 26, 2023
@golang golang unlocked this conversation Jan 26, 2023
@guettli
Copy link

guettli commented Apr 20, 2023

Are there any updates on this topic? Is there a work-around? It realy hurts to not have a sane search for Go symbols in vscode.

Here is the my current work-around: https://stackoverflow.com/a/75233923/633961

@findleyr
Copy link
Contributor

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.

@findleyr findleyr modified the milestones: gopls/later, gopls/v0.12.0 Apr 20, 2023
@guettli
Copy link

guettli commented Apr 21, 2023

@findleyr Thank you for the good news. I think it depends on the your-code/library-code ratio. I develop a kubernetes operator (CRD). My code is only few lines, but I import a lot of packages. If I use ctrl-t (Go to Symbol in Workspace) I see ~50 matches. Only ~5 are from my code. We use code-vendoring. Please don't include the vendored code into the results. The vendored code is (according to my definition) not part of my code.

@gopherbot
Copy link

Change https://go.dev/cl/490935 mentions this issue: gopls/internal/lsp/source: add the "symbolScope" option

@findleyr findleyr self-assigned this May 4, 2023
@myitcv
Copy link
Member Author

myitcv commented May 4, 2023

Just driving by to re-up #37237 in this context. Something like the approach listed in that issue would allow the caller to define the scope on a per query basis. I'm constantly bugged by the fact that I get symbols outside of the current package, let alone the workspace!

@findleyr
Copy link
Contributor

findleyr commented May 5, 2023

@myitcv we will likely not undertake #37237 until VS Code allows controlling the workspace symbol result dialog. As it is now, any sort of advanced query syntax would cause VS Code to filter out all results, because it does its own round of sorting and filtering after we return results.

@findleyr
Copy link
Contributor

findleyr commented May 5, 2023

@guettli you can now test this out by building gopls@master:

e.g.:

git clone https://go.googlesource.com/tools
cd tools/gopls && go install

@guettli
Copy link

guettli commented May 5, 2023

@findleyr thank you very, much ctrl+t show now my code only:

image

old:

image

works fine, thank you!

@myitcv
Copy link
Member Author

myitcv commented May 6, 2023

As it is now, any sort of advanced query syntax would cause VS Code to filter out all results

But that would only occur if someone opted into using the advanced query syntax. Nothing would change by default. And as you say, because a) there isn't a good interface/UX for doing this in VSCode, and b) it performs its own filtering (incorrectly) then VSCode users would not be able to leverage such a feature. And it could be documented as such.

My point is that we don't need nor are we required to gate implementing such an experimental feature on whether VSCode can support it or not. Such a feature would provide a serious improvement of experience for those users able to take advantage of it.

@guettli
Copy link

guettli commented May 8, 2023

@myitcv this issue is about symbols outside the workspace. Did this change help you?

@myitcv
Copy link
Member Author

myitcv commented May 8, 2023

@myitcv this issue is about symbols outside the workspace. Did this change help you?

@guettli - not really. Because the scope in which I want to search for symbols changes. Sometimes I want to search across the equivalent of the all package space, sometimes just in the workspace, sometimes just in the scope of the module to which the current file belongs, sometimes the scope of the package to which the current file belongs, sometimes in the scope of just the current file.

Having a single static config option doesn't much help therefore. Not to say that it wouldn't help others, I'm just drawing a connection to a previously discussed issue.

@findleyr
Copy link
Contributor

findleyr commented May 8, 2023

My point is that we don't need nor are we required to gate implementing such an experimental feature on whether VSCode can support it or not. Such a feature would provide a serious improvement of experience for those users able to take advantage of it.

Understood. I was just trying to convey priorities: right now we have a lot of high priority, impactful work to do for gopls (releasing our new, scalable architecture, working on making gopls zero-config, improving workspace support, etc). Realistically, we are unlikely to prioritize this work until more users could benefit from it.

Also: let's see what the feedback is from this current change.

@myitcv
Copy link
Member Author

myitcv commented May 10, 2023

We can start with "workspace" as the default.

@findleyr - I had missed this detail until I happened to start using ddfa2200ae0bde969aa31087e186187f4fa91da0.

Why did you decide to make this the default, given the current behaviour is "all"?

Would it not be better to have people opt into this, rather than changing the default behaviour?

Or does "several users" actually translate to "the majority of users"?

@findleyr
Copy link
Contributor

@myitcv we don't actually know what our users want: we only hear from users via surveys and issues, and I have only ever heard users express that they are suprised when workspace/symbol responses return results outside their opened workspace folder. We don't have evidence that folks like the current behavior: I feel like I've been a bit stubborn in keeping it, because I prefer it.

However, your question has made me reconsider this decision: it may be more prudent to start with the default value of "all", and solicit feedback as to whether we should change the default.

Questions like these are why we want to add telemetry to gopls: this type of question does not usually fit the scope of a survey, and yet by seeing whether folks actually jump to a symbol outside the workspace we'd immediately know whether the current behavior is useful.

@gopherbot
Copy link

Change https://go.dev/cl/494217 mentions this issue: gopls: change the default value of "symbolScope" to "all"

gopherbot pushed a commit to golang/tools that referenced this issue May 10, 2023
Following discussion on golang/go#37236, let's be a bit careful before
changing behavior. For gopls@v0.12.0, we can keep the default of this
setting at "all", and solicit feedback for which default our users
prefer.

Updates golang/go#37236

Change-Id: Ia92382d808983a6ce566c85d06b82afd2375fb90
Reviewed-on: https://go-review.googlesource.com/c/tools/+/494217
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
@myitcv
Copy link
Member Author

myitcv commented May 11, 2023

We don't have evidence that folks like the current behavior

Arguably you have some evidence that folks don't dislike the behaviour of "all" - the fact that in the 3+ years since I raised this issue, it (this issue) was the only one on the subject (assuming any dups would have been linked to it if created, I didn't perform an exhaustive search).

Points very much taken on telemetry BTW.

FWIW, because this is configurable I don't really mind which way you have the default. I was just flagging the fact that the default had changed... and that surprised me given the aforementioned evidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants