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

Format on save doesn't work anymore #1419

Closed
tapir opened this issue Dec 19, 2017 · 41 comments
Closed

Format on save doesn't work anymore #1419

tapir opened this issue Dec 19, 2017 · 41 comments

Comments

@tapir
Copy link

tapir commented Dec 19, 2017

I've just noticed that go doesn't automatically format anymore.
I've also noticed that now go.formatOnSave is deprecated hence I'vee switched to editor.formatOnSave: true yet it still doesn't work.

@ramya-rao-a
Copy link
Contributor

Can you try

"[go]": {
   "editor.formatOnSave": true
}

Remember that Workspace Settings will override Global/User Settings.

Can you also try on a smaller package separately?
If the formatter takes more than 750ms then it is skipped as slow processes like that can end up adding edits to file after manual edits from user

@tapir
Copy link
Author

tapir commented Dec 20, 2017

Hi,

tried above setting on 2 different computer (windows and linux) with multiple size projects including very small ones. still no luck.

@tapir
Copy link
Author

tapir commented Dec 20, 2017

Actually, now I notice, other facilities such as automatically arranging imports, go to definition etc don't work either. Here is my global settings file (there is no workspace specific setting)

// Place your settings in this file to overwrite the default settings
{
    "go.lintTool": "gometalinter",
    "git.confirmSync": false,
    "extensions.ignoreRecommendations": true,
    "workbench.startupEditor": "newUntitledFile",
    "git.autofetch": true,
    "[go]": {
        "editor.formatOnSave": true
     }
}

@gngchrs
Copy link

gngchrs commented Dec 20, 2017

Release notes say go.formatOnSave is deprecated. Tried the above setting by @ramya-rao-a but still no luck.
Also tried adding "go.formatFlags": ["-w"], since its using goreturns, but no luck.
Turns out the -w flag is ignored by design

Output on VS CODE only shows that go vet and go lint have been run

Using MacOS

@ramya-rao-a
Copy link
Contributor

Does manual formatting work?

@tapir
Copy link
Author

tapir commented Dec 20, 2017

It does, yes.

@esinek
Copy link

esinek commented Dec 20, 2017

Same here. Running gofmt from the cmd line works. Save on format isn't working. Option-Shift-F (the default format file in MacOS, which, I believe should use gofmt) doesn't format file either.
When I select some lines and do Cmd-K, Cmd-F, I get the following message:
Sorry, but there is no formatter for 'go'-files installed.
this is with the following:

    "editor.formatOnSave": true,
    "go.formatTool": "gofmt",
    "[go]": {
        "editor.formatOnSave": true
    }

looking at the output console there is no gofmt as mentioned by @gangachris :

Finished running tool: /Users/me/workspace/myapp/bin/golint

Finished running tool: /usr/local/go/bin/go vet ./...

Finished running tool: /usr/local/go/bin/go build -i -o /var/folders/1z/3p0xvm1125941j03c7jr8vm7wfr3q/T/go-code-check mystuff

NOTE: uninstalled and installed 0.6.69 from VSIX and it's working again

@ramya-rao-a
Copy link
Contributor

Ok, then it's the case of formatter taking more time than 750ms.

I am currently on vacation. A few things you can do

  • stay on the current version, run manual formatting when needed. When I get back, I can look at bringing back the old behavior as a fall back, but it will bring back the 5 issues that the new model is fixing
  • or you can revert to the previous version. Get the vsix file from the previous release https://github.com/Microsoft/vscode-go/releases/tag/0.6.69 and run "code --install-extension Go-0.6.69.vsix" and then disable aauto update of extensions

@tapir
Copy link
Author

tapir commented Dec 20, 2017

Ok thanks.

I'm not convinced it's the 750ms issue though because even a very simple hello world can't be formatted on my computer.

Have a nice vacation.

@philipithomas
Copy link

I'm also having this issue all of a sudden. No formatting or goimports being run.

@gngchrs
Copy link

gngchrs commented Dec 20, 2017

@philipithomas had to revert to 0.6.69 like @ramya-rao-a suggested.

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Dec 21, 2017

@esinek manual formatting shldnt be affected. I don't recall the keyboard shortcut in Mac for formatting. Can you try the formatting cmd from the cmd pallet or right click on editor and choose "format document"

@tapir That does sound suspicious. Can you drop a few console.log() statements in the file "$HOME/.vscode/extensions/lukehoban.go-0.6.70/out/src/goFormat.js" and see if formatting provider is being called?

  • Add console.log(timeTaken); just before the return resolve(textEdits);.
  • Save the file and reload VS Code
  • Edit a Go file and save it.
  • Check the console for the logs. Help -> Toggle Developer Tools -> Console

@egorovli
Copy link

egorovli commented Dec 21, 2017

Also missing go.formatOnSave which was fine until I had a variable name collision with some package which gets automatically imported :(

out2

config is defined in main.go, trying to save routes.go which uses this config variable, but instead package github.com/prometheus/common/config gets imported.

Update

Fixed it changing settings to:

"go.formatTool": "goimports"

@lunny
Copy link

lunny commented Dec 21, 2017

here only gofmt work, but goimports and goreturns didn't work.

@andrevoget
Copy link

andrevoget commented Dec 22, 2017

I have the same problem. Neither

"go.formatTool": "gofmt"
"go.formatTool": "goimports"
"go.formatTool": "goreturns"

work all of a sudden. Installing 0.6.69 fixed the problem.

@ramya-rao-a Keep up the good work! Please don't interrupt your well deserved vacation as there is a workaround.

@nathj07
Copy link

nathj07 commented Dec 24, 2017

This is the same behaviour I am seeing:
OS: MacOS Sierra
VS-Code: 1.19.1
Extension: 0.6.70
goimports works from the command line, and on save it will add/remove imports from the standard library but nothing from the project, the GOPATH, or the vendored directory.

I too am on vacation so no real rush, just thought a little more info may help. Enjoy the holidays.

@iwaseyusuke
Copy link

On my environment, formatting on save does not work when problems of linter are too much (exceed the max num of the problems (>100?)).

Can I increase the max num of problems like "python.linting.maxNumberOfProblems"?
Or force to run format tool even if problems are too much?

@tapir
Copy link
Author

tapir commented Dec 25, 2017

Strangely started working on my Linux computer. Did nothing or maybe I've updated the whole VSCode package (that I'm not sure as I did not pay attention to what's in the update)

Windows computer is still not working with the latest version of VSCode

@ramya-rao-a
Copy link
Contributor

@egorovli The reason for what you are seeing is explained in #613 (comment)

@iwaseyusuke The formatting tool does not depend on what the linter does. The format on save feature however is aborted when formatting takes more than 750ms.

@tapir Did you use the same codebase on Linux and Windows for comparison? It could be that the formatting tool is slower in Windows when compared to Linux? Also, did you get a chance to follow the steps in #1419 (comment) and get some logging on the time taken by formatting?

@nathj07 Adding/removing of imports just from std lib and not from GOPATH seems unrelated to this issue. Does the same happen when you are on the 0.6.69 version of the Go extension? See #1419 (comment) for instructions on reverting to 0.6.69.

@iwaseyusuke
Copy link

@ramya-rao-a Thanks! Indeed the file failed to format have too many lines. Can we increase the value of timeout (750ms)?

@nathj07
Copy link

nathj07 commented Dec 26, 2017

@ramya-rao-a I have rolled back the extension, that was a simple process thanks for that. It has indeed worked. Thanks for your help.

@ramya-rao-a
Copy link
Contributor

@nathj07 Can you log a separate issue for the adding/removing of imports working only for std lib in the latest version with a sample code that I can use to repro?

@nathj07
Copy link

nathj07 commented Dec 26, 2017

@ramya-rao-a done - #1427 the code I added reproduces what I've been seeing. Thanks for the help.

@tapir
Copy link
Author

tapir commented Dec 27, 2017

Here is the requested change and it's result after a save in Go workspace: https://imgur.com/a/N4UfX
I've also noticed other errors related to Go extension in the console. Will that cause any issues?

For people who doesn't want to look to the screenshot
1- As far as I see, "format" is not engaged at all.
2- VSCode complains that vscode-extension-telemetry missing (which is used by Go extension apparently)

@ramya-rao-a
Copy link
Contributor

@tapir Add a console.log(timeTaken); just before the return resolve(textEdits); Ensure that you have reloaded VS Code after making changes to the goFormat.js file. If you still get the Activating extension "lukehoban.Go" failed message with the missing module, then your current version of the extension seems to be corrupted. Uninstall and Install the extension and try again.

Others on 0.6.70 version and not getting the format on save feature,
Do you see any error messages in the Console when saving the file? Help -> Toggle Developer Tools -> Console

@tapir
Copy link
Author

tapir commented Dec 28, 2017

Re-instelled the extension and everything works again. Don't know why I didn't try that before...
So the actual bug is corruption on extension update then? Does anybody have similar error?

@ramya-rao-a
Copy link
Contributor

@tapir In your case, the issue turned out to be extension activation failure. Meaning, none of the extension features worked

But many others here are seeing features line lint/vet work, but not format on save. So there is still an issue here that I cannot repro.

@iwaseyusuke Can you consistently repro the format on save feature not working on larger projects, and it works on smaller projects?

It will help if any of the others having the format on save issue in 0.6.70 version of the Go extension can follow the instructions in #1419 (comment) and share if indeed the formatting is taking more than 750ms.

@kaixianghu
Copy link

kaixianghu commented Dec 28, 2017

@ramya-rao-a
I am experiencing the same issue of auto format not being run. I have auto save turned on.
Added debug message as suggested in comment#1419, my experiment shows that format function was simply not called, i see no prints of any log when i perform a random edit, and wait for editor to auto save.

if I manually trigger a format, i see time taken to format printed.
I have tried re-installing go plugin, trying gofmt, goimports as formatter, nothing seem to work.
also, auto import stopped working as well, i now need to manually run formatting once in a while to get import and formatter to run

@ramya-rao-a
Copy link
Contributor

ramya-rao-a commented Dec 29, 2017

@kaixianghu Thanks for the extra information about auto-save. Now I can repro!! Can you try disabling the auto-save feature and then seeing if the formatter is being called on manual file save?

Others,
Anybody seeing the format on save issue when auto-save feature is disabled?

@kaixianghu
Copy link

@ramya-rao-a , disabled auto-save feature, manually saving the file does trigger auto-format.

@lunny
Copy link

lunny commented Dec 30, 2017

@ramya-rao-a I haven't enable auto-save and reinstall Go extension resolved my problem.

@iwaseyusuke
Copy link

@ramya-rao-a Sorry for the delay.

Yes, I could reproduce this issue consistently with the large file (over 8000 lines in a single file):

/usr/share/code/resources/app/out/vs/workbench/workbench.main.js:249 [Extension Host] stdout maxBuffer exceeded
/usr/share/code/resources/app/out/vs/workbench/workbench.main.js:249 [Extension Host] Check the console in dev tools to find errors when formatting.

With the file which has about 4000 lines:

/usr/share/code/resources/app/out/vs/workbench/workbench.main.js:249 [Extension Host]% 1925

Also, when succeeded to format:

/usr/share/code/resources/app/out/vs/workbench/workbench.main.js:249 [Extension Host]% 47

Hmmm... it might be better to refactor and split my codes first...

@ramya-rao-a
Copy link
Contributor

@iwaseyusuke I can fix the issue of the formatting tool erroring with the max buffer issue. Are the numbers 1925 and 47 the time taken?

@iwaseyusuke
Copy link

@ramya-rao-a Yes, I just followed your comments and got the outputs of console.

@ramya-rao-a
Copy link
Contributor

Ok, so far there are 4 categories I see forming

  1. Issue with the Go installation where uninstalling and installing the extension again works.
    • These are the cases where in the console you see error messages about extension activation failure
    • Or error about no formatter for go installed
  2. Auto-Save is enabled and set to afterDelay
    • We moved from our own special implementation of format-on save feature to that supported by VS Code. And by design the latter is disabled when auto-save is on. This is to keep cursor and selections stable as you type.
  3. Formatter takes more than 750ms
    • Slow formatters are aborted by the format on save feature of VS Code as they can delay the save operation and cause perf issues.
    • With the latest Go extension (0.6.71), we start logging a message in the console (Help -> Toggle Developer Tools -> Console) if the formatter is taking too long.
  4. Packages being wrongly imported when there are global variables matching the pkg name.
    • With the latest Go extension (0.6.71), you will be prompted to update your goreturns tool after which you shouldn't see this issue.

Anybody seeing anything other than the above 4?

@STiAT
Copy link

STiAT commented Jan 24, 2018

I had the issue of goreturns taking too long to update, which worked perfectly well before I updated this week. "go.formatTool": "gofmt" works though.

@mlitvin
Copy link

mlitvin commented Jan 28, 2018

I have an issue where autosave doesn't format but issuing the "format document" command manually works.

I have files.autoSave, as off, it doesn't look like the 750ms issue (the files are short, and doing it manually is very responsive).

It is linting, building and vetting on save, just not formatting.

I'm running vscode 1.19.3, and go extention 0.6.73

@bofabcd
Copy link

bofabcd commented Jan 28, 2018

[Extension Host] Formatting took too long(1406ms). Format On Save feature could be aborted.

@ramya-rao-a
Copy link
Contributor

@mlitvin Do you see similar error like @bofabcd in #1419 (comment) when the formatting on save doesn't work? You will have to open the console via Help -> Toggle Developer Tools -> Console

@bofabcd Unfortunately, that is the expected outcome of slow formatters currently

@mlitvin
Copy link

mlitvin commented Jan 29, 2018

OK looking at the console I do get the error that it take too long. Sorry.

@ramya-rao-a
Copy link
Contributor

Anybody reaching this issue please see #1419 (comment)

Closing this issue now, as most of the formatting issues have been addressed.

@vscodebot vscodebot bot locked and limited conversation to collaborators Mar 15, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests