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: Wrong completion for int * time.Duration #58944

Closed
tebeka opened this issue Mar 7, 2023 · 5 comments
Closed

x/tools/gopls: Wrong completion for int * time.Duration #58944

tebeka opened this issue Mar 7, 2023 · 5 comments
Labels
FrozenDueToAge gopls/completion Issues related to auto-completion in gopls. 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

@tebeka
Copy link
Contributor

tebeka commented Mar 7, 2023

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.20.1 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
Build info
----------
golang.org/x/tools/gopls v0.11.0
    golang.org/x/tools/gopls@v0.11.0 h1:/nvKHdTtePQmrv9XN3gIUN9MOdUrKzO/dcqgbG6x8EY=
    github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp@v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
    golang.org/x/exp/typeparams@v0.0.0-20221031165847-c99f073a8326 h1:fl8k2zg28yA23264d82M4dp+YlJ3ngDcpuB1bewkQi4=
    golang.org/x/mod@v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
    golang.org/x/sync@v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
    golang.org/x/sys@v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
    golang.org/x/text@v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
    golang.org/x/tools@v0.3.1-0.20221213193459-ca17b2c27ca8 h1:7/HkGkN/2ktghBCSRRgp31wAww4syfsW52tj7yirjWk=
    golang.org/x/vuln@v0.0.0-20221109205719-3af8368ee4fe h1:qptQiQwEpETwDiz85LKtChqif9xhVkAm8Nhxs0xnTww=
    honnef.co/go/tools@v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
    mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.20.1
  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.
1.76.0
92da9481c0904c6adfe372c12da3b7748d74bdcb
x64
  • Check your installed extensions to get the version of the VS Code Go extension
    • v0.38.0
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.
Checking configured tools....
GOBIN: undefined
toolsGopath: 
gopath: /home/miki/go
GOROOT: /home/miki/sdk/go1.20.1
PATH: /home/miki/bin:/home/miki/.local/bin:/home/miki/.venv/bin:/home/miki/sdk/go/bin:/home/miki/go/bin:/home/miki/.poetry/bin:/home/miki/.cargo/bin:/home/miki/.local/share/gem/ruby/3.0.0/bin:/home/miki/.local/bin:/opt/google-cloud-sdk/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/var/lib/snapd/snap/bin:/home/miki/.fzf/bin

	go:	/home/miki/sdk/go/bin/go: go version go1.20.1 linux/amd64

	gotests:	/home/miki/go/bin/gotests	(version: v1.6.0 built with go: go1.20)
	gomodifytags:	/home/miki/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.20.1)
	impl:	/home/miki/go/bin/impl	(version: v1.2.1-0.20221223162334-b4ecb144baaf built with go: go1.20.1)
	goplay:	/home/miki/go/bin/goplay	(version: v1.0.0 built with go: go1.20)
	dlv:	/home/miki/go/bin/dlv	(version: v1.20.1 built with go: go1.20.1)
	staticcheck:	/home/miki/go/bin/staticcheck	(version: v0.4.2 built with go: go1.20.1)
	gopls:	/home/miki/go/bin/gopls	(version: v0.11.0 built with go: go1.20.1)

go env
Workspace Folder (cache): /home/miki/work/lil/go-optimize/playground/cache
	GO111MODULE=""
	GOARCH="amd64"
	GOBIN=""
	GOCACHE="/home/miki/.cache/go-build"
	GOENV="/home/miki/.config/go/env"
	GOEXE=""
	GOEXPERIMENT=""
	GOFLAGS=""
	GOHOSTARCH="amd64"
	GOHOSTOS="linux"
	GOINSECURE=""
	GOMODCACHE="/home/miki/go/pkg/mod"
	GONOPROXY=""
	GONOSUMDB=""
	GOOS="linux"
	GOPATH="/home/miki/go"
	GOPRIVATE=""
	GOPROXY="https://proxy.golang.org,direct"
	GOROOT="/home/miki/sdk/go1.20.1"
	GOSUMDB="sum.golang.org"
	GOTMPDIR=""
	GOTOOLDIR="/home/miki/sdk/go1.20.1/pkg/tool/linux_amd64"
	GOVCS=""
	GOVERSION="go1.20.1"
	GCCGO="gccgo"
	GOAMD64="v1"
	AR="ar"
	CC="gcc"
	CXX="g++"
	CGO_ENABLED="1"
	GOMOD="/home/miki/work/lil/go-optimize/playground/go.mod"
	GOWORK=""
	CGO_CFLAGS="-O2 -g"
	CGO_CPPFLAGS=""
	CGO_CXXFLAGS="-O2 -g"
	CGO_FFLAGS="-O2 -g"
	CGO_LDFLAGS="-O2 -g"
	PKG_CONFIG="pkg-config"
	GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2080536578=/tmp/go-build -gno-record-gcc-switches"

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
Share all the settings with the go. or ["go"] or gopls prefixes.

	"go.toolsManagement.autoUpdate": true,

Describe the bug

A clear and concise description of what the bug.
A clear and concise description of what you expected to happen.

When I have the following code:

n := 3
time.Sleep(n * time.Millisecond

And hit ENTER, the generated code is

n := 3
time.Sleep(n * int(time.Millisecond))

Which is wrong since time.Sleep expects a time.Duration.
The right code should be:

n := 3
time.Sleep(time.Duration(n) * time.Millisecond)

Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. See error

See above.

Screenshots or recordings

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

@hyangah
Copy link
Contributor

hyangah commented Mar 8, 2023

Thanks for the report. @tebeka
Wonder if this can be generalized beyond time.Duration. When we see two possible type conversions, decide the winner based on how the expression output is used. Transferring to the gopls issue tracker.

@hyangah hyangah changed the title Wrong completion for int * time.Duration x/tools/gopls: Wrong completion for int * time.Duration Mar 8, 2023
@hyangah hyangah transferred this issue from golang/vscode-go Mar 8, 2023
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Mar 8, 2023
@gopherbot gopherbot added this to the Unreleased milestone Mar 8, 2023
@hyangah hyangah modified the milestones: Unreleased, gopls/later Mar 8, 2023
@hyangah hyangah added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. gopls/completion Issues related to auto-completion in gopls. and removed FeatureRequest labels Mar 8, 2023
@muirdm
Copy link

muirdm commented Mar 13, 2023

The issue here is that n is of type int, which influences the expected type of time.Millisecond. If you accepted the completion for n initially, it would have inserted time.Duration(n), which would have kept things on the right track.

Perhaps special cases like this could be detected and completed as you suggest, but it doesn't seem possible in general.

@tebeka
Copy link
Contributor Author

tebeka commented Mar 14, 2023

If you accepted the completion for n initially, it would have inserted time.Duration(n),

What does "accepted the completion for n initially" mean?

@muirdm
Copy link

muirdm commented Mar 15, 2023

What does "accepted the completion for n initially" mean?

It makes more sense with a longer variable like “fooBar := 3”. If you complete time.Sleep(foo it would insert fooBar wrapped in a type conversion.

@findleyr
Copy link
Contributor

Thanks Muir -- I meant to comment the same.

I think this is working as intended.

@findleyr findleyr closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2023
@golang golang locked and limited conversation to collaborators Mar 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls/completion Issues related to auto-completion in gopls. 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