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

"Go: Extract to function" command fails as vscode can't resolve dependencies when using gopls and modules #677

Closed
luca-battistelli opened this issue Sep 23, 2020 · 4 comments

Comments

@luca-battistelli
Copy link

Please direct general questions to:

Please review the documentation before filing an issue.
Helpful pages include:

Please answer these questions before submitting your issue. Thanks!

What version of Go, VS Code & VS Code Go extension are you using?

  • Run go version to get version of Go
    • go version go1.15.2 darwin/amd64
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    • 1.49.1
    • 58bb7b2331731bf72587010e943852e13e6fd3cf
    • x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.17.0
  • Run go env to get the go development environment details
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/lucabattistelli/Library/Caches/go-build"
GOENV="/Users/lucabattistelli/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/lucabattistelli/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/lucabattistelli/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="/Users/lucabattistelli/repos/signalfx-mimi/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/w2/bhwq2wyx2r98trb8_htdvhw00000gq/T/go-build749457552=/tmp/go-build -gno-record-gcc-switches -fno-common"

Share the Go related settings you have added/edited

    "go.useLanguageServer": true,
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
        // Optional: Disable snippets, as they conflict with completion ranking.
        "editor.snippetSuggestions": "none",
    },
    "[go.mod]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
    },
    "gopls": {
         // Add parameter placeholders when completing a function.
        "usePlaceholders": true,
    
        // If true, enable additional analyses with staticcheck.
        // Warning: This will significantly increase memory usage.
        "staticcheck": false,
    }

Describe the bug

VsCode is looking for dependencies in e.g /usr/local/go/src/rsc.io/quote (from $GOROOT) and /Users/lucabattistelli/go/src/rsc.io/quote (from $GOPATH)

and not in the module cache, where they are instead located, e.g /Users/lucabattistelli/go/pkg/mod/rsc.io/quote\@v1.5.2/

Steps to reproduce the behavior:

  1. Download project go-test-modules-gopls-deps.zip (created by following the go modules blog up to "Adding a dependency" and then adding the line a := 2 + 3)
  2. Select a := 2 + 3
  3. Run the Go: Extract to function command
  4. Notice the error
Defaulting to file scope /Users/lucabattistelli/repos/go-test-modules-gopls-deps/hello.go for refactoring (provide an explicit scope to change this) hello.go:6:2: Error: could not import rsc.io/quote (cannot find package "rsc.io/quote" in any of: /usr/local/go/src/rsc.io/quote (from $GOROOT) /Users/lucabattistelli/go/src/rsc.io/quote (from $GOPATH))

Screenshots or recordings

If applicable, add screenshots or recordings to help explain your problem.

@hyangah
Copy link
Contributor

hyangah commented Sep 23, 2020

@luca-battistelli thanks for filing an issue. The tool implementing "Go: Extract to function" doesn't seem to handle modules mode. If you are already using gopls, gopls offers the 'extract to function' capability through the code action. When you select the code range to refactor, find a light bulb appearing (💡 ) near the selected region. Click it.

Screen Shot 2020-09-23 at 9 38 29 AM

You can also bind a key short cut for refactor.extract following this guide: https://code.visualstudio.com/docs/getstarted/keybindings#_custom-keybindings-for-refactorings
But I am currently not sure yet the refactorings action naming of gopls is stable before gopls v1.0.

@stamblerre Is this mapping for the code action kind stable? I think this should be refactor.extract.function but not sure. Once code action kinds & command names gopls uses become stable, I think we need to document and use them for "Go: Extract to function" implementation.

@luca-battistelli
Copy link
Author

Didn't notice the lightbulb! Thanks for the help 🙂

@stamblerre
Copy link
Contributor

@stamblerre Is this mapping for the code action kind stable? I think this should be refactor.extract.function but not sure. Once code action kinds & command names gopls uses become stable, I think we need to document and use them for "Go: Extract to function" implementation.

I don't think we have the refactor.extract.function kind yet, but we can add it. We can try to address this when we change the command names (golang/go#41187).

@stamblerre
Copy link
Contributor

Looks like this has been resolved--closing.

@stamblerre stamblerre removed this from the Untriaged milestone Apr 9, 2021
@golang golang locked and limited conversation to collaborators Apr 9, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants