Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Go: Extract function refactoring errors, can't find imports in $GOPATH/pkg/mod/ #3040

Closed
mhansen opened this issue Feb 11, 2020 · 2 comments
Closed
Labels
upstream-gopls Issue for gopls

Comments

@mhansen
Copy link

mhansen commented Feb 11, 2020

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

  • Run go version to get version of Go
    go version go1.13.7 darwin/amd64

  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders
    1.42.0
    ae08d5460b5a45169385ff3fd44208f431992451
    x64

  • Check your installed extensions to get the version of the VS Code Go extension
    0.13.0

  • Run go env GOOS GOARCH to get the operating system and processor arhcitecture details
    darwin
    amd64

Share the Go related settings you have added/edited

"[go]": {

},
"go.formatTool": "goimports",
"go.useLanguageServer": true

Describe the bug

I'm in a go modules project, but the "Extract Function" refactoring errors when I try to execute it, because it can't find an import that's located in $GOPATH/pkg/mod.

I expected the extract function refactoring to work. Instead I get an error toast.

Steps to reproduce the behavior:

  1. Clone https://github.com/mhansen/nzwirelessmap-fetch
  2. Select some go code. I'm using these lines as an example that I'd like to extract to a function:
	var selectErr bytes.Buffer
	selectCmd := exec.Command("/usr/bin/sqlite3", tmpSqlite.Name())
	selectCmd.Stdin = sqlF
	selectCmd.Stdout = tmpCsv
	selectCmd.Stderr = &selectErr

	err = selectCmd.Run()
	if err != nil {
		return fmt.Errorf("couldn't select: %v, stderr: %v", err, selectErr.String())
	}
  1. Command-Shift-P "Go: Extract To Function"
  2. Type any function name. I used 'hello'
  3. See error toast, which says:

Defaulting to file scope /Users/mark/projects/nzwirelessmap-fetch/fetch.go for refactoring (provide an explicit scope to change this) fetch.go:18:2: Error: could not import cloud.google.com/go/storage (cannot find package "cloud.google.com/go/storage" in any of: /usr/local/Cellar/go/1.13.7/libexec/src/cloud.google.com/go/storage (from $GOROOT) /Users/mark/go/src/cloud.google.com/go/storage (from $GOPATH))

Yeah, the package isn't present in either of those. It's present in /Users/mark/go/pkg/mod/cloud.google.com/go/[email protected] instead.

@stamblerre
Copy link
Contributor

The tool that provides this feature, godoctor, doesn't seem to support modules yet (godoctor/godoctor#50). This feature should probably be disabled when modules are enabled. I filed golang/go#37170 to track adding support for this in the language server.

@stamblerre stamblerre added the upstream-gopls Issue for gopls label Feb 11, 2020
@stamblerre
Copy link
Contributor

Closing in favor of the upstream issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
upstream-gopls Issue for gopls
Projects
None yet
Development

No branches or pull requests

2 participants