Open
Description
This is a clean repro:
#:package LibGit2Sharp@0.*
#:package Spectre.Console@0.*
using System;
using LibGit2Sharp;
using Spectre.Console;
using var repo = new Repository(Environment.CurrentDirectory);
var signature = repo.Config.BuildSignature(DateTimeOffset.Now);
if (signature == null)
{
AnsiConsole.MarkupLine("[red] Unexpected error: Git user.name and/or user.email are not configured.[/]");
return 1;
}
else
{
AnsiConsole.MarkupLine($"[lime]{signature}[/]");
return 0;
}When running this script from a git repo dir with dotnet run --file repro.cs, you get the proper signature built from the found config.
Doing dotnet publish repro.cs and then running it from artifacts\repro\repro.exe will result in the error path consistently.
The PR #2188 fixes it and can be verified by changing the above repro script to use my CI feed:
#:package LibGit2Sharp@0.31.1
#:package Spectre.Console@0.*
#:property RestoreSources=https://api.nuget.org/v3/index.json;https://pkg.kzu.app/index.jsonMetadata
Metadata
Assignees
Labels
No labels
Activity
kzu commentedon Jul 22, 2026
Root cause analysis
This is not a missing native library / load failure, and it is not the same as #2160 (static linking for single-file AOT). The native
git2-*binary loads correctly next to the published AOT executable.The failure is in config discovery under Native AOT.
What fails
Configuration.BuildSignatureonly readsuser.name/user.emailfrom the layered config:When opening a repository, global/system/xdg/programdata paths are resolved via:
Under CoreCLR (
dotnet run), those finds succeed and global config is loaded (includinginclude.pathchains that often holduser.name/user.email).Under Native AOT (
dotnet publish+ run the native exe), the same APIs appear to fail:HasConfig(Global)/HasConfig(System)are false, so only local.git/configis present. If identity is not set locally,BuildSignaturereturnsnull.Why it fails under AOT
GitBufis a sequential-layout class passed by value to P/Invoke:On CoreCLR, blittable sequential classes are typically pinned and native code writes directly into the object fields.
On Native AOT, sequential class marshalling is effectively
[In]-only: native code can fill a temporary buffer and return success, but managedptr/sizeare never updated.ConvertPaththen does:So discovery returns "no path", global/system config is never added, and signature building fails whenever identity lives outside the local repo config.
Minimal probe (struct vs class vs pin)
Calling the same
git_config_find_globalexport aftergit_libgit2_init:refblittable structrc=0, path filled)rc=0butptr=0GCHandle+IntPtr)That isolates the bug to marshalling of sequential classes under Native AOT, not to libgit2 path logic or environment variables (
USERPROFILE/HOMEare fine; settingHOMEdoes not help).Why "explicit path" workarounds work
skips
git_config_find_*and still loads includes. That confirms libgit2 config parsing is fine; only discovery viaGitBufclass P/Invoke is broken under AOT.Proposed fix
Introduce a blittable buffer type and use it for path-returning APIs that currently go through
ConvertPath.1. Add a native-friendly buffer struct
Keep the existing
GitBufclass for call sites that still use class marshalling (can migrate later).2. Change path-find P/Invokes to
ref GitBufNativeAt minimum (these feed
Configuration/ repository discovery):git_config_find_globalgit_config_find_systemgit_config_find_xdggit_config_find_programdatagit_repository_discovergit_buf_dispose(ref GitBufNative)overload3. Update
Proxy.ConvertPathMethod-group binding stays clean, e.g.
ConvertPath(NativeMethods.git_config_find_global).4. Expected outcome
After this change, Native AOT published apps should again:
include.pathrepo.Config.BuildSignature(DateTimeOffset.Now)matchdotnet runbehavior whenuser.name/user.emailcome from global configFollow-up (optional, larger)
Many other APIs still pass class
GitBuffor out-buffers (short ids, messages, opts getters, etc.). Those can misbehave under AOT the same way. Migrating remaining out-git_bufcall sites toref GitBufNativewould make the whole surface AOT-safe; the config-find path above is the high-impact fix for this issue.Not in scope for this fix
libgit2, not about config discovery.Configuration.BuildFrom(...)paths are only needed on current NuGet packages.Happy to open a PR with the surgical
GitBufNative+ConvertPathchange if that approach looks good to maintainers.kzu commentedon Jul 22, 2026
I promise the above is not just AI slop. I have this issue currently and am looking for a proper fix. I'll submit a PR that does so, but it's written by Grok 4.5.
Fix rebase issue when run with dnx go