dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
491 stars 190 forks source link

Resolve issues with global usings in Razor compiler #7539

Open captainsafia opened 3 years ago

captainsafia commented 3 years ago

At least based on my initial investigations via https://github.com/dotnet/aspnetcore/issues/34217#issuecomment-882001435, it doesn't look like global usings work by default as a feature when building users.

Repro

  1. Create a new Blazor WASM template.
  2. Copy the imports in _Imports.razor into a global usings file.
  3. Comment out the contents of _Imports.razor.
  4. Build and observe errors.

See https://github.com/captainsafia/GlobalUsings for repro and bin log.

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. Because it's not immediately obvious that this is a bug in our framework, we would like to keep this around to collect more feedback, which can later help us determine the impact of it. We will re-evaluate this issue, during our next planning meeting(s). If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

captainsafia commented 3 years ago

We had a hunch that disabling the Razor source generator might resolve this but doesn't look like that is the case.

❯ dotnet build /bl /p:UseRazorSourceGenerator=false
Microsoft (R) Build Engine version 17.0.0-preview-21357-02+cdc5faeda for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

/Users/captainsafia/.dotnet/sdk/6.0.100-preview.7.21359.29/MSBuild.dll -consoleloggerparameters:Summary -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,/Users/captainsafia/.dotnet/sdk/6.0.100-preview.7.21359.29/dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,/Users/captainsafia/.dotnet/sdk/6.0.100-preview.7.21359.29/dotnet.dll -maxcpucount -restore -verbosity:m /bl ./GlobalUsings.csproj
  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
/Users/captainsafia/Verifications/GlobalUsings/App.razor(1,1): warning RZ10012: Found markup element with unexpected name 'Router'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/App.razor(2,5): warning RZ10012: Found markup element with unexpected name 'Found'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/App.razor(4,9): warning RZ10012: Found markup element with unexpected name 'FocusOnNavigate'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/App.razor(6,5): warning RZ10012: Found markup element with unexpected name 'NotFound'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/Pages/Index.razor(7,1): warning RZ10012: Found markup element with unexpected name 'SurveyPrompt'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/Shared/NavMenu.razor(11,13): warning RZ10012: Found markup element with unexpected name 'NavLink'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/Shared/NavMenu.razor(16,13): warning RZ10012: Found markup element with unexpected name 'NavLink'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/Shared/NavMenu.razor(21,13): warning RZ10012: Found markup element with unexpected name 'NavLink'. If this is intended to be a component, add a @using directive for its namespace. [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/App.razor(3,32): error CS0103: The name 'routeData' does not exist in the current context [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]
/Users/captainsafia/Verifications/GlobalUsings/App.razor(4,38): error CS0103: The name 'routeData' does not exist in the current context [/Users/captainsafia/Verifications/GlobalUsings/GlobalUsings.csproj]

Build FAILED.

cc: @jaredpar @pranavkm

ghost commented 2 years ago

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.

jasenf commented 2 years ago

So global using are basically useless if you are using Razor pages. Great.

Siphonophora commented 2 years ago

In case it helps, I ran into this on an app I migrated to dotnet 6. I was able to move most usings to implicit / global, with a few exceptions:

_Imports.razor

@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Routing
@using MyProject.Web.Components
@using Radzen.Blazor

So a few of the MS Components libraries, the namespace where I have custom components in my project, and the Radzen blazor components library.

These mostly look like libraries where components are declared, but if I remove the @using Microsoft.AspNetCore.Components.Web I get the runtime error below:

[12:35:24 WRN] Unhandled exception rendering component: InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': '@onclick' is not a valid attribute name. System.InvalidOperationException: InvalidCharacterError: Failed to execute 'setAttribute' on 'Element': '@onclick' is not a valid attribute name. at Microsoft.AspNetCore.Components.RenderTree.Renderer.InvokeRenderCompletedCallsAfterUpdateDisplayTask(Task updateDisplayTask, Int32[] updatedComponents)

Ghostbird commented 1 year ago

Could this be what causes my issue? Ive got:

Usings.cs:

global using IdentityModel

_Layout.cshtml:

@{
  string name = Context.User?.FindFirst(JwtClaimTypes.Name)?.Value;
}

This throws:

Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.CompilationFailedException: One or more compilation failures occurred:
./Views/Shared/_Layout.cshtml(6,36): error CS0103: The name 'JwtClaimTypes' does not exist in the current context

However it does not seem to throw in the published app. I guess that publishing more conventionally compiles the razor pages. I guess the missing inclusion of global usings is limited to the JIT compiler.

maurobringolf commented 1 year ago

Any updates on this? I have global usings in the .csproj file but the imported things are not available in razor pages in the project.

Mike-Logit commented 8 months ago

I think this issue might be related to a bug I encountered today but with a .NET 8 MVC app. I had to copy all the GlobalUsings.cs using statements and add them to _ViewImports.cshtml file while using the AddRazorRuntimeCompilation() middleware. Otherwise, when I edited the HTML (even raw text content) the app throws the same CompilationFailedException exception mentioned above. It was ignoring the global using statements at runtime only. The initial build was fine.

EDIT: Yep. There was a merged PR to update the razor file compilation documentation: https://learn.microsoft.com/en-us/aspnet/core/mvc/views/view-compilation

Runtime compilation:
- Isn't supported for Razor components of Blazor apps.
- Doesn't support global using directives.

So it affects ALL razor files (.razor and .cshtml) and not just Blazor apps. So, my MVC app bug is the same one as the Blazor app bugs being reported.