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

ImplicitNamespaceImports generated codefile generates an invalid import if you try to import as an alias. #19446

Closed
AraHaan opened this issue Jul 31, 2021 · 9 comments
Milestone

Comments

@AraHaan
Copy link
Member

AraHaan commented Jul 31, 2021

Let's say for example I try to import these:

<Import Include="System" />
<Import Include="System.Collections.Generic" />
<Import Include="System.CommandLine" />
<Import Include="System.CommandLine.Help" />
<Import Include="System.CommandLine.Invocation" />
<Import Include="System.CommandLine.IO" />
<Import Include="System.Globalization" />
<Import Include="System.IO" />
<Import Include="System.Linq" />
<Import Include="System.Reflection" />
<Import Include="System.Text" />
<Import Include="System.Threading.Tasks" />
<Import Include="Elskom.Generic.Libs" />
<Import Include="Microsoft.Diagnostics.NETCore.Client" />
<Import Include="newsmakeResources = Elskom.Generic.Libs.Properties" />

The resulting generated <project name>.ImplicitNamespaceImports.cs file will contain:

// <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;
global using global::System.CommandLine;
global using global::System.CommandLine.Help;
global using global::System.CommandLine.Invocation;
global using global::System.CommandLine.IO;
global using global::System.Globalization;
global using global::System.Reflection;
global using global::System.Text;
global using global::Elskom.Generic.Libs;
global using global::Microsoft.Diagnostics.NETCore.Client;
global using global::newsmakeResources = Elskom.Generic.Libs.Properties;

Which results in: <project name>/obj/Debug/net6.0/<project name>.ImplicitNamespaceImports.cs(18,40): error CS1525: Invalid expression term '='

I feel like whereever in the .NET SDK that generates that file that it should actully explicitly look for things like =, static, etc before it tries to insert global:: then insert it after it finds static and/or = so that way the compilation would work.

My .NET SDK version currently is: 6.0.100-rc.1.21366.58.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Jul 31, 2021
@AraHaan
Copy link
Member Author

AraHaan commented Jul 31, 2021

Bug seems to be here:

<ItemGroup>
<_UniqueImport Include="@(Import->Distinct())" />
<_ImportFileLine Include="// %3Cautogenerated />"/>
<_ImportFileLine Include="global using global::%(_UniqueImport.Identity)%3B"/>
</ItemGroup>

@AraHaan
Copy link
Member Author

AraHaan commented Jul 31, 2021

The only way I can think of to this this problem is to actually move this into either an inline C# code task in the SDK (you guys probably have a policy against that) or move it to one of the Build Task projects that builds an dll so it can properly be fixed and have it assign to _ImportFileLine so that way the rest of the Target would work without issues.

AraHaan added a commit to AraHaan/sdk that referenced this issue Jul 31, 2021
AraHaan added a commit to AraHaan/sdk that referenced this issue Aug 1, 2021
AraHaan added a commit to AraHaan/sdk that referenced this issue Aug 1, 2021
@AraHaan
Copy link
Member Author

AraHaan commented Aug 10, 2021

Welp I tried on the PR and it seems no mater what I do something in the tests decides to fail currently.

@KalleOlaviNiemitalo
Copy link
Contributor

According to #19521, it should be:

<Using Include="Elskom.Generic.Libs.Properties" Alias="newsmakeResources"/>

However, #19599 has not been merged yet.

@AraHaan
Copy link
Member Author

AraHaan commented Aug 10, 2021

So basically I should be doing <Using instead of <Import Include="..."?

@KalleOlaviNiemitalo
Copy link
Contributor

For C# projects yes, use <Using> rather than <Import>, after #19599 is merged.

@AraHaan
Copy link
Member Author

AraHaan commented Aug 10, 2021

But when will the daily builds ship with #19599 after it gets merged? On my Repositories and locally I use the daily builds from the .NET install scripts.

@wli3
Copy link

wli3 commented Aug 10, 2021

@JunTaoLuo could you look into this?

@DamianEdwards
Copy link
Member

@wli3 @JunTaoLuo doesn't wort at Microsoft anymore.

@AraHaan the change to support <Using /> has been merged and is now available in daily builds of the SDK. As such I'm closing this issue. If you have any other problems please log a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants