csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Files formatted by CSharpier.MsBuild do not reload before VS debugger hits breakpoints.

Open belav opened this issue 3 years ago • 5 comments

This is related to #674

This appears to be a problem specific to VS2022, I tested on 17.3.0 and 17.3.5. Rider does not have this problem.

  1. Start a new project.
  2. Add a file like
using System;
using System.Diagnostics;

namespace HelloWorld;

class HelloWorld
{
    static void Main(string[] args)
    {
        var x = 0;
        x = 1; x = 2; Debugger.Break();
        x = 3;
    }
}
  1. Add csharpier.msbuild version 0.20.0 to the project.
  2. Debug the project.

VS complains that The source file is different from when the module was built

It is possible to verify that CSharpier formats the file before the compile by looking at the output of dotnet build -v d. There is a setting in VS for Detect when file is changed outside the environment, this was set to on. Adding the equivalent of a sleep for 10s after csharpier.msbuild runs, the file in VS still does not update. You can verify it was changed with notepad++. Using version 17.3.5, the results are a little different, but debugging still does not work.

Method of getting the build to sleep after csharpier formats.

<UsingTask TaskName="Sleep" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
    <Delay ParameterType="System.Int32" Required="true" />
  </ParameterGroup>
  <Task>
    <Code Type="Fragment" Language="cs">
       <![CDATA[
System.Threading.Thread.Sleep(this.Delay);
]]>
    </Code>
  </Task>
</UsingTask>

<Target Name="Sleep" AfterTargets="CSharpierFormat">
  <Sleep Delay="10000"/>
</Target>

belav avatar Oct 10 '22 16:10 belav

@OneCyrus I dug into this more, and it seems to be a problem specific to VS. I'm not sure if there is some other way to get VS to detect the file change, but it kind of feels like a bug in VS.

One workaround would be to use the VS extension for CSharpier and enable format on save.

belav avatar Oct 10 '22 16:10 belav

just tried to debug some more and it looks like the issue is really that Visual Studio doesn't update the code window in "real time". actually when VS looses the focus it doesn't update the code window at all. But once i give the Visual Studio window the focus again (clicking anywhere to get it into the foreground) it instantly updates the code window.

so it looks like they implemented some perf optimizations to only update the windows when in foreground and not in the background. but that's quite unfortunate for this use case.

OneCyrus avatar Oct 18 '22 17:10 OneCyrus

I reported this on the Visual Studio feedback page. Some upvotes could help to get attention. https://developercommunity.visualstudio.com/t/Detect-when-file-is-changed-outside-the/10183731

OneCyrus avatar Oct 27 '22 09:10 OneCyrus

I reported this on the Visual Studio feedback page. Some upvotes could help to get attention. https://developercommunity.visualstudio.com/t/Detect-when-file-is-changed-outside-the/10183731

Looks like they've fixed it: https://developercommunity.visualstudio.com/t/Visual-Studio-clears-dirty-flag-before-c/10124978

@OneCyrus could you please confirm this works as expected now?

shocklateboy92 avatar Jan 21 '23 00:01 shocklateboy92

that is something else. it's not fixed yet.

OneCyrus avatar Jan 21 '23 08:01 OneCyrus