pedropombeiro / ReSharper.AutoFormatOnSave

An addin for Visual Studio which causes the documents to be reformated on save
http://blog.pedropombeiro.com/keeping-code-formatted-the-easy-way/
MIT License
27 stars 20 forks source link

Timing Issue #24

Open aetaylor opened 7 years ago

aetaylor commented 7 years ago

I appear to having a timing issue when I use this. I save the file, the file saves, the reformat occurs and the file is marked as "dirty" again. Is there a setting I need to change to get the reformat to occur before tha save?

Jogge commented 7 years ago

I have the exact same issue. I have upgraded to VS17, and the problem still occurs.

In large files, I experience an even worse bug: On saving, the code gets saved, and the reformat happens after the code is saved. The code is then marked as dirty, because of changes from the reformat action. When saving again the code gets saved, and reformat happens again, and the code is marked as dirty again, even though reformat also were done in the previous save. It makes it impossible to save the file, without cancelling the auto reformat action.

aetaylor commented 7 years ago

I have disabled this plug-in for now and I'm using the re-format on save option in CodeMaid instead which works as expected.

pavlo-liapota commented 7 years ago

I got this issue when I upgraded ReSharper from 2017.1.1 to 2017.1.3. I rolled back to 2017.1.1 version and extension works fine again.

dro123 commented 7 years ago

https://youtrack.jetbrains.com/issue/RSRP-465536

NelsonRothermel commented 6 years ago

Until ReSharper provides a better alternative, is there any way for this extension to watch/wait for the file to be modified before the final save? Maybe ReSharper code clean always changes the file modified time, so you would check every second to see if the modified time is within x seconds. It's hacky, but it would be better than what we currently have.

dro123 commented 6 years ago

The file time will not change after the cleanup ran, because the file is not saved. The only way this could be done is run cleanup and save, wait a few seconds and check if the file is not saved and then save again. But there's a high chance, that the change was not only the cleanup but other stuff, that you didn't want to save. Or that the cleanup is still running. Or the user immediately started a build and the file is saved after being compiled. Really horrible race conditions. This can only be fixed within ReSharper.

LorneCash commented 5 years ago

Right after the CodeCleanup has run couldn't you just disable the OnDocumentSaved event then save the file and re-enable the event?

Alternatively or if that doesn't work couldn't you save a list of documents to do the CodeCleanup on (looks like this is already being done) then run it on all of them, then save each one and if the document is in the list that just ran skip the cleanup this time. Finally you would need to clear the list so that they don't get skipped accidentally if more changes are made.

Update: I've done more reading, I see now that the problem is due to CodeCleanup being changed to Async.

Grahamvs commented 5 years ago

@aetaylor , I believe this is due to Visual Studio's API only triggering the extension AFTER it has already saved the document (hence the need to save it again).

@NelsonRothermel , implementing an automatic second save is dangerous, as calling save in code will trigger another format. This can potentially cause an endless loop of saving and formatting (for example, if you have both CodeMaid and ReSharper.AutoFormatOnSave listening for the Saved event, you might end up with documents that may never be "not dirty").

To be honest, I doubt this will actually get fix, or even be investigated, since as far as I'm aware, the owner is no longer maintaining this extension. It is due to this very reason (and the fact that VS 2019.1 doesn't like it using legacy API), I have (just) created my own extension. Admittedly my extension also suffers from the same issue (hence my insight), however, I am investigating alternative ways to get around this.

I sincerely hope the owner decides to continue maintaining this extension and is able to fix these issues.