From 6fb4d8581fc867241cf8d81993368e6aa639067e Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Thu, 19 Mar 2026 22:20:20 +0000
Subject: [PATCH 1/7] Use RegisterPostInitializationOutput in
DownlevelLibraryImportGenerator to inject internal LibraryImportAttribute and
StringMarshalling definitions
Co-authored-by: jkoritzinsky <1571408+jkoritzinsky@users.noreply.github.com>
---
eng/generators.targets | 6 ----
.../DownlevelLibraryImportGenerator.cs | 34 +++++++++++++++++++
.../AdditionalAttributesOnStub.cs | 1 -
.../Compiles.cs | 26 ++++++++------
.../Diagnostics.cs | 2 +-
5 files changed, 50 insertions(+), 19 deletions(-)
diff --git a/eng/generators.targets b/eng/generators.targets
index c24eca9be55dd1..3290f37580be29 100644
--- a/eng/generators.targets
+++ b/eng/generators.targets
@@ -48,12 +48,6 @@
" />
-
-
-
-
-
diff --git a/src/libraries/System.Runtime.InteropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportGenerator.cs
index 63ca6ca49ff81d..101bc1df0ebc3e 100644
--- a/src/libraries/System.Runtime.InteropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportGenerator.cs
+++ b/src/libraries/System.Runtime.InteropServices/gen/DownlevelLibraryImportGenerator/DownlevelLibraryImportGenerator.cs
@@ -35,8 +35,42 @@ public static class StepNames
public const string GenerateSingleStub = nameof(GenerateSingleStub);
}
+ // Internal definitions of LibraryImportAttribute and StringMarshalling that are injected
+ // into user projects targeting downlevel frameworks that don't have these types in the BCL.
+ // These definitions only expose the properties and enum values supported downlevel.
+ private const string GeneratedInteropTypes = """
+ //
+ #pragma warning disable CS0436
+ #nullable enable
+ namespace System.Runtime.InteropServices
+ {
+ [global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
+ internal sealed class LibraryImportAttribute : global::System.Attribute
+ {
+ public LibraryImportAttribute(string libraryName) { }
+ public string? EntryPoint { get; set; }
+ public StringMarshalling StringMarshalling { get; set; }
+ public global::System.Type? StringMarshallingCustomType { get; set; }
+ public bool SetLastError { get; set; }
+ }
+
+ internal enum StringMarshalling
+ {
+ Custom = 0,
+ Utf8 = 1,
+ Utf16 = 2,
+ }
+ }
+ #pragma warning restore CS0436
+ """;
+
public void Initialize(IncrementalGeneratorInitializationContext context)
{
+ // Inject internal definitions of LibraryImportAttribute and StringMarshalling into the compilation
+ // so that users targeting downlevel frameworks can apply [LibraryImport] to their methods.
+ context.RegisterPostInitializationOutput(static ctx =>
+ ctx.AddSource("LibraryImportInteropTypes.g.cs", GeneratedInteropTypes));
+
// Collect all methods adorned with LibraryImportAttribute and filter out invalid ones
// (diagnostics for invalid methods are reported by the analyzer)
var methodsToGenerate = context.SyntaxProvider
diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs
index ebb295f6d3fe79..8d58aa3679f45c 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/AdditionalAttributesOnStub.cs
@@ -128,7 +128,6 @@ public async Task SkipLocalsInitOnDownlevelTargetFrameworks(TestTargetFramework
{
string source = $$"""
using System.Runtime.InteropServices;
- {{CodeSnippets.LibraryImportAttributeDeclaration}}
partial class C
{
[LibraryImportAttribute("DoesNotExist")]
diff --git a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs
index 225ae48288ebc1..3bc8b99d0adcc5 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs
+++ b/src/libraries/System.Runtime.InteropServices/tests/LibraryImportGenerator.UnitTests/Compiles.cs
@@ -500,8 +500,9 @@ protected override ParseOptions CreateParseOptions()
public static IEnumerable