Elders/VSE-FormatDocumentOnSave

[VS2022] CSHTML files are not saving when extension is enabled

grandslammer opened this issue ยท 6 comments

I have recently noticed an issue where CSHTML are not saving when the extension is enabled in VS2022 (using 2022 version of the extension).

When I save a CSHTML file using either CTRL+S or from the file menu, the asterix in the open file tab disappears for a moment and then immediately reappears.
Also, when I close VS, I get the popup asking if I would like to save the files before closing.

The issue only seems to be affecting CSHTML files, however I have only tested it with CSHTML and CS files, so I don't know if other file types are also affected.

I am unable to reproduce the issue if I disable the extension.

NOTE: I am also experiencing the exact same issue another format on save extension. I have created an issue there also - Here

image

image

Yes, there are some breaking changes in the visual studio and everything related to XML languages is not working properly. There is no easy way to fix this. The real issue is that the formatter is now externalized and executed in parallel but it is really slow. The format result could exceed 1 second.

I have no solution right now for this.

I am facing the same problem as @grandslammer and it is especially bad when the file is checked into git (you commit but the file changes again...if you undo changes, the file changes again so git shows it as changed and the cycle has to repeat).

@mynkow would it solve the problem if you tracked the contents of the document and only executed the "Format Document" command if the contents had changed since the last format attempt?

I was thinking of many workarounds with no success. The main issue is that the formatting is executed in another thread or process where I have no access or information that the operation has completed. It will be really awful if we add a save after some time, like 500ms, but we could try and see what will be the experience (I could make a custom build and try it).

Here is the flow I am seeing:

  1. The user saves a document
  2. The extension intercepts the invocation and formats the document prior save
    2.1. Visual Studio delegates the formatting to something else
    2.2. The format command returns success
  3. The document is saved
  4. The process which formats the document finishes and changes the contents of the file and makes it dirty.

It is normal the other extensions to not work as well because they are clones of this one. But if someone finds a solution I will add it here asap.

@mynkow many thanks for following up and for your effort on this handy extension!

I was reading the extension's docs and the section on disabling the extension has provided a simple workaround for me:

  1. Save the document as normal so it can be formatted (it will be marked dirty immediately after).
  2. Turn on CapsLock to temporarily disable the extension.
  3. Save the document again while CapsLock is on (this will clear the dirty state).

It's a little inconvenient, but I can live with it because the extension helps ensure I don't forget to format documents before committing them to git ๐Ÿ‘

Thanks again for your help!

hmm, this is interesting. It will not be perfect but combining all suggestions above we could have:

  1. The user saves a document
  2. The extension intercepts the invocation and formats the document prior save
    2.1. Visual Studio delegates the formatting to something else
    2.2. The format command returns success
    2.3. Track the document as @ericmutta suggested
    2.4. Schedule a document save command after 500ms (this MUST be a configurable value per project)
  3. The document is saved
  4. The process which formats the document finishes and changes the contents of the file and makes it dirty.
  5. The scheduled save command kicks in and because we are tracking the document we will ignore the format command. Just save.

I really do not like it. :( unless @madskristensen comes and saves us!

This has been fixed. Sometimes it is not the best experience but it was working till this has happened => #47