Aspnetcore: Compilation error in in .NET 5 Preview 8 with attributes without value, in some cases

Created on 26 Aug 2020  路  18Comments  路  Source: dotnet/aspnetcore

Compilation error with attributes without value, in some cases

To Reproduce

In any Razor component, add:
<video src="@videoSrc" autoplay></video>
@code{
string videoSrc = string.Empty;
}

In .NET 5 Preview 8,
error CS1503: Argument 2: cannot convert from 'string' to 'in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame'

<video src="movie.mp4" autoplay></video>
Compiles without error

dotnet --info

.NET SDK (reflecting any global.json):
Version: 5.0.100-preview.8.20417.9
Commit: fc62663a35

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100-preview.8.20417.9\

Host (useful for support):
Version: 5.0.0-preview.8.20407.11
Commit: bf456654f9

Done area-blazor bug

Most helpful comment

Another fix I've just tested, which avoids any changes to the HTML is to add a global.json file to force use of the .NET 3.1 SDK for the build:

  1. Add a text file global.json to the root of the solution folder (assuming you don't already have one)
  2. Edit the file to set the SDK version you have installed and wish to compile against. Mine is currently 3.1.401
{
  "sdk": {
    "version": "3.1.401",
    "rollForward": "latestMinor"
  }
}

If you don't know what SDK version you have installed, use

dotnet --list-sdks

at the command line

All 18 comments

Yep, I'm getting exactly the same error with a Blazor Server Side app.
Most Visual Studio releases seem to break something these days, seriously, I'm not impressed! More wasted time.
Using Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 2.0

The following two lines are common amongst the errors I'm getting in the xxx.razor.g.cs files:
__builder.AddAttribute(15, "selected");
__builder.AddAttribute(12, "disabled");

In the code fragment below, its generating the error on the line:
<option value="@pageSize" selected>

<select id="pageSizeSelector" class="form-control-sm" FullWidth="false" @onchange="OnPageSizeChanged">
    @foreach (var pageSize in AvailablePageSizes)
    {
        var isSelected = pageSize == PageSize;
        @if (isSelected)
        {
            <option value="@pageSize" selected>
                @pageSize
            </option>
        }
        else
        {
            <option value="@pageSize">
                @pageSize
            </option>
        }
    }
</select>

Thanks for reporting this. I'm trying to track this down but so far haven't managed to reproduce the issue. Here's what I've tried:

  1. In an empty directory, ran:

    • dotnet --version and verified the output is 5.0.100-preview.8.20417.9

    • dotnet new blazorserver

    • dotnet run (and verified it works in a browser)

  2. Using VS Code, edited Pages/Index.razor to contain the following:
@page "/"

<video src="@videoSrc" autoplay></video>

@code{
    string videoSrc = string.Empty;
}
  1. Back on the command line, ran dotnet build, which completed without errors. Also dotnet run works.
  2. Now also tried editing Pages/Index.razor to contain:
@page "/"

<select id="pageSizeSelector" class="form-control-sm" FullWidth="false" @onchange="OnPageSizeChanged">
    @foreach (var pageSize in AvailablePageSizes)
    {
        var isSelected = pageSize == PageSize;
        @if (isSelected)
        {
            <option value="@pageSize" selected>
                @pageSize
            </option>
        }
        else
        {
            <option value="@pageSize">
                @pageSize
            </option>
        }
    }
</select>

@code {
    void OnPageSizeChanged() {}

    int[] AvailablePageSizes = new[] { 1, 2, 3 };

    int PageSize = 1;
}
  1. Back on the command line, ran dotnet build, which completed without errors. Also dotnet run works.

So I'm not sure how to reproduce the problem. I also tried building in VS 2019 16.8.0 Preview 1.0 which worked fine too. I'll try Preview 2 later.

@sjohag @as-srandall Do either of you have further suggestions on how to reproduce the issue? If you follow the steps I listed above, does the error occur for you, or does it only happen in some specific project?

If it only happens in an existing project, can you try deleting your project's obj/bin directories and building again?

Using Visual Studio Version 16.8.0 Preview 2.0, File - New - Project - Blazor App - Blazor Server App
In .csproj target framework is netcoreapp3.1 and the problem can be reproduced.
Changing to net5.0 solves the problem.

I had
<td "table-primary" that I changed to <td class="table-primary" and one of the errors went away.
The other errors seemd non-legit.

Threw away obj and bin and built successfully.

Woops, spoke too early. It built but it didn't run! Same errors again. (What's that all about?)

Upon further analysis :)

The other five errors were actually legit, but the error message is misleading to say the least.

I had several td class=""
When I removed the empty class attribute, the errors went away.

@23min it's a little difficult to keep track of what you're saying. Could you clarify by posting a complete example of something that fails to compile with the error Argument 2: cannot convert from 'string' to 'in Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame', if you saying you have such an example? Thanks!

Also, if you're getting a different error, then please post it as a different issue instead of here.

I had the same issue.

Each file with an error contained a html tag which had class="" in it. Once I removed all of those, my program is running again.

@SteveSandersonMS to sum it up:

In each case, I had a table definition <td> with an invalid class or an attribute that was not recognized.

For example (invalid class)
image

and (empty class)
image

Does that clarify?

Just ran into this as updating to Version 16.8.0 preview 2
As @jeffrey-caldwell and @23min pointed out any blank attribute will trigger this. This also includes stuff like
<option value="">

For @23min's example if you need to keep the custom attribute you could just assign a string to it to keep it.
<td table-primary="table-primary">

Cheers to @jeffrey-caldwell and @23min for pointing this out. 馃憤

This also effects stuff like:
<textarea readonly>
Which as far as I know is an empty attribute.

@Zelif I just encountered this issue with VS 2019 16.8.0 preview 2.

For the