Skip to content

Update implicit global usings feature to address issues #19521

Closed
@DamianEdwards

Description

@DamianEdwards

The new implicit global usings feature added to the SDK in 6.0.100-preview.7 has caused a few issues in some important scenarios such that some changes will be made to mitigate them and generally improve its usability.

These changes are targeting 6.0.100-rc.1

Items:

  • Rename the MSBuild property from <DisableImplicitNamespaceImports> to <ImplicitUsings>
    • Note this is for C# only
    • Visual Basic will continue to use the <DisableImplicitNamespaceImports> property for disabling implicit namespace imports
  • Add the <ImplicitUsings> property to the schema file so it appears in statement completion
  • Change the defined conditions to only enable adding the implicit usings from the SDK when the <ImplictUsings> property is set to "true" or "enable"
    • The existing conditions based on TFM should be removed
    • The only condition for adding the implicit usings as defined in the SDK will be the check that <ImplicitUsings> is equal to "true" or "enable"
  • Rename the item type that specifies the global namespaces to emit to the generated .cs file from <Import> to <Using>
    • Note this is for C# only
    • Visual Basic will continue to use the <Import> item type for specifying namespaces to import
  • Update schema for ImplicitUsings feature msbuild#6745
  • Ensure the generated file is still emitted if any <Using> items are declared (even if <ImplicitUsings> is false)
  • Ensure that implicit <Import> items for Visual Basic are not changed from what they were in .NET 5
  • Update processing of <Using> items to support defining using [alias] and using static [type] as well as using [namespace], e.g.:
    • <Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" />
      emits global using Results = global::Microsoft.AspNetCore.Http.Results;
    • <Using Include="Microsoft.AspNetCore.Http.Results" Static="True" />
      emits global using static global::Microsoft.AspNetCore.Http.Results;
    • <Using Include="Microsoft.AspNetCore.Http" />
      emits global using global::Microsoft.AspNetCore.Http;
  • Update the C# project templates to enable implicit usings for new .NET 6 projects, i.e. include <ImplicitUsings>enable</ImplicitUsings> in the .csproj file

Example project file content after these changes:

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

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <Using Include="My.Awesome.Namespace" />
  </ItemGroup>
</Project>

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions