dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Blazor code with @ref attributes casues compiler error "rzc generate exited with code 1" and crashes Visual Studio when edited. #13318

Closed mikewodarczyk closed 5 years ago

mikewodarczyk commented 5 years ago

Blazor code containing components using the @ref attribute cause an error in the method Microsoft.AspNetCore.Razor.Language.Components.ComponentReferenceCaptureLoweringPass.ExecuteCore which causes a compile time error to be produced and will crash Visual studio when the affected file is viewed in the editor.

This appears to be related to the issue https://github.com/aspnet/AspNetCore/issues/13115 however they were unable to create an example that fails and they closed the issue.

I have made a github repo with a small project that reproduces the issue. The repo is at: https://github.com/mikewodarczyk/BlazorVisualStudioBug

This started in Microsoft.AspNetCode.App version 3.0.0-preview8.19405.7 Microsoft.NETCode.App verion 3.0.0-preview8-28405-07

The code contains 1 added page "Create.razor" with backing code in "CreateBase.cs". This page includes a component "EditFields.razor" with backing code in "EditFieldsBase.cs". The Edit Fields razor page appears to be the source of the issue.

<!-- From EditFields.razor -->
 <EditForm @ref="EditFormElement" @ref:suppressfield  Model="@Distributor">
    <DataAnnotationsValidator />
    @*<ValidationSummary />*@
    <table class="table">
        <tbody>
     ....

The base class contains the variable EditFormElement:

// from EditFieldsBase.cs 
 protected ElementReference EditFormElement { get; set; }

When you compile the code, you get the error:

 Error      rzc generate exited with code 1.    BlazorApp1  C:\Program Files\dotnet\sdk\3.0.100-preview8-013656\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.CodeGeneration.targets    150

and Output

 1>------ Rebuild All started: Project: BlazorApp1, Configuration: Debug Any CPU ------
1>You are using a preview version of .NET Core. See: https://aka.ms/dotnet-core-preview
1>Pages\CreateBase.cs(26,30,26,45): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
1>Specified argument was out of the range of valid values. (Parameter 'index')
1>   at Microsoft.AspNetCore.Razor.Language.Intermediate.IntermediateNodeCollection.get_Item(Int32 index)
1>   at Microsoft.AspNetCore.Razor.Language.Components.ComponentReferenceCaptureLoweringPass.ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
1>   at Microsoft.AspNetCore.Razor.Language.IntermediateNodePassBase.Execute(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
1>   at Microsoft.AspNetCore.Razor.Language.DefaultRazorOptimizationPhase.ExecuteCore(RazorCodeDocument codeDocument)
1>   at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
1>   at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
1>   at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
1>   at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
1>   at Microsoft.AspNetCore.Razor.Tools.GenerateCommand.<>c__DisplayClass46_0.<GenerateCode>b__0(Int32 i)
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
1>--- End of stack trace from previous location where exception was thrown ---
1>   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source)
1>   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker& currentWorker, Int32 timeout, Boolean& replicationDelegateYieldedBeforeCompletion)
1>   at System.Threading.Tasks.TaskReplicator.Replica`1.ExecuteAction(Boolean& yieldedBeforeCompletion)
1>   at System.Threading.Tasks.TaskReplicator.Replica.Execute()
1>C:\Program Files\dotnet\sdk\3.0.100-preview8-013656\Sdks\Microsoft.NET.Sdk.Razor\build\netstandard2.0\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): error : rzc generate exited with code 1.
1>Done building project "BlazorApp1.csproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Worse yet, if you try and edit the file BlazorApp1/Pages/EditFields.razor in Visual Studio and scroll down to line 34 which contains a @ref attribute. VS will hang and crash.

Expected behavior

There should either be a compile time error pointing to a line in my code with the error or there should be no compile time error at all.

Editing the file BlazorApp1/Pages/EditFields.razor in Visual Studio should not crash VS.

Additional context

dotnet --info

.NET Core SDK (reflecting any global.json): Version: 3.0.100-preview8-013656 Commit: 8bf06ffc8d

Runtime Environment: OS Name: Windows OS Version: 10.0.18362 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.0.100-preview8-013656\

Host (useful for support): Version: 3.0.0-preview8-28405-07 Commit: d01b2fb7bc

.NET Core SDKs installed: 1.0.0-preview1-002702 [C:\Program Files\dotnet\sdk] 1.0.0 [C:\Program Files\dotnet\sdk] 1.0.4 [C:\Program Files\dotnet\sdk] 1.1.0 [C:\Program Files\dotnet\sdk] 2.0.3 [C:\Program Files\dotnet\sdk] 2.1.104 [C:\Program Files\dotnet\sdk] 2.1.800 [C:\Program Files\dotnet\sdk] 2.2.101 [C:\Program Files\dotnet\sdk] 2.2.300-preview-010067 [C:\Program Files\dotnet\sdk] 3.0.100-preview6-012264 [C:\Program Files\dotnet\sdk] 3.0.100-preview8-013656 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview6.19307.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0-preview8.19405.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 1.0.0-rc2-3002702 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.1.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 1.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0-preview8-28405-07 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0-preview6-27804-01 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.0.0-preview8-28405-07 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

githubfanster commented 5 years ago

have u tried changing @ref:suppressfield to @ref:suppressField ?

pranavkm commented 5 years ago

Looks like @githubfanster might have the right answer. Could you try making that change to see if it resolves the crash?

@ajaybhargavb do you know why this might crash VS? That sounds problematic.

githubfanster commented 5 years ago

i was able to build your repo by making two changes:

in EditFields.razor, line 34: <EditForm @ref="EditFormElement" @ref:suppressField Model="@Distributor">

then in EditFieldsBase.cs line 26: from: protected ElementReference EditFormElement { get; set; } to protected EditForm EditFormElement { get; set; }

githubfanster commented 5 years ago

i thought your experience might have been related to this: https://github.com/aspnet/Announcements/issues/381

mikewodarczyk commented 5 years ago

@githubfanster

Thanks! Making the changes @ref:suppressfield => @ref:suppressField and "protected ElementReference EditFormElement { get; set; }" to "protected EditForm EditFormElement { get; set; }" got the code to compile and stopped the visual studio crash. That gets me out of my issue and I can work with the latest patch release.

It still seems like the error message reported: "rzc generate exited with code 1." and the crash of visual studio when editing the razor file should be fixed. I think that there is work in the next pre-release 9 addressing this.

I will close this for now since it is a pre-release and I will re-test my broken code with the next pre-release and see if I need to reopen. Thanks!!

NTaylorMullen commented 5 years ago

Verified this no longer crashes in preview9