libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.12k stars 878 forks source link

Interacting with configuration where not ProgramData config does not exists now throws an exception #2021

Closed zentron closed 1 year ago

zentron commented 1 year ago

It's not immediately clear what side the bug is on, but the latest upgrade to libgit2 v1.6.2 seems to have introduced an issue with the loading of the ProgramData config path in lib2git.

Reproduction steps

  1. Ensure you have no configuration file in the program data directory. In the LibGit2Sharp tests these are generated automatically in %userprofile%\AppData\Local\Temp\LibGit2Sharp-TestRepos which may explain why it wasnt picked up in the tests

  2. Try to read any config

    var repo = new Repository();
    repo.Config.Get<bool>("core.autocrlf");

    Expected behavior

    The Configuration.cs constructor should load what configuration it can and continue on its way. libgit2 should presumably be returning a 0 as some of the other missing config paths do, which would result in the path resolving "successfully" to null

Actual behavior

The call down to NativeMethods.git_config_find_programdata returns a -1 with an error message that resolves as

LibGit2Sharp.LibGit2SharpException: the ProgramData file 'config' doesn't exist: 
   at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in D:\Development\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 154
   at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in D:\Development\libgit2sharp\LibGit2Sharp\Core\Ensure.cs:line 172
   at LibGit2Sharp.Core.Proxy.ConvertPath(Func`2 pathRetriever) in D:\Development\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 3801
   at LibGit2Sharp.Core.Proxy.git_config_find_programdata() in D:\Development\libgit2sharp\LibGit2Sharp\Core\Proxy.cs:line 497
   at LibGit2Sharp.Configuration..ctor(Repository repository, String repositoryConfigurationFileLocation, String globalConfigurationFileLocation, String xdgConfigurationFileLocation, String systemConfigurationFileLocation) in D:\Development\libgit2sharp\LibGit2Sharp\Configuration.cs:line 47
   at LibGit2Sharp.Repository.<>c__DisplayClass25_1.<.ctor>b__3() in D:\Development\libgit2sharp\LibGit2Sharp\Repository.cs:line 220
   at System.Lazy`1.CreateValue()

Testing against the previous version it looks like this was previously not an enforced configuration file.

Version of LibGit2Sharp (release number or SHA1)

master (commit 70d62d5)

Operating system(s) tested; .NET runtime tested

Windows 11

pde-cds commented 1 year ago

Can confirm this issue with the V0.27 Release

ethomson commented 1 year ago

https://github.com/libgit2/libgit2/pull/6547

bording commented 1 year ago

@zentron From my testing so far, https://github.com/libgit2/libgit2/pull/6547 does seem to have fixed the exception you mentioned, but it's not really clear what behavior you're expecting after that fix.

When I run your repro code using LibGit2Sharp 0.26.2, it wasn't valid to access the config when using the default Repository constructor either.

arturcic commented 1 year ago

@bording this is the exception I was getting in GitVersion when trying to update from 0.27.0-preview-0182 to 0.27.1

https://github.com/GitTools/GitVersion/actions/runs/4680350775/jobs/8291675407#step:4:119

    Error Message:
     LibGit2Sharp.LibGit2SharpException : the ProgramData file 'config' doesn't exist: 
    Stack Trace:
       at LibGit2Sharp.Core.Ensure.HandleError(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 154
     at LibGit2Sharp.Core.Ensure.ZeroResult(Int32 result) in /_/LibGit2Sharp/Core/Ensure.cs:line 172
     at LibGit2Sharp.Core.Proxy.ConvertPath(Func`2 pathRetriever) in /_/LibGit2Sharp/Core/Proxy.cs:line 3801
     at LibGit2Sharp.Core.Proxy.git_config_find_programdata() in /_/LibGit2Sharp/Core/Proxy.cs:line 497
     at LibGit2Sharp.Configuration..ctor(Repository repository, String repositoryConfigurationFileLocation, String globalConfigurationFileLocation, String xdgConfigurationFileLocation, String systemConfigurationFileLocation) in /_/LibGit2Sharp/Configuration.cs:line 47
     at LibGit2Sharp.Repository.<>c__DisplayClass25_1.<.ctor>b__3() in /_/LibGit2Sharp/Repository.cs:line 220
     at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
     at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
     at System.Lazy`1.CreateValue()
     at LibGit2Sharp.Repository.get_Config() in /_/LibGit2Sharp/Repository.cs:line 323
     at LibGit2Sharp.Core.PathCase..ctor(IRepository repo) in /_/LibGit2Sharp/Core/PathCase.cs:line 12
     at LibGit2Sharp.RepositoryExtensions.PathStartsWith(IRepository repository, String path, String value) in /_/LibGit2Sharp/RepositoryExtensions.cs:line 226
     at LibGit2Sharp.RepositoryExtensions.BuildRelativePathFrom(IRepository repo, String path) in /_/LibGit2Sharp/RepositoryExtensions.cs:line 213
     at LibGit2Sharp.Repository.ToFilePaths(IEnumerable`1 paths) in /_/LibGit2Sharp/Repository.cs:line 1652
     at LibGit2Sharp.Diff.BuildDiffList(ObjectId oldTreeId, ObjectId newTreeId, TreeComparisonHandleRetriever comparisonHandleRetriever, DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 545
     at LibGit2Sharp.Diff.Compare[T](DiffModifiers diffOptions, IEnumerable`1 paths, ExplicitPathsOptions explicitPathsOptions, CompareOptions compareOptions) in /_/LibGit2Sharp/Diff.cs:line 484
     at LibGit2Sharp.Commands.Stage(IRepository repository, IEnumerable`1 paths, StageOptions stageOptions) in /_/LibGit2Sharp/Commands/Stage.cs:line 77
     at LibGit2Sharp.Commands.Stage(IRepository repository, String path) in /_/LibGit2Sharp/Commands/Stage.cs:line 25
bording commented 1 year ago

@arturcic Yes, that is also what I've observed, and I have confirmed that it will be fixed with the native binaries that include https://github.com/libgit2/libgit2/pull/6547.

That's not what I'm asking about in my previous comment though.

Using LibGit2Sharp 0.26.2, using the repro code in this issue:

var repo = new Repository();
repo.Config.Get<bool>("core.autocrlf");

That doesn't appear to have ever worked, and it doesn't work with https://github.com/libgit2/libgit2/pull/6547 included either.

For the scenario where you're passing a repo path to Repository, that is #2031.

zentron commented 1 year ago

@bording , you are probably right. The repro in the original issue was simply to show the minimal way to specifically reproduce that listed exception.

The "real world" usage of the code will presumably work fine once I update to use the https://github.com/libgit2/libgit2/pull/6547 change. Thanks ill keep you posted.

bording commented 1 year ago

@zentron LibGit2Sharp 0.27.2 is out and includes the change, so you can try it now.

zentron commented 1 year ago

Thanks @bording all looks good now after updating (via our fork).

In the actual usage the repository is created with a specific location so it doesn't get the follow-up error noticed in the repro once the NativeBinaries dependency was updated.