Skip to content

Update Razor compiler to use global:: more liberally #18757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MichelArendt opened this issue Feb 3, 2020 · 10 comments · Fixed by dotnet/razor-compiler#34
Closed

Update Razor compiler to use global:: more liberally #18757

MichelArendt opened this issue Feb 3, 2020 · 10 comments · Fixed by dotnet/razor-compiler#34
Assignees
Labels
affected-few This issue impacts only small number of customers bug This issue describes a behavior which is not expected - a bug. Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool
Milestone

Comments

@MichelArendt
Copy link

Issue: In a Razor Component any variables named "Microsoft" reproduce the error (variable type on the error message changes accordingly):

Error CS1061 'string' does not contain a definition for 'AspNetCore' and no accessible extension method 'AspNetCore' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

Question: Is this an issue or by design?

Project details and files: This is a Razor Class Library project.

.csproj

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <RazorLangVersion>3.0</RazorLangVersion>
    <LangVersion>8</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.1" />
  </ItemGroup>

</Project>

LoginFormUserComponent.razor

@using SBL.Models.User.Login.OpenId

<div class="login">
    <div class="page-title">
        Login
    </div>

    <p>
        To login you must use one of the available Social Login options below:
    </p>

    <div class="social-login__container">
        <SocialButtonLoginComponent Provider="@Facebook" Image="/images/social/brands/facebook_37x37.png" />
        <SocialButtonLoginComponent Provider="@Google" Image="/images/social/brands/google_37x37.png" />
        @*<SocialButtonLoginComponent Provider="@Microsoft" Image="/images/social/brands/microsoft_37x37.png" />*@
        <SocialButtonLoginComponent Provider="@Twitter" Image="/images/social/brands/twitter_37x37.png" />
    </div>
</div>

@code {
    [Parameter]
    public ProviderOpenIdModel? Facebook { get; set; } = null;

    [Parameter]
    public ProviderOpenIdModel? Google { get; set; } = null;

    //[Parameter]
    //public ProviderOpenIdModel? Microsoft { get; set; } = null; // ERROR

    [Parameter]
    public ProviderOpenIdModel? Twitter { get; set; } = null;

    public string Microsoft { get; set; } // ERROR
}

Comments:
I started getting this random error and found out that apparently I cannot write "Microsoft" as a variable name in a Razor component.

I was unable to find the search keywords for anything similar to this.

I initially thought that there was a reference issue on my project when I realized the commented parameter was the issue. I then tried using a native variable type to see if the issue persisted - and it did.

It's not really an issue I need fixing per say - just wondering why this happens.

Thanks in advance!

@Pilchie Pilchie added the area-blazor Includes: Blazor, Razor Components label Feb 3, 2020
@mkArtakMSFT mkArtakMSFT added this to the Next sprint planning milestone Feb 3, 2020
@pranavkm pranavkm added bug This issue describes a behavior which is not expected - a bug. and removed investigate labels Feb 4, 2020
@pranavkm
Copy link
Contributor

pranavkm commented Feb 4, 2020

Thanks for the issue report @MichelArendt. This looks like a bug in Razor's code generation. Unfortunately there isn't a very good workaround for this outside of using a different member name.

A fix for this would be to globally qualify the Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck call.

protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder) 
        {
            __builder.AddMarkupContent(0, "<h1>Hello, world!</h1>\r\n\r\nWelcome to your new app.\r\n\r\n");
            __builder.OpenComponent<b.Pages.Counter>(1);
            __builder.AddAttribute(2, "Model", Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<b.MyModel>( // here
                Microsoft
            ));
            __builder.CloseComponent();
        }

    public MyModel Microsoft { get; set; } // ERROR

@pranavkm pranavkm removed their assignment Feb 4, 2020
@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Feb 5, 2020

@pranavkm I can make a doc issue of this and add Microsoft to Razor reserved keywords. Let me know if you'd like to go that route.

@pranavkm
Copy link
Contributor

pranavkm commented Feb 5, 2020

@Rick-Anderson this is not a reserved keyword though. Presumably once we fix the underlying issue, this should work.

@pranavkm pranavkm changed the title [Razor Component] "does not contain a definition for 'AspNetCore'" when using variable named "Microsoft" Update Razor compiler to use global:: more willingly Apr 24, 2020
@ghost
Copy link

ghost commented Jul 23, 2020

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

@MajdajkD
Copy link

Same old thing?
#13220

@pranavkm pranavkm changed the title Update Razor compiler to use global:: more willingly Update Razor compiler to use global:: more liberally Aug 26, 2020
@diegofrata
Copy link

diegofrata commented Aug 26, 2020

@pranavkm the problem with this issue is that there is no workaround other than changing the offending type declaration to object or dynamic. It's not possible to simply rename a type I might not own or refactor an entire application changing the namespace whenever there's a conflict.

Whilst it's a rare issue, when it occurs it is a showstopper. Any chance this can be pulled out of the backlog and shipped as part of .NET 5?

@pranavkm
Copy link
Contributor

@diegofrata at this point we're close to wrapping up changes for 5.0. I'll have a look to see how trivial this change is, but I suspect it's fairly involved would make it more likely this would only be fixed in the next major release.

@TanayParikh TanayParikh modified the milestones: Backlog, .NET 7 Planning Oct 19, 2021
@TanayParikh TanayParikh added the Priority:2 Work that is important, but not critical for the release label Oct 22, 2021
@TanayParikh TanayParikh added Priority:1 Work that is critical for the release, but we could probably ship without and removed Priority:2 Work that is important, but not critical for the release labels Oct 23, 2021
@TanayParikh TanayParikh added Priority:0 Work that we can't release without and removed Priority:1 Work that is critical for the release, but we could probably ship without labels Oct 23, 2021
@TanayParikh
Copy link
Contributor

TanayParikh commented Oct 23, 2021

Repro app: https://github.com/jirikanda/Blazor-CascadingTypeParameterIssue from #37506

@mkArtakMSFT mkArtakMSFT added Priority:1 Work that is critical for the release, but we could probably ship without triaged and removed Priority:0 Work that we can't release without labels Nov 2, 2021
@mkArtakMSFT mkArtakMSFT removed the area-blazor Includes: Blazor, Razor Components label Nov 30, 2021
@javiercn javiercn modified the milestones: 7.0-preview1, 7.0-preview2 Jan 24, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affected-few This issue impacts only small number of customers bug This issue describes a behavior which is not expected - a bug. Priority:1 Work that is critical for the release, but we could probably ship without severity-major This label is used by an internal tool
Projects
None yet