Include debug and #line directive for compilation errors?

Apr 29, 2011 at 9:19 AM

Is there a way with Web.Razor to retrieve meaningful error message (error from line, column) while parsing a template?

Currently, it is painful to debug a template error, as we only get an error from the compilation of a c# file. Does Razor provide a way to include #line pragmas in the generated file?

Apr 29, 2011 at 2:27 PM

Well, the most meaningful errors are contained in the Errors property of the exception. I've found those to be pretty helpful in targeting the errors that I have in my templates. With regards to more information the problem is the templates are converted to a c# file which is then compiled and that makes it much more difficult to provide any information about the cshtml template itself. I don't think even the MVC one does. It's something I'll look into though.

Apr 29, 2011 at 3:58 PM

When you have hundreds of lines in your template and the error says "missing ;", moreover when the error line is meaningless, It's not so easy to found it! ;)

Any templating system is usually able to include pragma lines (this is the case for the T4 engine for example) to improve diagnostics for template errors. Looking at the code in System.Web.Razor, it seems that the class System.Web.Razor.Generator.RazorCodeGenerator contains a property GenerateLinePragmas, so they are probably handling it. Going to check if I set this flag in RazorEngine, does the line is coming up...

Apr 29, 2011 at 4:17 PM

Just made a quick test and its working fine in RazorEngine! This is much easier now!

The only thing that was slightly annoying in the current code is that the name for "named" templates is not propagate. So I had to change quite some code, but It didn't hurt anything in the workflow. Just add a TemplateName property to TypeContext and propagate it.

I'm going to upload a patch with those changes.

Apr 29, 2011 at 4:24 PM

The patch for line pragmas is uploaded. Tell me if it's working fine on your side or if you encounter any issues with this patch.

Apr 29, 2011 at 5:17 PM

I have just uploaded a new patch which includes support for debugging template file inside visual studio by settings breakpoint directly inside a template, which is pretty cool, when you have to debug things!

In order to achieve that, I have extended all the signature that compiles/parse a template to pass a new optional argument "location" where you can specify the location of the template file (absolute location is better).

Let me know what do you think about this extension? That would be fantastic to integrate it in the 2.2 release, as I wouldn't have to maintain a local version of this great project!

Apr 29, 2011 at 6:57 PM

That's awesome. I'm definitely going to look at that this weekend. That's a wonderful contribution.

Apr 29, 2011 at 7:50 PM

That's indeed pretty cool! After testing it, It seems that breakpoints can only be set on a line inside @{ ... } or @functions { ... }, which can be already quite helpful.