Skip to content

Use LibraryImport instead of DllImport for proper AOT support#323

Merged
Tyrrrz merged 3 commits intoprimefrom
copilot/use-libraryimport-for-aot-support
Mar 20, 2026
Merged

Use LibraryImport instead of DllImport for proper AOT support#323
Tyrrrz merged 3 commits intoprimefrom
copilot/use-libraryimport-for-aot-support

Conversation

Copy link
Contributor

Copilot AI commented Mar 20, 2026

DllImport is not AOT-compatible; LibraryImport (introduced in .NET 7) uses a source generator to emit the P/Invoke stub at compile time, making it trimmer- and AOT-safe. LibraryImport cannot be used on older targets even with polyfills, because the source generator (while SDK-provided and TFM-agnostic) does not emit an implementation for pre-.NET 7 targets — the generated P/Invoke stub relies on runtime APIs introduced in .NET 7.

Changes

  • CliWrap/Utils/NativeMethods.cs — Made NativeMethods and Unix classes partial; switched Kill to [LibraryImport] on net7.0+, keeping [DllImport] for older targets via conditional compilation, with a comment explaining why the source generator doesn't help on older targets:
// LibraryImport's source generator doesn't emit an implementation for pre-.NET 7 targets
// because the generated code relies on runtime APIs introduced in .NET 7.
#if NET7_0_OR_GREATER
    [LibraryImport("libc", EntryPoint = "kill", SetLastError = true)]
    public static partial int Kill(int pid, int sig);
#else
    [DllImport("libc", EntryPoint = "kill", SetLastError = true)]
    public static extern int Kill(int pid, int sig);
#endif
  • CliWrap/CliWrap.csproj — Added <AllowUnsafeBlocks>true</AllowUnsafeBlocks> unconditionally (required by the LibraryImport source generator on net7.0+; harmless on older targets).

  • CliWrap.Signaler/Utils/NativeMethods.cs — Left unchanged; the Signaler project targets net35 where LibraryImport is unavailable.


⚡ Quickly spin up Copilot coding agent tasks from anywhere on your macOS or Windows machine with Raycast.

Copy link
Owner

@Tyrrrz Tyrrrz left a comment

Choose a reason for hiding this comment

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

…nsafeBlocks

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/CliWrap/sessions/be98739b-de00-4db6-89eb-a67dee97b267
…s due to runtime API requirements

Co-authored-by: Tyrrrz <1935960+Tyrrrz@users.noreply.github.com>
Agent-Logs-Url: https://github.com/Tyrrrz/CliWrap/sessions/9cf901e2-f7e6-4cc2-98a2-9aba5902746b
@Tyrrrz Tyrrrz marked this pull request as ready for review March 20, 2026 20:08
Copilot AI review requested due to automatic review settings March 20, 2026 20:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates CliWrap’s Unix P/Invoke for kill to use LibraryImport on .NET 7+ to improve trimmer/AOT compatibility while retaining DllImport for older target frameworks.

Changes:

  • Switched NativeMethods.Unix.Kill to [LibraryImport] for NET7_0_OR_GREATER, keeping [DllImport] for older TFMs via conditional compilation.
  • Marked NativeMethods / NativeMethods.Unix as partial to support source-generated P/Invoke stubs.
  • Enabled AllowUnsafeBlocks in CliWrap.csproj (needed by the LibraryImport generator output).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
CliWrap/Utils/NativeMethods.cs Uses LibraryImport for kill on .NET 7+ and keeps DllImport for older frameworks; updates types to partial for source generation.
CliWrap/CliWrap.csproj Enables unsafe blocks to allow compilation of LibraryImport-generated stubs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Tyrrrz Tyrrrz merged commit 7d6c219 into prime Mar 20, 2026
11 checks passed
@Tyrrrz Tyrrrz deleted the copilot/use-libraryimport-for-aot-support branch March 20, 2026 20:12
@codecov
Copy link

codecov bot commented Mar 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.73%. Comparing base (1705a3d) to head (4ca9da5).
⚠️ Report is 1 commits behind head on prime.

Additional details and impacted files
@@            Coverage Diff             @@
##            prime     #323      +/-   ##
==========================================
+ Coverage   94.48%   94.73%   +0.24%     
==========================================
  Files          45       45              
  Lines        1215     1215              
  Branches       89       87       -2     
==========================================
+ Hits         1148     1151       +3     
  Misses         42       42              
+ Partials       25       22       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants