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

Update implicit global usings feature #19599

Merged
merged 4 commits into from
Aug 13, 2021
Merged

Conversation

pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Aug 9, 2021

  • Undo changes to VB projects
  • For C#, projects use the Using itemgroup to generate global usings. Support aliasing and static imports with attributes.
  • Use ImplicitUsings to determine if SDK-specific namespaces are imported by default.

Contributes to #19521

@DamianEdwards
Copy link
Member

Can we get the schema files updated as part of this too please?

@pranavkm
Copy link
Contributor Author

pranavkm commented Aug 9, 2021

Can we get the schema files updated as part of this too please?

Is it in this repo?

@pranavkm pranavkm marked this pull request as ready for review August 9, 2021 20:25
@DamianEdwards
Copy link
Member

@DamianEdwards
Copy link
Member

@pranavkm logged dotnet/msbuild#6745 to track the schema updates

@DamianEdwards
Copy link
Member

@pranavkm when you merge this please change it to "Contributes to #19521" as it doesn't fix everything in that issue yet.

@AraHaan
Copy link
Member

AraHaan commented Aug 10, 2021

In this case cant all projects generate usings for System, System.Collections.Generic, System.Threading.Tasks, System.Linq, etc by default?

There might be a few others as well that even my projects use commonly as well too (exposed in Elskom/Sdk#248). In which case I can change some of them to the new <Using and nuke the other more common ones for the ones the .NET SDK would automatically add then.

src/BlazorWasmSdk/Sdk/Sdk.props Show resolved Hide resolved
Comment on lines 59 to 65
var existing = File.ReadAllText(outputFilePath);

if (string.Equals(existing, contentToWrite, StringComparison.Ordinal))
{
Log.LogMessage(MessageImportance.Low, Strings.SkippingUnchangedFile, OutputFile.ItemSpec);
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The WriteLinesToFile task has a WriteOnlyWhenDifferent which handles this. Rather than duplicate the logic here (and have to add an additional string), I think we should change it so that the GenerateGlobalUsings task outputs the generated file contents to a property, and then we use the WriteLinesToFile task to write it with WriteOnlyWhenDifferent set to true.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that before on my failed PR and it would result in test failures, I also tried consuming that task directly in a task like this and it would pass when ran on it's own on the specific test assembly for this dll, but then fail on other tests or at least the same tests in CI vs when ran locally.

Comment on lines 38 to 51
if (@using.Static)
{
builder.Append("static ");
}

if (!string.IsNullOrEmpty(@using.Alias))
{
builder.Append(@using.Alias)
.Append(" = ");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if a Using item has both Static and Alias metadata. This will generate C# code that won't compile, right? Should we error here in that case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we should error as global using static Foo = global::My.Namespace.Value isn't valid AFAIK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My plan was to let the compiler complain (it produces an error saying that you cannot alias a static using). It felt less complicated to do this in case a newer langversion started supporting this in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Letting the compiler generate the error seems less diagnosable to me. It will take you to the generated source file, but not indicate that the issue is with Import items.

I don't mind either way, it's just something to consider.

* Undo changes to VB projects
* For C#, projects use the `Using` itemgroup to generate global usings. Support aliasing and static imports with attributes.
* Use ImplicitUsings to determine if SDK-specific namespaces are imported by default.
@pranavkm pranavkm force-pushed the prkrishn/sdk-implicitusings branch from e64e990 to 08db8d7 Compare August 12, 2021 15:57
Copy link
Member

@RussKie RussKie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@pranavkm pranavkm merged commit f2cc77b into main Aug 13, 2021
@pranavkm pranavkm deleted the prkrishn/sdk-implicitusings branch August 13, 2021 01:40
@AraHaan
Copy link
Member

AraHaan commented Aug 13, 2021

Thanks.

RussKie added a commit to RussKie/winforms that referenced this pull request Aug 17, 2021
RussKie added a commit to dotnet/winforms that referenced this pull request Aug 17, 2021
RussKie added a commit to dotnet/wpf that referenced this pull request Aug 17, 2021
ryalanms pushed a commit to dotnet/wpf that referenced this pull request Aug 18, 2021
@@ -10,6 +10,30 @@ Copyright (c) .NET Foundation. All rights reserved.
***********************************************************************************************
-->
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Condition=" '$(DisableImplicitNamespaceImports)' != 'true' and '$(TargetFrameworkIdentifier)' == '.NETFramework'">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I please get a clarification why VB's implicit imports are now hidden behind DisableImplicitNamespaceImports, which is on by default? VB always had implicit imports on by default AFAIK.

This has broken dotnet/winforms#5437

/cc: @KathleenDollard @KlausLoeffelmann @DamianEdwards

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may be the culprit, which needs to be undone:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a test I built a .NET 5.0 VB app, and neither DisableImplicitNamespaceImports nor DisableImplicitFrameworkReferences are defined.

E.g.

dotnet new winforms --language vb -f net5.0
dotnet build /bl
start msbuild.binlog

image
image

And it has all imports:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this might have been missed. @pranavkm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the screen shot show "net6.0-windows" if this is a " .NET 5.0 VB app"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue that @RussKie is running in to is because of an Arcade change - dotnet/arcade#7745. That said, I missed undoing one of the VB changes that is covered as part of #19840

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

Successfully merging this pull request may close these issues.

7 participants