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: "Extract Function" code action provides invalid suggestions #48963

Closed
OmarTawfik opened this issue Oct 14, 2021 · 1 comment
Closed

Comments

@OmarTawfik
Copy link

@OmarTawfik OmarTawfik commented Oct 14, 2021

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

Version Information
  • Run go version to get version of Go from the VS Code integrated terminal.
go version go1.17.2 windows/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
Build info
----------
golang.org/x/tools/gopls v0.7.2
    golang.org/x/tools/gopls@v0.7.2 h1:kRKKdvA8GOzra8rhSFDClOR7hV/x8v0J0Vm4C/gWq8s=
    github.com/BurntSushi/toml@v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
    github.com/google/go-cmp@v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
    golang.org/x/sys@v0.0.0-20210809222454-d867a43fc93e h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
    golang.org/x/tools@v0.1.6-0.20210908190839-cf92b39a962c h1:C0nyHiBU2m0cR6hDiUORWqQIt3h37wsp1255QBSSXqY=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.2.0 h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
    mvdan.cc/gofumpt@v0.1.1 h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
    mvdan.cc/xurls/v2@v2.3.0 h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
1.61.0
ee8c7def80afc00dd6e593ef12f37756d8f504ea
x64
  • Check your installed extensions to get the version of the VS Code Go extension
v0.28.1

Share the Go related settings you have added/edited

  • No custom settings/overrides. This is reproducible on a fresh installation.

Describe the bug

When using the Extract Function code action on the curly-bracket-wrapped body of an anonymous function, it produces invalid code, likely due to selecting the wrong source range from the document to be extracted. I suspect the code action logic is selecting the start/end lines of the selection, disregarding the start/end columns, which are important in this case.

Steps to reproduce the behavior:

  1. Open a new repro.go file, and paste the following code:
package main

type converter func(int) int

func convert(value int, fn converter) int {
	return fn(value)
}

func handler() {
	convert(5, func(value int) int {
		return value + 1
	})
}
  1. Now select the body of the converter inside handler(), including the curly brackets (as indicated in the screenshot):

image

  1. Execute the Extract Function code action
  • Expected: the correct method is extracted, and a call to it is inlined.
  • Actual: invalid syntax is generated, corrupting the source file, as seen in the screenshot below:

image

@stamblerre stamblerre transferred this issue from golang/vscode-go Oct 14, 2021
@stamblerre stamblerre changed the title "Extract Function" code action provides invalid suggestions x/tools/gopls: "Extract Function" code action provides invalid suggestions Oct 14, 2021
@gopherbot gopherbot added this to the Unreleased milestone Oct 14, 2021
@stamblerre stamblerre removed this from the Unreleased milestone Oct 14, 2021
@stamblerre stamblerre added this to the gopls/unplanned milestone Oct 14, 2021
@gopherbot
Copy link

@gopherbot gopherbot commented Oct 21, 2021

Change https://golang.org/cl/357615 mentions this issue: internal/lsp: adjust extract function range if block statement

@gopherbot gopherbot closed this in 6f2254e Oct 22, 2021
@stamblerre stamblerre removed this from the gopls/unplanned milestone Oct 22, 2021
@stamblerre stamblerre added this to the gopls/v0.7.4 milestone Oct 22, 2021
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