adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
585 stars 88 forks source link

All errors produce same warning message #47

Closed kobruleht closed 3 years ago

kobruleht commented 3 years ago

To reporoduce, add Microsoft.AspNetCore.Mvc.ViewFeatures and RazorEngineCore.Extensions assemblies and compile using RazorEngineCorePageModel from RazorEngin Extensyions project:

var compiledTemplateLocal = razorEngine.Compile<RazorEngineCorePageModel<TModel>>(template, builder =>
                    {
builder.AddAssemblyReference(typeof(RazorEngineCorePageModel));
builder.AddAssemblyReference(typeof(ITempDataDictionary));
});

Observed:

compilation produces warning:

Warning CS1701: Assuming assembly reference 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' used by 'Eeva' matches identity 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' of 'Microsoft.AspNetCore.Mvc.ViewFeatures', you may need to supply runtime policy

Version 5.0.0.0 of Microsoft.AspNetCore.Mvc.ViewFeatures assembly does not exist. Latest is 2.2.0.0

RazorEngine assigns first message from compiler message list to exception Message property. So every compilation exception returns this same warning message.

adoconnection commented 3 years ago

As I can see Microsoft.AspNetCore.Mvc.ViewFeatures does not exists as a package any more, it is bundled with AspNetCore v5 https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.viewfeatures.htmlhelper?view=aspnetcore-5.0

I dont understand how I'm expected to proceed :)

kobruleht commented 3 years ago

Run code

razorEngine.Compile<RazorEngineCorePageModel<TModel>>("@if causecompileerror", builder =>
                    {
                        builder.AddAssemblyReference(typeof(RazorEngineCorePageModel));
                        builder.AddAssemblyReference(typeof(ITempDataDictionary));
                    });

Error message appears about assembly version mismatch, not about "@if causecompileerror" syntax error.

adoconnection commented 3 years ago

can you please post full code example or maybe you can attach console app project as a zip

kobruleht commented 3 years ago

Suspected line is

https://github.com/adoconnection/RazorEngineCore/blob/master/RazorEngineCore/RazorEngine.cs#L113

which returns first message which may be warning. Failed to create standalone testcase since error messages appeared first in list.

Tried to create assembly version mismatch warnig by using

builder.AddAssemblyReference( Assembly.LoadFile("c:/program files/dotnet/packs/Microsoft.AspNetCore.App.Ref/5.0.0/ref/net5.0/Microsoft.AspNetCore.Mvc.ViewFeatures.dll"));

but got error

System.BadImageFormatException: 'Could not load file or assembly 'Microsoft.AspNetCore.Mvc.ViewFeatures, Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (0x80131058)'

No idea how to cause compiler warning about version mismatch.list.

wdcossey commented 3 years ago

@adoconnection @kobruleht

I will put in a PR to make the exceptions a little more descriptive.

Before:

Unhandled exception. RazorEngineCore.RazorEngineCompilationException: Unable to compile template: v2bggr5g.zad(31,5): error CS1003: Syntax error, '(
' expected
   at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options) in C:\<omitted>\RazorEngine.cs:line 120
   at RazorEngineCore.RazorEngine.Compile(String content, Action`1 builderAction) in C:\<omitted>\RazorEngine.cs:line 43
   at ExampleAppNET5.Program.Main(String[] args) in C:\<omitted>\ExampleAppNET5\Program.cs:line 44

After:

Unhandled exception. RazorEngineCore.RazorEngineCompilationException: Unable to compile template:
k1v0b14o.1hw(31,5): error CS1003: Syntax error, '(' expected
k1v0b14o.1hw(31,22): error CS1026: ) expected
k1v0b14o.1hw(31,22): error CS1525: Invalid expression term '}'
k1v0b14o.1hw(31,22): error CS1002: ; expected
k1v0b14o.1hw(31,5): error CS0103: The name 'causecompileerror' does not exist in the current context
   at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options) in C:\<omitted>\RazorEngine.cs:line 120
   at RazorEngineCore.RazorEngine.Compile(String content, Action`1 builderAction) in C:\<omitted>\RazorEngine.cs:line 43
   at ExampleAppNET5.Program.Main(String[] args) in C:\<omitted>\ExampleAppNET5\Program.cs:line 44
kobruleht commented 3 years ago

Have you noticed that RazonEngineCore code returns first message in case of error:

https://github.com/adoconnection/RazorEngineCore/blob/master/RazorEngineCore/RazorEngine.cs#L113

. If first message is warning, it returns warning message as error message, hiding real error message. Why not to add filter like in dynamic scaffold project:

https://github.com/jdtcn/RuntimeEfCore/blob/master/RuntimeEfCore/Program.cs#L57

to get first error, excluding warning.

wdcossey commented 3 years ago

Updated the PR to only show errors (DiagnosticSeverity.Error)

Unhandled exception. RazorEngineCore.RazorEngineCompilationException: Unable to compile template: 1l5l1blv.5lm(19,7): error CS0103: The name 'causecompileerror' does not exist in the current context
   at RazorEngineCore.RazorEngine.CreateAndCompileToStream(String templateSource, RazorEngineCompilationOptions options) in <omitted>\RazorEngine.cs:line 117
   at RazorEngineCore.RazorEngine.Compile(String content, Action`1 builderAction) in <omitted>\RazorEngine.cs:line 43
   at ExampleAppNET5.Program.Main(String[] args) in <omitted>\ExampleAppNET5\Program.cs:line 43
adoconnection commented 3 years ago

fixed in 2021.7.1