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

Split dotnet/runtime-only downlevel LibraryImport support into its own source generator #106436

Merged
merged 14 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
AfterTargets="ResolveTargetingPackAssets">
<ItemGroup>
<_targetingPackReferenceExclusion Include="$(TargetName)" />
<_targetingPackReferenceExclusion Include="@(_ResolvedProjectReferencePaths->Metadata('Filename'))" />
<_targetingPackReferenceExclusionProjects Include="@(_ResolvedProjectReferencePaths->Metadata('Filename'))" />
<_targetingPackReferenceExclusion Include="@(DefaultReferenceExclusion)" />
</ItemGroup>

Expand All @@ -177,7 +177,7 @@
<_targetingPackReferenceWithProjectName Include="@(Reference->WithMetadataValue('ExternallyResolved', 'true')->Metadata('Filename'))"
OriginalIdentity="%(Identity)" />
<_targetingPackIncludedReferenceWithProjectName Include="@(_targetingPackReferenceWithProjectName)"
Exclude="@(_targetingPackReferenceExclusion)" />
Exclude="@(_targetingPackReferenceExclusion);@(_targetingPackReferenceExclusionProjects->WithMetadataValue('ReferenceOutputAssembly', 'true'))" />
<_targetingPackExcludedReferenceWithProjectName Include="@(_targetingPackReferenceWithProjectName)"
Exclude="@(_targetingPackIncludedReferenceWithProjectName)" />
<Reference Remove="@(_targetingPackExcludedReferenceWithProjectName->Metadata('OriginalIdentity'))" />
Expand All @@ -187,7 +187,7 @@
<_targetingPackAnalyzerReferenceWithProjectName Include="@(Analyzer->WithMetadataValue('ExternallyResolved', 'true')->Metadata('Filename'))"
OriginalIdentity="%(Identity)" />
<_targetingPackIncludedAnalyzerReferenceWithProjectName Include="@(_targetingPackAnalyzerReferenceWithProjectName)"
Exclude="@(_targetingPackReferenceExclusion)" />
Exclude="@(_targetingPackReferenceExclusion);@(_targetingPackReferenceExclusionProjects->WithMetadataValue('OutputItemType', 'Analyzer'))" />
<_targetingPackExcludedAnalyzerReferenceWithProjectName Include="@(_targetingPackAnalyzerReferenceWithProjectName)"
Exclude="@(_targetingPackIncludedAnalyzerReferenceWithProjectName)" />
<Analyzer Remove="@(_targetingPackExcludedAnalyzerReferenceWithProjectName->Metadata('OriginalIdentity'))" />
Expand Down
44 changes: 27 additions & 17 deletions eng/generators.targets
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
<Project>

<PropertyGroup>
<!-- Enable LibraryImportGenerator for CoreLib. -->
Copy link
Member

Choose a reason for hiding this comment

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

I understand that this already existed before this change but it feels weird to special case CoreLib in here. Can we move this property into the shared CoreLib project instead?

<EnableLibraryImportGenerator Condition="'$(EnableLibraryImportGenerator)' == '' and
'$(MSBuildProjectName)' == 'System.Private.CoreLib'">true</EnableLibraryImportGenerator>
</PropertyGroup>

<ItemGroup>
<EnabledGenerators Include="LibraryImportGenerator" Condition="'$(EnableLibraryImportGenerator)' == 'true'" />
<!-- If the current project is not System.Private.CoreLib, we enable the LibraryImportGenerator source generator
<!-- We enable the Downlevel LibraryImportGenerator
when the project is a C# source or test project that:
- doesn't target the a TFM that includes LibraryImportGenerator or
- doesn't reference the live targeting pack (i.e. when inbox) and
- references System.Private.CoreLib, or
- references System.Runtime.InteropServices -->
<EnabledGenerators Include="LibraryImportGenerator"
- doesn't target the a TFM that includes LibraryImportGenerator -->
<EnabledGenerators Include="DownlevelLibraryImportGenerator"
Condition="'$(EnableLibraryImportGenerator)' == '' and
(
'$(IsSourceProject)' == 'true' or
'$(IsTestProject)' == 'true' or
'$(IsTestSupportProject)' == 'true'
) and
'$(MSBuildProjectExtension)' == '.csproj' and
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', '$(NetCoreAppMinimum)'))" />
Copy link
Member

@ViktorHofer ViktorHofer Aug 22, 2024

Choose a reason for hiding this comment

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

This should just be $(TargetFrameworkIdentifier)' != '.NETCoreApp' as we shouldn't support targeting an out-of-support .NETCoreApp TFM.


<!-- We enable the LibraryImportGenerator source generator
when the project is a C# source project that:
- references System.Private.CoreLib directly
- references System.Runtime.InteropServices directly and not through the live targeting pack (i.e. when inbox) -->
<EnabledGenerators Include="LibraryImportGenerator"
Condition="'$(EnableLibraryImportGenerator)' == '' and
'$(IsSourceProject)' == 'true' and
'$(MSBuildProjectExtension)' == '.csproj' and
(
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0')) or
(
'$(DisableImplicitFrameworkReferences)' == 'true' and
(
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true' or
'@(ProjectReference->AnyHaveMetadataValue('Identity', '$(CoreLibProject)'))' == 'true'
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Private.CoreLib'))' == 'true'
Copy link
Member

Choose a reason for hiding this comment

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

This change looks wrong. System.Private.CoreLib is never a reference as it isn't part of the targeting pack.

)
)
)" />

<!-- We enable the ComInterfaceGenerator source generator
when the project is a C# source project that:
- references System.Runtime.InteropServices directly and not through the live targeting pack (i.e. when inbox) -->
<EnabledGenerators Include="ComInterfaceGenerator"
Condition="'$(IsSourceProject)' == 'true' and
'$(MSBuildProjectExtension)' == '.csproj' and
(
'$(DisableImplicitFrameworkReferences)' == 'true' and
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true'
)" />
'$(MSBuildProjectExtension)' == '.csproj' and
(
'$(DisableImplicitFrameworkReferences)' == 'true' and
'@(Reference->AnyHaveMetadataValue('Identity', 'System.Runtime.InteropServices'))' == 'true'
)" />
</ItemGroup>

<ItemGroup Condition="'@(EnabledGenerators)' != '' and
@(EnabledGenerators->AnyHaveMetadataValue('Identity', 'LibraryImportGenerator')) and
!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
@(EnabledGenerators->AnyHaveMetadataValue('Identity', 'DownlevelLibraryImportGenerator'))">
<Compile Include="$(CoreLibSharedDir)System\Runtime\InteropServices\LibraryImportAttribute.cs" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\InteropServices\StringMarshalling.cs" />
</ItemGroup>
Expand All @@ -57,6 +62,11 @@
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"
SetConfiguration="Configuration=$(LibrariesConfiguration)" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\DownlevelLibraryImportGenerator\DownlevelLibraryImportGenerator.csproj"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"
SetConfiguration="Configuration=$(LibrariesConfiguration)"
Condition="@(EnabledGenerators->AnyHaveMetadataValue('Identity', 'DownlevelLibraryImportGenerator'))" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\LibraryImportGenerator\LibraryImportGenerator.csproj"
ReferenceOutputAssembly="false"
OutputItemType="Analyzer"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<Folder Include="Resources\xlf\" />
<Compile Include="$(CommonPath)Roslyn\DiagnosticDescriptorHelper.cs" Link="Common\Roslyn\DiagnosticDescriptorHelper.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System\Runtime\InteropServices\JavaScript\MarshalerType.cs" Link="System\Runtime\InteropServices\JavaScript\MarshalerType.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices\gen\Common\UnreachableException.cs" Link="System\Runtime\InteropServices\Common\UnreachableException.cs" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" Link="Common\System\Runtime\CompilerServices\IsExternalInit.cs" />
</ItemGroup>

</Project>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public static IEnumerable<object[]> CodeSnippetsToCompile()

[Theory]
[MemberData(nameof(CodeSnippetsToCompile))]
public async Task ValidateSnippets(string source)
public void ValidateSnippets(string source)
{
Compilation comp = await TestUtils.CreateCompilation(source, allowUnsafe: true);
Compilation comp = TestUtils.CreateCompilation(source, allowUnsafe: true);
TestUtils.AssertPreSourceGeneratorCompilation(comp);

var newComp = TestUtils.RunGenerators(comp, out var generatorDiags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ public class Fails

[Theory]
[MemberData(nameof(CodeSnippetsToFail))]
public async Task ValidateFailSnippets(string source, string[]? generatorMessages, string[]? compilerMessages)
public void ValidateFailSnippets(string source, string[]? generatorMessages, string[]? compilerMessages)
{
Compilation comp = await TestUtils.CreateCompilation(source, allowUnsafe: true);
Compilation comp = TestUtils.CreateCompilation(source, allowUnsafe: true);
TestUtils.AssertPreSourceGeneratorCompilation(comp);

var newComp = TestUtils.RunGenerators(comp, out var generatorDiags,
Expand All @@ -75,10 +75,10 @@ public async Task ValidateFailSnippets(string source, string[]? generatorMessage
}

[Fact]
public async Task ValidateRequireAllowUnsafeBlocksDiagnostic()
public void ValidateRequireAllowUnsafeBlocksDiagnostic()
{
string source = CodeSnippets.TrivialClassDeclarations;
Compilation comp = await TestUtils.CreateCompilation(new[] { source }, allowUnsafe: false);
Compilation comp = TestUtils.CreateCompilation(new[] { source }, allowUnsafe: false);
TestUtils.AssertPreSourceGeneratorCompilation(comp);

var newComp = TestUtils.RunGenerators(comp, out var generatorDiags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{1FBC0A06-EE6
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{8FA3249B-3567-4C76-BA32-9488FC92994D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DownlevelLibraryImportGenerator", "gen\DownlevelLibraryImportGenerator\DownlevelLibraryImportGenerator.csproj", "{DD1873BA-2928-464A-B368-7AEFB1E49FD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Checked|Any CPU = Checked|Any CPU
Expand Down Expand Up @@ -783,6 +785,36 @@ Global
{B36C9254-0C55-414E-8403-03B4F18D5F35}.Release|x64.Build.0 = Release|Any CPU
{B36C9254-0C55-414E-8403-03B4F18D5F35}.Release|x86.ActiveCfg = Release|Any CPU
{B36C9254-0C55-414E-8403-03B4F18D5F35}.Release|x86.Build.0 = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|Any CPU.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|Any CPU.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|arm.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|arm.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|arm64.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|arm64.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|x64.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|x64.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|x86.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Checked|x86.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|arm.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|arm.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|arm64.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|arm64.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|x64.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Debug|x86.Build.0 = Debug|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|Any CPU.Build.0 = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|arm.ActiveCfg = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|arm.Build.0 = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|arm64.ActiveCfg = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|arm64.Build.0 = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|x64.ActiveCfg = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|x64.Build.0 = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|x86.ActiveCfg = Release|Any CPU
{DD1873BA-2928-464A-B368-7AEFB1E49FD8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -823,6 +855,7 @@ Global
{A043B2C0-71A2-4933-900B-E5DFDCB2B508} = {8FA3249B-3567-4C76-BA32-9488FC92994D}
{D5382D82-CC63-4781-A57B-4398A9757BDF} = {8FA3249B-3567-4C76-BA32-9488FC92994D}
{1FBC0A06-EE6A-4B51-BFA8-8EF93B6EE574} = {8FA3249B-3567-4C76-BA32-9488FC92994D}
{DD1873BA-2928-464A-B368-7AEFB1E49FD8} = {0522E1B2-BF32-4B3A-A74C-1CBF642E85E4}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D4031401-FEB5-4CCF-91C1-38F5646B2BFD}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<Compile Include="..\Common\ConvertToSourceGeneratedInteropFixer.cs" Link="Common\ConvertToSourceGeneratedInteropFixer.cs" />
<Compile Include="..\Common\FixAllContextExtensions.cs" Link="Common\FixAllContextExtensions.cs" />
<Compile Include="$(CoreLibSharedDir)System\Index.cs" Link="Common\System\Index.cs" />
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" Link="Common\System\Runtime\CompilerServices\IsExternalInit.cs" />
<Compile Include="../Common/UnreachableException.cs" Link="Common/UnreachableException.cs" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

Loading
Loading