Skip to content

Razor compilation exception when UseRazorSourceGenerator=false in 10.0.100 #12492

@ArturDorochowicz

Description

@ArturDorochowicz

Describe the bug

Compilation of a project with a ViewComponent with UseRazorSourceGenerator=false fails with the following exception:

    Expected 'OriginalTypeNameObject' to be non-null.
     File='/_/src/razor/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Mvc/ViewComponentMetadata.cs', Line=37
       at Microsoft.AspNetCore.Razor.ThrowHelper.ThrowInvalidOperationException(String message)
       at Microsoft.AspNetCore.Razor.Assumed.NotNull[T](Nullable`1 value, String message, String valueExpression, String path, Int32 line)
       at Microsoft.AspNetCore.Razor.NullableExtensions.AssumeNotNull[T](Nullable`1 value, String message, String valueExpression, String path, Int32 line)
       at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentMetadata.Builder.Build()
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.ReadViewComponentMetadata(JsonDataReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.Json.JsonDataReader.ReadNonNullObject[T](String propertyName, ReadProperties`1 readProperties)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.<ReadTagHelperFromProperties>g__ReadMetadata|3_7(JsonDataReader reader, String propertyName)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.ReadTagHelperFromProperties(JsonDataReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectJsonConverter`1.ReadJson(JsonReader reader, Type objectType, T existingValue, Boolean hasExistingValue, JsonSerializer serializer)
       at Newtonsoft.Json.JsonConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.GetTagHelpers(String tagHelperManifest)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.ExecuteCore(RazorConfiguration configuration, String projectDirectory, String tagHelperManifest, SourceItem[] sourceItems)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.ExecuteCoreAsync()
       at Microsoft.NET.Sdk.Razor.Tool.CommandBase.ExecuteAsync()
    C:\Program Files\dotnet\sdk\10.0.100\Sdks\Microsoft.NET.Sdk.Razor\targets\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): error rzc generate exited with code 1.

Among other cases, this prevents compilation of .net 5 and earlier projects containing view components where UseRazorSourceGenerator=false is the implicit and only option.

To Reproduce

Sample project: WebApplication14.zip

Create a default Asp.Net Core project with Razor Pages (dotnet new webapp).

Add an empty ViewComponent, e.g.:

using Microsoft.AspNetCore.Mvc;

public class MyViewComponent : ViewComponent
{
}

And build with dotnet build -p:UseRazorSourceGenerator=false -v:d

Exceptions (if any)

Build output:

>dotnet build -p:UseRazorSourceGenerator=false -v:d
Restore complete (0,2s)
    Determining projects to restore...
    All projects are up-to-date for restore.
  WebApplication14 net10.0 failed with 1 error(s) (0,4s)
    Expected 'OriginalTypeNameObject' to be non-null.
     File='/_/src/razor/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Mvc/ViewComponentMetadata.cs', Line=37
       at Microsoft.AspNetCore.Razor.ThrowHelper.ThrowInvalidOperationException(String message)
       at Microsoft.AspNetCore.Razor.Assumed.NotNull[T](Nullable`1 value, String message, String valueExpression, String path, Int32 line)
       at Microsoft.AspNetCore.Razor.NullableExtensions.AssumeNotNull[T](Nullable`1 value, String message, String valueExpression, String path, Int32 line)
       at Microsoft.AspNetCore.Mvc.Razor.Extensions.ViewComponentMetadata.Builder.Build()
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.ReadViewComponentMetadata(JsonDataReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.Json.JsonDataReader.ReadNonNullObject[T](String propertyName, ReadProperties`1 readProperties)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.<ReadTagHelperFromProperties>g__ReadMetadata|3_7(JsonDataReader reader, String propertyName)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectReaders.ReadTagHelperFromProperties(JsonDataReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.Json.ObjectJsonConverter`1.ReadJson(JsonReader reader, Type objectType, T existingValue, Boolean hasExistingValue, JsonSerializer serializer)
       at Newtonsoft.Json.JsonConverter`1.ReadJson(JsonReader reader, Type objectType, Object existingValue, JsonSerializer serializer)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.DeserializeConvertable(JsonConverter converter, JsonReader reader, Type objectType, Object existingValue)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
       at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
       at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
       at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.GetTagHelpers(String tagHelperManifest)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.ExecuteCore(RazorConfiguration configuration, String projectDirectory, String tagHelperManifest, SourceItem[] sourceItems)
       at Microsoft.NET.Sdk.Razor.Tool.GenerateCommand.ExecuteCoreAsync()
       at Microsoft.NET.Sdk.Razor.Tool.CommandBase.ExecuteAsync()
    C:\Program Files\dotnet\sdk\10.0.100\Sdks\Microsoft.NET.Sdk.Razor\targets\Microsoft.NET.Sdk.Razor.CodeGeneration.targets(150,5): error rzc generate exited with code 1.

Build failed with 1 error(s) in 1,1s

Further technical details

Using .net sdk 10.0.100 RTM, Windows as well as Linux.

This seems to also be reported here: https://developercommunity.visualstudio.com/t/Error-compiling-razor-library-with-VS-20/10993637?sort=newest&topics=visual

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions