adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
565 stars 84 forks source link

Update for debug templates pull request #111

Closed TheAtomicOption closed 8 months ago

TheAtomicOption commented 2 years ago

This is a rebase and enhancement of pull request #83

Many thanks to @shehrozeee for creating the original PR as this is a really useful feature.

In addition to the rebase I've amended the final commit to:

adoconnection commented 2 years ago

Hi, thanks for you PR!

maarno commented 1 year ago

Hi, is there a plan to update & merge this? This would be a very helpful feature.

shehrozeee commented 1 year ago

Let me update it against the latest version and open another pull request.

On Wed, Jan 11, 2023, 12:31 PM MA @.***> wrote:

Hi, is there a plan to update & merge this? This would be a very helpful feature.

— Reply to this email directly, view it on GitHub https://github.com/adoconnection/RazorEngineCore/pull/111#issuecomment-1378340856, or unsubscribe https://github.com/notifications/unsubscribe-auth/AICB5JC2GC4FGMHMXCU7AZLWRZOWVANCNFSM5254CTNA . You are receiving this because you were mentioned.Message ID: @.***>

adoconnection commented 1 year ago

Hi all, sure, this is truly useful feature. I'll review and merge asap. Having busy days 😵‍💫

DashNY commented 1 year ago

👍 Also looking forward to this functionality

chrisdaiii commented 1 year ago

@TheAtomicOption Hello, I have benefited a lot from reading your PR, but what I don't understand is how to find the .cshtml file when vs is running? Where can I learn more about this?

shehrozeee commented 1 year ago

In my original pr I created saved both the pdb and the cshtml the system temp folder. That would be tmp on debian and %temp% on windows.

One the debugger hits, it asks for the pdb file, that's when you point it to the temp folder. It picks up the file automatically from that Point

On Sun, Jul 2, 2023, 5:00 PM chrisdai @.***> wrote:

@TheAtomicOption https://github.com/TheAtomicOption Hello, I have benefited a lot from reading your PR, but what I don't understand is how to find the .cshtml file when vs is running? Where can I learn more about this?

— Reply to this email directly, view it on GitHub https://github.com/adoconnection/RazorEngineCore/pull/111#issuecomment-1616653220, or unsubscribe https://github.com/notifications/unsubscribe-auth/AICB5JH22MBELCKRUDTD4ULXOFWFXANCNFSM5254CTNA . You are receiving this because you were mentioned.Message ID: @.***>

chrisdaiii commented 1 year ago

In my original pr I created saved both the pdb and the cshtml the system temp folder. That would be tmp on debian and %temp% on windows. One the debugger hits, it asks for the pdb file, that's when you point it to the temp folder. It picks up the file automatically from that Point On Sun, Jul 2, 2023, 5:00 PM chrisdai @.> wrote: @TheAtomicOption https://github.com/TheAtomicOption Hello, I have benefited a lot from reading your PR, but what I don't understand is how to find the .cshtml file when vs is running? Where can I learn more about this? — Reply to this email directly, view it on GitHub <#111 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AICB5JH22MBELCKRUDTD4ULXOFWFXANCNFSM5254CTNA . You are receiving this because you were mentioned.Message ID: @.>

Thank you very much!

chrisdaiii commented 1 year ago

@TheAtomicOption Sorry to bother you again, when I generate the .cshtml file to other places, not a temporary directory, VS can also find this file and hit a breakpoint during debugging. What puzzles me is that I did not specify the full path. How is it Do you know where to find the .cshtml file? Thanks!

shehrozeee commented 1 year ago

This would happen if you store it in the same directory as the build directory, which is why the vs picks it up without the absolute path, as the build directory is already in the folders that vs is looking in for the pdb file. You would need to do a clean up of the file yourself though.

On Thu, Jul 6, 2023, 7:34 AM chrisdai @.***> wrote:

@TheAtomicOption https://github.com/TheAtomicOption Sorry to bother you again, when I generate the .cshtml file to other places, not a temporary directory, VS can also find this file and hit a breakpoint during debugging. What puzzles me is that I did not specify the full path. How is it Do you know where to find the .cshtml file? Thanks!

— Reply to this email directly, view it on GitHub https://github.com/adoconnection/RazorEngineCore/pull/111#issuecomment-1622915686, or unsubscribe https://github.com/notifications/unsubscribe-auth/AICB5JH63PEO7KDLRGT7S7LXOYW5BANCNFSM5254CTNA . You are receiving this because you were mentioned.Message ID: @.***>

chrisdaiii commented 1 year ago

This would happen if you store it in the same directory as the build directory, which is why the vs picks it up without the absolute path, as the build directory is already in the folders that vs is looking in for the pdb file. You would need to do a clean up of the file yourself though. On Thu, Jul 6, 2023, 7:34 AM chrisdai @.> wrote: @TheAtomicOption https://github.com/TheAtomicOption Sorry to bother you again, when I generate the .cshtml file to other places, not a temporary directory, VS can also find this file and hit a breakpoint during debugging. What puzzles me is that I did not specify the full path. How is it Do you know where to find the .cshtml file? Thanks! — Reply to this email directly, view it on GitHub <#111 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AICB5JH63PEO7KDLRGT7S7LXOYW5BANCNFSM5254CTNA . You are receiving this because you were mentioned.Message ID: @.>

I still don't quite understand what you mean by the build directory. I tried pointing RazorProjectFileSystem.Create("D:\") to other places, and VS can still find the generated .cshtml file in the temp directory, or put . cshtml generation works elsewhere as well, which makes me feel like a full disk scan.

TheAtomicOption commented 1 year ago

@13750573877 These questions are a bit outside the scope of this PR and the best way to understand what's happening would be for you to read up on how .pdb files work and the C# build/compilation process.

To try to give a succinct answer: No, it's never a full disk scan.

The whole 'magic' of Razor is that you can continue to refer to cshtml full/partial filenames in code while what the rest of the program actually interacts with is the assemblies compiled from them. C# compilation in debug mode yields both an assembly and a pdb which acts as a lookup between the instructions in the assembly and the original source file and that file's specific lines. This PR just makes RazorEngineCore start using those pdb both when loading an assembly and when saving to disk, instead of just discarding the lookup data.

If you hit a breakpoint in a file in VS, hovering the mouse over the edit window's tab should display the full path and filename of the file you're viewing as a tooltip. That path and filename would have been determined when the assembly was compiled by RazorEngineCore.

Whether your project includes a cshtml file or resulting assembly is determined by what files and directories your project and solution include. The C# project build process and how the directories it uses are specified is definitely far outside the scope of this PR, but its settings are the basis for what your program has available at run time. Hopefully that and the key term "build directory" gives you some direction to research further if you need to.

TheAtomicOption commented 1 year ago

Rebased this again and fixed a typo. Didn't update version numbers or update readme changes to reflect the time since this was originally proposed. just resolved the code conflicts.

Feel free to update as needed for smooth merging.

chrisdaiii commented 1 year ago

@13750573877 These questions are a bit outside the scope of this PR and the best way to understand what's happening would be for you to read up on how .pdb files work and the C# build/compilation process.

To try to give a succinct answer: No, it's never a full disk scan.

The whole 'magic' of Razor is that you can continue to refer to cshtml full/partial filenames in code while what the rest of the program actually interacts with is the assemblies compiled from them. C# compilation in debug mode yields both an assembly and a pdb which acts as a lookup between the instructions in the assembly and the original source file and that file's specific lines. This PR just makes RazorEngineCore start using those pdb both when loading an assembly and when saving to disk, instead of just discarding the lookup data.

If you hit a breakpoint in a file in VS, hovering the mouse over the edit window's tab should display the full path and filename of the file you're viewing as a tooltip. That path and filename would have been determined when the assembly was compiled by RazorEngineCore.

Whether your project includes a cshtml file or resulting assembly is determined by what files and directories your project and solution include. The C# project build process and how the directories it uses are specified is definitely far outside the scope of this PR, but its settings are the basis for what your program has available at run time. Hopefully that and the key term "build directory" gives you some direction to research further if you need to.

Thank you very much for your patient answer. Yes, when vs hits the breakpoint, the full path I see on the tab of the editing window is the file path I generated in the temporary directory. What I don’t understand is how vs/Razor Knowing the full path of my file, I just specified the file name, I should go and see the magic in Razor and pdb, anyway, thank you very much.

MattMinke commented 9 months ago

Is there a reason this has not been merged yet ?

adoconnection commented 9 months ago

Hi, basically lack of time to review. I did a test quite a time ago and something went wrong so I postponed merge 😅Will have some free time on a next week so seems like a good moment to finalize this work 21 окт. 2023 г., в 03:48, Matt Minke @.***> написал(а): Is there a reason this has not been merged yet ?

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you commented.Message ID: @.***>

adoconnection commented 8 months ago

@TheAtomicOption Hi, may I ask you to make any sort of instruction on how to debug template step by step.

shehrozeee commented 8 months ago

@adoconnection to use it you should set the options.GeneratePdbStream to true. this will generate a pdb file along the cshtml file in the temp directory in temp folder.

this is from the original commit that I added to the readme that explains how to use it

Debugging templates

Add the following line in your template source and your debugger(vs code/vs studio) break on it.

System.Diagnostics.Debugger.Break();

Use this overload for the Compile function and pass true for the addPdb argument

razorEngine.Compile(templateSource, builder =>
{
    builder.AddAssemblyReferenceByName("System.Security"); // by name
    builder.AddAssemblyReference(typeof(System.IO.File)); // by type
    builder.AddAssemblyReference(Assembly.Load("source")); // by reference
},
true); //This 'true' will enable debugging.

Your debbuger will ask you to provide the path to the source file, by defult it is set to be generated in %temp% (point your there when asked for the file).

image

adoconnection commented 8 months ago

@shehrozeee @TheAtomicOption Thanks fellas! 🚀 released in 2023.11.1 https://www.nuget.org/packages/RazorEngineCore/

I made few changes to your code to enable some extras:

First of all option to include debuggig info, at this point nothing is stored anywhere, we only produce inmemory pdb and keep final template source code. This template can be saved and loaded (why not) without loosing this debugging data.

IRazorEngineCompiledTemplate template2 = razorEngine.Compile(templateText, builder =>
{
    builder.IncludeDebuggingInfo();
});

To activate debugging:

template2.EnableDebugging(); // optional path to output directory

string result = template2.Run(new
{
    Title = "Welcome"
});

Shortcut for breakpoint @{ Breakpoint(); } if EnableDebugging() was called it will stop, if not - no.

<div>
    <h1>Hello</h1>

    @{ Breakpoint(); }

    @if (Model != null)
    {
        <h2>Hello @Model</h2>
    }

   ...