-
Notifications
You must be signed in to change notification settings - Fork 18k
x/tools/gopls: formatting resets cursor after save with CRLF line endings #31937
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
Comments
Does this happen often or just a single time? Do you have a case that could reproduce it? |
It happens every time, a condition I have found is that it must be in a Go module. I have not tested with gopath yet, but when editing Go files that are not in Go modules, the editor does nothing on save and the cursor isn't affected. I haven't gotten around to testing it more yet. It appeared that it formatted the code even when I had turned "format on save" off. It happens on my work computer, I haven't tested on my home computer yet but I'll get back when I do. |
My initial guess is that it has something to do with your configurations with You could try explicitly turning off formatting and import organization through "go.languageServerExperimentalFeatures": {
"format": false
},
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
}, |
It looks like if either When both are set to My configuration where it is "working"{ "git.autofetch": true, "files.associations": { "*.tmpl": "gotemplate", "*.html.tmpl": "gohtml", }, "terminal.integrated.shell.windows": "C:\\Program Files\\Git\\bin\\bash.exe", "go.useLanguageServer": true, "go.languageServerExperimentalFeatures": { "format": false, "diagnostics": true // for diagnostics as you type }, "[go]": { "editor.codeActionsOnSave": { "source.organizeImports": false }, }, "gopls": { "usePlaceholders": true, // add parameter placeholders when completing a function "enhancedHover": true, // experimental to improve quality of hover (will be on by default soon) }, "go.lintOnSave": "off", "editor.formatOnSave": true } Edit: |
I should have asked earlier - when was the last time you updated |
I ran the command and it looks like nothing has changed |
Thanks for letting me know. My next guess is that perhaps it has something to do with the fact that you are on Windows? Can you try switching to Unix line endings by adding |
Well that changed something - the cursor now resets to the beginning of the line rather than the beginning of the file. |
Hm, that is so strange. I've never heard of anyone experiencing this kind of behavior on Windows. I will try to reproduce this and see if I can come up with a fix. |
Yeah, it's strange for sure. Doesn't happen on my home machine which really confuses me. |
Scratch that - it's happening on my home machine too now. Conditions to reproduce for me:
Weird behaviors:
|
So the reason I think that this happens even though the settings are set to false is that VSCode-Go will fall back to its own formatting mechanism, so it's just running I was actually able to reproduce the jumping to the beginning of the line behavior on my own Windows machine, as well as on my Mac in some cases. I will investigate further, but I'm starting to think this might not be a Can you give an example of a case where you would expect the cursor to stay put, but it instead gets moved to the beginning of the line? |
Ok, I have been able to repro the cursor resetting to the start of the file behavior. It has to do with CRLF line endings - I will investigate this further now that I have a solid repro. |
I'm glad you could reproduce and that I'm not just crazy haha. Some more information:
|
It seems that this is a VSCode issue. For now, the correct workaround is to add |
That makes sense. Were you ever able to reproduce the line reset bug with LF line endings? |
Yes, I think the line reset is another consequence of this behavior. Since the diffs are line by line, we just delete a line and replace it, so if VSCode doesn't understand how to replace the cursor, it just puts it back to the beginning of the line. |
+1 I have the same problem |
There is a temporary workaround for this issue (setting Also, a reminder to please not comment +1 on issues, but rather to use Github's upvoting mechanism: https://groups.google.com/forum/#!topic/golang-dev/OirJxPzfgug. |
I having issue when save, the cursor goes to start of the file on a vscode in a ubuntu machine
|
@tweakmy For now there seems to be no workaround to the line resetting other than disabling the language server |
FTR:
disables formatting through |
Setting |
I am actually seeing the opposite effect. On windows when I save the cursor goes to the top of the file. Its a very odd behavior for sure. Disabling formatting via gopls as @Xjs suggested removes this problem; so it must be related. Just an fyi, when I say it moves the cursor to the top of the file, I mean it moves it right to the left of the p in package. |
@tensor-programming that is not the opposite effect, that is exactly what this bug is describing |
I just want to throw in there's another, not so drastic, workaround: Using EditorConfig Create a file named root = true
[*.go]
end_of_line = lf And install the EditorConfig extension. What this does is automatically setting the EOL to LF for every .go file in the project. ( I've tested it and it works (once your file uses LF). The cursor still jumps to the beginning of a line if it's inside a line that gets formatted, but it's better than nothing. Otherwise it stays just where it is. If you don't know what EditorConfig is: Using EditorConfig is actually pretty common in web development (HTML/CSS/JS). Some editors even have built-in support for it (including Visual Studio). |
Change https://golang.org/cl/191018 mentions this issue: |
this supports sub-line diffs and is much faster Fixes golang/go#33003 Fixes golang/go#32586 Updates golang/go#31937 Change-Id: I02f82c75828e7e3ec804e8beee916893d4c14b3d Reviewed-on: https://go-review.googlesource.com/c/tools/+/191018 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
It is not clear what the status of this bug is. The external references seem to all be closed, and only this one remains that I can find reporting this bug (which is still present): #32586 (this is closed) |
This bug can be closed as well, I believe. The next version of |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Saved a .go file
What did you expect to see?
My cursor should stay in the same place after formatting
What did you see instead?
My cursor is reset to the beginning of the file. I have tried toggling the
[go] > editor.codeActionsOnSave
andgo.lintOnSave
settings to false and it still occurs.VSCode Settings
Go extension: 0.10.2
The text was updated successfully, but these errors were encountered: