microsoft / vscode-go

An extension for VS Code which provides support for the Go language. We have moved to https://github.com/golang/vscode-go
Other
5.93k stars 646 forks source link

Windows 10 / VSCode 1.6.1 - gofmt -w — updated file does not visually refresh in Editor #583

Closed ITSecMedia closed 7 years ago

ITSecMedia commented 8 years ago

I've noticed a new bug on my system Not sure if it's caused by a VSCode bug or the extension.

Using all latest versions: Windows 10 updates VSCode version VSCode Extension and go packages updates.

My settings:

 "files.autoSave": "off",
"go.goroot": "C:\\Tools\\go",
"go.gopath": "R:\\go",
"go.buildOnSave": true,
"go.formatFlags": ["-w"],
"go.formatOnSave": true,
"go.buildFlags": [],
"go.buildTags": "",
"go.coverOnSave": true,
"go.formatTool": "goreturns",
"go.gocodeAutoBuild": true,
"go.lintFlags": [],
"go.lintOnSave": true,
"go.useCodeSnippetsOnFunctionSuggest": true,
"go.vetFlags": [],
"go.vetOnSave": true,

When I manually trigger save, the go file gets properly formatted but it's not shown in VSCode until I close and reopen it or switch to another file and go back.

When I instantly do a text-formatting change again ( adding a tap or few spaces ) and click save I get a DIFF report, because the file has been changed

Failed to save 'main.go': The content on disk is newer. Click on Compare to compare your version with the one on disk

What seems to work is, if I click 2 or three times save always waiting a second or two, then the file gets refreshed...

No matter if I use gofmt or goreturns ... seems to be general refresh issue.

BUT : If I edit the file in another parallel opened Texteditor like EmEditor ... VSCode directly updates the file.

So in general instant update/refresh seems to work?!

dring1 commented 8 years ago

Was also able to reproduce this locally using v0.6.47, windows 10, Go 1.7.1

ramya-rao-a commented 7 years ago

@dring1 @ITSecMedia Any reason you are using the -w flag? This flag is intended to write the formatted content on disk, where as internally we use the -d option so that the formatting tool gives diff output which the extension uses to apply the edits on the document

dring1 commented 7 years ago

Removing all flags, no formatting is taking place. This is my following config:

    "editor.formatOnSave": true,
    "go.buildOnSave": true,
    "go.lintOnSave": true,
    "go.vetOnSave": true,
    "go.buildTags": "",
    "go.buildFlags": [],
    "go.lintTool": "golint",
    "go.lintFlags": [],
    "go.vetFlags": [],
    "go.coverOnSave":false,
    "go.useCodeSnippetsOnFunctionSuggest": true,
    "go.formatOnSave": true, 
    "go.formatTool": "goreturns",
    "go.formatFlags": [],
    "go.gocodeAutoBuild": false,
    "go.autocompleteUnimportedPackages": true

Running go fmt . through a terminal will result in the file being formatted correctly.

From the output panel I only see the following

Finished running tool: C:\Go\bin\golint.exe

Finished running tool: C:\tools\go\bin\go.exe tool vet c:\Go\src\github.com\dring1\test\test.go

Finished running tool: C:\tools\go\bin\go.exe build -o C:\Users\DEVON~1.RIN\AppData\Local\Temp\go-code-check -tags "" .
ramya-rao-a commented 7 years ago

@dring1 Can you set go.useDiffForFormatting to false and try again ?

dring1 commented 7 years ago

This appears to be doing the trick! Thank you!

Although auto-import does not appear to be working ( if that is expected functionality)

Finished running tool: C:\tools\go\bin\go.exe build -o C:\Users\DEVON~1.RIN\AppData\Local\Temp\go-code-check -tags "" .
c:\Go\src\github.com\dring1\test\test.go:10: undefined: log in log.Printl
ITSecMedia commented 7 years ago

On my windows machine the setting [ "go.useDiffForFormatting": false, ] shows no effect. gofmt and goreturns both don't do the -w formatting inside VSCode, But when I call gofmt -w main.go in VSCode terminal, the file gets reloaded / formatted.

My settings are

    "go.useDiffForFormatting": false,
    "go.buildFlags": [],
    "go.buildOnSave": true,
    "go.buildTags": "",
    "go.coverOnSave": true,
    "go.formatFlags": ["-w"],
    "go.formatOnSave": true,
    "go.formatTool": "goreturns",
    "go.gocodeAutoBuild": true,
    "go.gopath": "D:\\go",
    "go.goroot": "C:\\Tools\\go",
    "go.lintFlags": [],
    "go.lintOnSave": true,
    "go.lintTool": "golint",
    "go.useCodeSnippetsOnFunctionSuggest": true,
    "go.vetFlags": [],
    "go.vetOnSave": false,
ramya-rao-a commented 7 years ago

@ITSecMedia As long as you use -w in the formatflags, things will not work as expected as explained here https://github.com/Microsoft/vscode-go/issues/583#issuecomment-260718842

I will be updating the code to ignore this flag altogether later this week. Can you please remove the w flag and try?

@dring1 The reason setting go.useDiffForFormatting to false worked for you is due to an upstream issue: https://github.com/sqs/goreturns/issues/28 Can you update/upvote that so that we get some traction there?

@dring1 Auto-import should work for as long as the formatting tool is not gofmt. Can you confirm that? If that still does not work, can you open a separate issue?

dring1 commented 7 years ago

Can do, thank you for your help.

ITSecMedia commented 7 years ago

@ramya-rao-a I appreciate hint #583 ! I haven't seen that one, been busy all day long ... :( It's working now. 👍 Thank you very much.

ramya-rao-a commented 7 years ago

Glad I could help, Happy Coding!

ramya-rao-a commented 7 years ago

Have updated the code to ignore the -w flag with this: https://github.com/Microsoft/vscode-go/commit/727346e9a25044730e458f7de2bb86ea8882f394