Skip to content
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

Verify that Razor compiler honors global usings when generating code for .cshtml & .razor files #34217

Closed
DamianEdwards opened this issue Jul 8, 2021 · 2 comments
Assignees
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views Validation This issue is used to track validation efforts
Milestone

Comments

@DamianEdwards
Copy link
Member

DamianEdwards commented Jul 8, 2021

With C# 10 introducing support for global usings declarations and the SDK now implicitly adding global usings for projects targeting .NET 6 we need to verify that the Razor compiler honors any global usings (whether set via the SDK or in the project) in the code it generates.

We expect this will "just work" so this issue is to ensure we verify that.

Assuming this is verified we can update the project templates to not explicitly add usings in .cshtml/.razor files if they're already added by the SDK.

@captainsafia @pranavkm

DamianEdwards added a commit that referenced this issue Jul 8, 2021
-Depends on dotnet/sdk#18459 flowing to dotnet/aspnetcore repo before we can merge this
- There's a couple of places we could remove usings from .razor files but need to verify the global usings flow into Razor compiler (#34217)
- Reordered usings in some places to ensure they get emitted in alphabetical order (modulo System.* & Microsoft.* coming before any others)
@javiercn javiercn added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views labels Jul 9, 2021
@mkArtakMSFT mkArtakMSFT added the Validation This issue is used to track validation efforts label Jul 9, 2021
@mkArtakMSFT mkArtakMSFT added this to the 6.0-rc1 milestone Jul 9, 2021
DamianEdwards added a commit that referenced this issue Jul 9, 2021
-Depends on dotnet/sdk#18459 flowing to dotnet/aspnetcore repo before we can merge this
- There's a couple of places we could remove usings from .razor files but need to verify the global usings flow into Razor compiler (#34217)
- Reordered usings in some places to ensure they get emitted in alphabetical order (modulo System.* & Microsoft.* coming before any others)
DamianEdwards added a commit that referenced this issue Jul 10, 2021
-Depends on dotnet/sdk#18459 flowing to dotnet/aspnetcore repo before we can merge this
- There's a couple of places we could remove usings from .razor files but need to verify the global usings flow into Razor compiler (#34217)
- Reordered usings in some places to ensure they get emitted in alphabetical order (modulo System.* & Microsoft.* coming before any others)
@captainsafia
Copy link
Member

At the moment, the set of imports that we include via the SDK:

// <autogenerated />
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Threading;
global using global::System.Threading.Tasks;

Mostly overlaps with the set that we include by default in the generated Razor code.

"@using System\r\n" +
"@using System.Collections.Generic\r\n" +
"@using System.Linq\r\n" +
"@using System.Threading.Tasks\r\n" +
"@using " + ComponentsApi.RenderFragment.Namespace + "\r\n"; // Microsoft.AspNetCore.Components

So we can already remove those imports.

Razor currently uses the _Imports.razor file to define global usings. Moving the usings from Imports.razor to a global usings file doesn't seem to work for a Blazor WASM app.

global using BlazorWasmApp.Shared;

will fail to compile due to issues resolving types in the Shared namespace.

/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]

@captainsafia
Copy link
Member

Closing in favor of dotnet/razor#7539.

@ghost ghost added Done This issue has been fixed and removed Working labels Jul 26, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Done This issue has been fixed feature-mvc-razor-views Features related to the Razor view engine for Razor pages and MVC views Validation This issue is used to track validation efforts
Projects
None yet
Development

No branches or pull requests

5 participants