-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Update implicit global usings feature to address issues #19521
Comments
Could |
@alexrp yes that was the intent but I didn't capture it properly in the item RE using alias support. Updated to make it clearer. |
nit: Suggest avoiding any parsing of the <Using Include="Microsoft.AspNetCore.Http.Results" Alias="Results" /> and <Using Include="Microsoft.AspNetCore.Results" Static="true" /> |
Thanks @dougbu I had the exact same thought last night and glad others agree. Will update the description. |
Note the SDK is being updated separately to actually implement support for this property in dotnet/sdk#19521 Addresses #35131
Note the SDK is being updated separately to actually implement support for this property in dotnet/sdk#19521 Addresses #35131
To call out a dependency - Windows Desktop (i.e. Windows Forms and WPF) can't be updated until this change is implemented. /cc: @ryalanms |
With this will it also be valid to also do stuff like: <Using Include="System.Runtime.Loader" Condition="'$(TargetFramework)' == 'net5.0'" /> as well? |
Yes, conditions work on any kind of MSBuild item. |
@DamianEdwards I do not think we should support |
@dsplaisted from the PR
|
@DamianEdwards Any ETA on when all this can be completed and in the daily rc.1 builds so I can try it locally / on my repositories in CI? Yes I rigged up my CI (github actions) to always install the daily quality builds from the .NET install scripts. |
@AraHaan hopefully within the next week. |
@DamianEdwards I think the users will have no way to figure that out themselves. I need to run a /pp:out.xml to realize the import order. And few people know the 3 pass of msbuild evaluation |
I think the best way is to in the future move away from setting things in the csproj and instead put everything in an Directory.Build.props (that are not items in msbuild / properties that can properly be set there) and then an Directory.Build.targets file for everything else, while Directory.Packages.props contains all project or package references. I already do that but I think updated templates for that might actually help out a lot more. |
Context: dotnet/sdk#19521 Fixes: dotnet#6075 Fixes: dotnet#6076 We need to make two sets of changes for C# 10: 1. Support "global usings". Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Use `$(Nullable)` `enable` by default in project templates. To test this, our .NET 6 MSBuild tests use `Nullable=enable` and `ImplicitUsings=enable` by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`. So `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> So these items are present at the time `.csproj` files are evaluated. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup>
@wli3 this is already the case today for many aspects of the SDK that we default, including file globbing, versions, etc. The difference in evaluation order between properties and items is long-standing and while it's not particularly discoverable it's how MSBuild works. |
The different here is, once we made it default someday, users have to migrate their code base over, and disabling an certain import across repo would be a common thing to do. Most of the "advance" SDK config are not that common. |
Doesn't change the fact that MSBuild works this way. And VB has had this feature for a very long time, on by default. If we do investigate making this feature on by default in a future SDK major release we'll need to look at potential issues associated with the change as part of that effort. |
Context: dotnet/sdk#19521 Fixes: dotnet#6075 Fixes: dotnet#6076 We need to make two sets of changes for C# 10: 1. Support "global usings". Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Use `$(Nullable)` `enable` by default in project templates. To test this, our .NET 6 MSBuild tests use `Nullable=enable` and `ImplicitUsings=enable` by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`. So `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> So these items are present at the time `.csproj` files are evaluated. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup>
A minor issue with this I've just come across trying out the new I have a library that targets the following TFMs to share some simple POCOs: <TargetFrameworks>net461;netstandard2.0;netstandard2.1;net5.0;net6.0</TargetFrameworks> The Severity Code Description Project File Line Suppression State
Error CS0234 The type or namespace name 'Http' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) MyApp.Models (net461) C:\Coding\MyApp\src\MyApp.Models\obj\Debug\net461\MyApp.Models.GlobalUsings.g.cs 7 Active I can work around this by adding the following to the <ItemGroup>
<Using Remove="System.Net.Http" />
</ItemGroup> |
The error might be covered by the disclaimer that C# 10.0 is supported on .NET 6 only. |
Yes this scenario (multi-targeting) was discussed during discussions for this update and and as @KalleOlaviNiemitalo points out, given that C# 10 is not supported on anything other than .NET 6, projects changing |
Context: dotnet/sdk#19521 Fixes: dotnet#6075 Fixes: dotnet#6076 We need to make two sets of changes for C# 10: 1. Support "global usings". Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Use `$(Nullable)` `enable` by default in project templates. To test this, our .NET 6 MSBuild tests use `Nullable=enable` and `ImplicitUsings=enable` by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`. So `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> So these items are present at the time `.csproj` files are evaluated. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup>
Context: dotnet/sdk#19521 Fixes: dotnet#6075 Fixes: dotnet#6076 We need to make two sets of changes for C# 10: 1. Support "global usings". Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Use `$(Nullable)` `enable` by default in project templates. To test this, our .NET 6 MSBuild tests use `Nullable=enable` and `ImplicitUsings=enable` by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`. So `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> So these items are present at the time `.csproj` files are evaluated. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup>
Why is this not just driven by the template? Have an |
@DamianEdwards please take a look at dotnet/project-system#7512 which adds a flag to enable/disable this feature from the Project Properties UI. In future we can add UI to control the specific set of items. |
Fixes: #6075 Fixes: #6076 Context: dotnet/sdk#19521 We need to make two sets of changes for C# 10: 1. Support ["global usings"][0]. Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Set `$(Nullable)`=enable by default in project templates, i.e. enable [C#8 nullable reference types][1] by default. To test this, our .NET 6 MSBuild tests use `$(Nullable)`=enable and `$(ImplicitUsings)`=enable by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`, and the `System` namespace will be in `@(Using)` by default. `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> so that these `using`s are present at the time `.csproj` files are compiled. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely, or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup> [0]: https://github.com/dotnet/csharplang/blob/b89d4c934041db923f7238b1427cd5f3ae71ed4b/proposals/csharp-10.0/GlobalUsingDirective.md#global-using-alias-directives [1]: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references
Fixes: #6075 Fixes: #6076 Context: dotnet/sdk#19521 We need to make two sets of changes for C# 10: 1. Support ["global usings"][0]. Our .NET 6 templates should have no `using` statements at the top of `.cs` files. 2. Set `$(Nullable)`=enable by default in project templates, i.e. enable [C#8 nullable reference types][1] by default. To test this, our .NET 6 MSBuild tests use `$(Nullable)`=enable and `$(ImplicitUsings)`=enable by default and do not include `using` statements in `.cs` files. I've made a new `MainActivity.cs` for our .NET 6 MSBuild tests. The "legacy" Xamarin.Android tests will use the original file. Our default `global using` are: global using global::Android.App; global using global::Android.Widget; global using Bundle = global::Android.OS.Bundle; The last one is intentionally not bringing in `Android.OS`, because `Android.OS.Environment` would conflict with `System.Environment`, and the `System` namespace will be in `@(Using)` by default. `AutoImport.props` should become: <ItemGroup Condition=" '$(TargetPlatformIdentifier)' == 'android' and ('$(ImplicitUsings)' == 'true' or '$(ImplicitUsings)' == 'enable') "> <Using Include="Android.App" /> <Using Include="Android.Widget" /> <Using Include="Android.OS.Bundle" Alias="Bundle" /> </ItemGroup> so that these `using`s are present at the time `.csproj` files are compiled. Any templates will add: <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> If users want to configure these settings, they can remove `$(ImplicitUsings)` from the `.csproj` completely, or remove specific `@(Using)` items: <ItemGroup> <Using Remove="Android.App" /> </ItemGroup> [0]: https://github.com/dotnet/csharplang/blob/b89d4c934041db923f7238b1427cd5f3ae71ed4b/proposals/csharp-10.0/GlobalUsingDirective.md#global-using-alias-directives [1]: https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references
Are the SDK-specific MSBuild properties still valid as of RC1? For example, Edit: I see from Mikayla's question that these have been removed. |
Closing as this is complete now. |
@martincostello while unsupported, you may be able to solve this problem using the approach described in dotnet/project-system#7488 (comment) |
Disable unnecessary usings statements. The ambiguous references seems to be a result of a compiler bug or feature which gets confused when implicit using and unnecessary usings statements are used. SDK Default namespaces
Microsoft.NET.Sdk.Web
Microsoft.NET.Sdk.Worker
|
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:
<DisableImplicitNamespaceImports>
to<ImplicitUsings>
<DisableImplicitNamespaceImports>
property for disabling implicit namespace imports<ImplicitUsings>
property to the schema file so it appears in statement completion<ImplictUsings>
property is set to "true" or "enable"<ImplicitUsings>
is equal to "true" or "enable"<Import>
to<Using>
<Import>
item type for specifying namespaces to import<Using>
items are declared (even if<ImplicitUsings>
is false)<Import>
items for Visual Basic are not changed from what they were in .NET 5<Using>
items to support definingusing [alias]
andusing static [type]
as well asusing [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;
<ImplicitUsings>enable</ImplicitUsings>
in the.csproj
fileExample project file content after these changes:
The text was updated successfully, but these errors were encountered: