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: reverting files after formatting #34423

Closed
relax-admin opened this issue Sep 20, 2019 · 10 comments
Closed

x/tools/gopls: reverting files after formatting #34423

relax-admin opened this issue Sep 20, 2019 · 10 comments
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@relax-admin
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.12.5 windows/amd64
Https://go.googlesource.com/tools
Commit:e45ffcd953cceb219dc536e24468c0d22a5be010

Does this issue reproduce with the latest release?

What did you do?

x/tools/gopls makes me painful.
I recently updated this package. I often can't save the code.
Sometimes, When I delete some code, after pressing ctrl +s, those extra codes will appear again.

I don't know if the description is clear,
if there are other people who have encountered it.
In short, this is a painful experience.

What did you expect to see?

When I press ctrl +s, I can save the code automatically.

What did you see instead?

When I delete some code, after pressing ctrl +s, those extra codes will appear again.

@gopherbot gopherbot added this to the Unreleased milestone Sep 20, 2019
@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 Sep 20, 2019
@gopherbot
Copy link

Thank you for filing a gopls issue! Please take a look at the Troubleshooting section of the gopls Wiki page, and make sure that you have provided all of the relevant information here.

@stamblerre
Copy link
Contributor

I'm sorry you're experiencing this issue; this definitely shouldn't happen. What editor are you using gopls with? Can you confirm your gopls version by running the gopls version command?

@stamblerre
Copy link
Contributor

Also, can you share your gopls logs from a time when this happens? You can get information on how to get logs in the Troubleshooting guide.

@stamblerre stamblerre changed the title x/tools/gopls makes me painful. x/tools/gopls: reverting files after formatting Sep 20, 2019
@relax-admin
Copy link
Author

@stamblerre

Thank you very much for your enthusiastic and positive reply. Just before the bug appeared, my operation is ctrl+x cutting a line, then pressing ctrl+s, then the code is messed up.

Initial code

package cmd

import (

	"github.com/ElandGroup/joblog"
	"log"
	"github.com/ElandGroup/joblog"
	"log"
	"github.com/ElandGroup/joblog"
	"log"
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/viper"
)

var jobLog *joblog.JobLog
const (
	YMLNAMEPROJEC        = "project"
	YMLNAMEDOCKERCOMPOSE = "docker-compose"
	CONFIGNAMENGINX      = "default"
	SUFSERVER            = "-server"
	PRETEST              = "rtc-"

	TEMP_FILE        = "temp"
	EventBroker_Name = "event-broker-kafka"
	jobLogUrl        = "https://gateway.p2shop.com.cn/batchjob-api/v1/jobs"
)

Code after ctrl + x

package cmd

import (
	"github.com/ElandGroup/joblog"
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/viper"
)

var jobLog *joblog.JobLog

const (
	YMLNAMEPROJEC        = "project"
	YMLNAMEDOCKERCOMPOSE = "docker-compose"
	CONFIGNAMENGINX      = "default"
	SUFSERVER            = "-server"
	PRETEST              = "rtc-"

	TEMP_FILE        = "temp"
	EventBroker_Name = "event-broker-kafka"
)

Code after ctrl + s

package cmd

import (
	_ "github.com/go-sql-driver/mysql"
const (
	YMLNAMEPROJEC        = "project"
	"log"
	YMLNAMEDOCKERCOMPOSE = "docker-compose"
	CONFIGNAMENGINX      = "default"
	SUFSERVER            = "-server"

	PRETEST              = "rtc-"

	TEMP_FILE        = "temp"
	EventBroker_Name = "event-broker-kafka"
)

golang.org/x/tools/gopls v0.1.7
    golang.org/x/tools/gopls@v0.1.7 h1:YwKf8t9h69++qCtVmc2q6fVuetFXmmu9LKoPMYLZid4=

PS D:\source\gopath\src\rtc-api> gopls -debug
flag needs an argument: -debug
The Go Language source tools.

Usage: gopls [flags] [command-flags] [command-args]

Available commands are:
serve : run a server for Go code using the Language Server Protocol
bug : report a bug in gopls
check : show diagnostic results for the specified file
format : format the code according to the go standard
query : answer queries about go source code
version : print the gopls version information

    Serve debug information on the supplied address

-listen string
address on which to listen for remote connections
-logfile string
filename to log to. if value is "auto", then logging to a default output file is enabled
-mode string
no effect
-ocagent string
The address of the ocagent, or off (default "off")
-port int
port on which to run gopls for debugging purposes
-profile.cpu string
write CPU profile to this file
-profile.mem string
write memory profile to this file
-profile.trace string
write trace log to this file
-remote string
EXPERIMENTAL - forward all commands to a remote lsp
-rpc.trace
Print the full rpc trace in lsp inspector format
-v Verbose output
PS D:\source\gopath\src\rtc-api>
PS D:\source\gopath\src\rtc-api> gopls bug
Please file a new issue at golang.org/issue/new using this template:

Please answer these questions before submitting your issue. Thanks!

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is better.
A failing unit test is the best.

What did you expect to see?

set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\source\gopath
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\ADMINI~1\AppData\Local\Temp\go-build513634462=/tmp/go-build -gno-record-gcc-switches



vscode version: 1.38.1

{
"explorer.confirmDragAndDrop": false,
"git.enableSmartCommit": true,
"window.zoomLevel": 1,
"git.autofetch": true,
"workbench.colorTheme": "Visual Studio Light",
"javascript.updateImportsOnFileMove.enabled": "never",
"terminal.integrated.rendererType": "dom",
"terminal.integrated.shell.windows": "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe",
"python.jediEnabled": false,
"editor.suggestSelection": "first",
"vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
"extensions.ignoreRecommendations": true,
"editor.minimap.enabled": true,
"workbench.statusBar.visible": true,
"files.eol": "\n",
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"go.formatTool": "goimports",
"go.useLanguageServer": true
}




@stamblerre
Copy link
Contributor

Can you share your gopls logs from when the error occurs? (See how to do this here.) It looks like you don't have the formatting configurations for gopls set, so I wonder if this is a combination of gopls interacting with VSCode-Go. Does the error still occur if you've set the following?

"[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true,
    }
}

@relax-admin
Copy link
Author

relax-admin commented Sep 25, 2019

@stamblerre
I applied your configuration, but it doesn't seem to work, I found a way on the Internet, and after a while, I will give you feedback.

"files.autoSave": "afterDelay",
    "go.goroot": "C:/Go",
    "go.gopath": "C:/Code/MyGoWorks",
    "go.useLanguageServer": true,
    "go.languageServerExperimentalFeatures": {
        "format": false,
        "autoComplete": true,
        "rename": true,
        "goToDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "goToTypeDefinition": true,
        "goToImplementation": true,
        "documentSymbols": true,
        "workspaceSymbols": true,
        "findReferences": true,
        "diagnostics": false
    },
    "http.proxy": "http://127.0.0.1:1080",
    "[go]": {
        "editor.defaultFormatter": "ms-vscode.Go"
    },

reference: https://fullstack.love/achieve/21

@relax-admin
Copy link
Author

relax-admin commented Sep 25, 2019

The problem still appears,Is this the log you expect to see?

I guess because I didn't use the go module, but gopls seems to be the go module mode. If things are really like what I guess, how can I solve it?

before ctrl + s

package cmd

 
var env struct {
	RtcApiUrl string
}

func SetEnv(envStr string){
	switch envStr {
		case "qa":
			env.RtcApiUrl="https://qa.p2shop.com.cn/pangpang-common/rtc-api"
		case "production":
			env.RtcApiUrl="https://gateway.p2shop.com.cn/pangpang-common/rtc-api"
		fallthrough
	default:

	}
}

after ctrl +s

package cmd

	RtcApiUrl string
}

func SetEnv(envStr string) {
func SetEnv(envStr string) {
	case "qa":
	case "qa":
		env.RtcApiUrl = "https://qa.p2shop.com.cn/pangpang-common/rtc-api"
	case "production":
		env.RtcApiUrl = "https://gateway.p2shop.com.cn/pangpang-common/rtc-api"
	default:

	}
}

vscode settings.jso

"files.autoSave": "afterDelay",
    "go.goroot": "C:/Go",
    "go.gopath": "C:/Code/MyGoWorks",
    "go.useLanguageServer": true,
    "go.languageServerExperimentalFeatures": {
        "format": false,
        "autoComplete": true,
        "rename": true,
        "goToDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "goToTypeDefinition": true,
        "goToImplementation": true,
        "documentSymbols": true,
        "workspaceSymbols": true,
        "findReferences": true,
        "diagnostics": false
    },
    "http.proxy": "http://127.0.0.1:1080",
    "[go]": {
        "editor.defaultFormatter": "ms-vscode.Go"
    },
gopls --rpc.trace -v check D:\source\gopath\src\rtc-api\cmd\env.go

2019/09/25 17:40:27 Info:2019/09/25 17:40:26 Build info
----------
golang.org/x/tools/gopls v0.1.7
    golang.org/x/tools/gopls@v0.1.7 h1:YwKf8t9h69++qCtVmc2q6fVuetFXmmu9LKoPMYLZid4=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
    golang.org/x/tools@v0.0.0-20190918214516-5a1a30219888 h1:ER45Jz0UDQ3e6em1lwXVwuPf96lvyQogb7m+gEbsoPg=
    golang.org/x/xerrors@v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc=

Go info
-------
go version go1.12.5 windows/amd64

set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
"GOMOD", stderr: <<>>
2019/09/25 17:40:27 Info:2019/09/25 17:40:27 292.0028ms for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "env" 
"GOPATH", stderr: <<>>
2019/09/25 17:40:27 Info:2019/09/25 17:40:27 375.0029ms for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "env" 
"GOMOD", stderr: <<>>
2019/09/25 17:40:28 Info:2019/09/25 17:40:28 429.0031ms for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "list" "-m" "-json" "all", stderr: <<go list -m: not using modules
>>
2019/09/25 17:40:28 Info:2019/09/25 17:40:28 191.999ms for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "env" "GOPATH", stderr: <<>>
2019/09/25 17:40:35 Info:2019/09/25 17:40:35 7.1759994s for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "D:\\source\\gopath\\src\\rtc-api\\cmd", stderr: <<>>       
2019/09/25 17:40:35 Info:2019/09/25 17:40:35 go/packages.Load
        packages = 1
2019/09/25 17:40:35 Info:2019/09/25 17:40:35 go/packages.Load
        package = rtc-api/cmd
        files = [D:\source\gopath\src\rtc-api\cmd\compose.go D:\source\gopath\src\rtc-api\cmd\enum.go D:\source\gopath\src\rtc-api\cmd\env.go D:\source\gopath\src\rtc-api\cmd\file.go D:\source\gopath\src\rtc-api\cmd\flag.go D:\source\gopath\src\rtc-api\cmd\project.go D:\source\gopath\src\rtc-api\cmd\start.go D:\source\gopath\src\rtc-api\cmd\utils.go]
D:\source\gopath\src\rtc-api\cmd\env.go:3:2: expected declaration, found RtcApiUrl
PS D:\source\gopath\src\rtc-api>opath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "list" "-m" "-json" "all", stderr: <<go list -m: not using modules
>>
2019/09/25 17:36:41 Info:2019/09/25 17:36:41 136.999ms for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "env" "GOPATH", stderr: <<>>
2019/09/25 17:36:43 Info:2019/09/25 17:36:43 2.6235343s for GOROOT=C:\Go GOPATH=D:\source\gopath GO111MODULE= PWD=D:\source\gopath\src\rtc-api go "list" "-e" "-json" "-compiled=true" "-test=true" "-export=false" "-deps=true" "-find=false" "--" "D:\\source\\gopath\\src\\rtc-api\\cmd", stderr: <<>>       
2019/09/25 17:36:43 Info:2019/09/25 17:36:43 go/packages.Load
        packages = 1
2019/09/25 17:36:43 Info:2019/09/25 17:36:43 go/packages.Load
        package = rtc-api/cmd
        files = [D:\source\gopath\src\rtc-api\cmd\compose.go D:\source\gopath\src\rtc-api\cmd\enum.go D:\source\gopath\src\rtc-api\cmd\env.go D:\source\gopath\src\rtc-api\cmd\file.go D:\source\gopath\src\rtc-api\cmd\flag.go D:\source\gopath\src\rtc-api\cmd\project.go D:\source\gopath\src\rtc-api\cmd\start.go D:\source\gopath\src\rtc-api\cmd\utils.go]

@stamblerre
Copy link
Contributor

stamblerre commented Sep 25, 2019

It looks like you have disabled the formatting feature from gopls, so if you still see this issue it means it's not coming from gopls. Can you run gopls -v -rpc.trace format path/to/file.go?

To get full logs from gopls (and to confirm if gopls is doing the formatting), add the following to your settings:

"go.languageServerFlags": [
    "-rpc.trace", // for more detailed debug logging
    "serve",
],

Then find the logs, by going to View -> Output -> Tasks -> gopls.

@stamblerre
Copy link
Contributor

Duplicate of #34955

@stamblerre stamblerre marked this as a duplicate of #34955 Oct 17, 2019
@stamblerre
Copy link
Contributor

Let's move the discussion of this issue to #34955, as I'm hoping to consolidate all of the error reports from users experiencing this issue. Please follow the suggestions in that issue.

@golang golang locked and limited conversation to collaborators Oct 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants