Thanks to visit codestin.com
Credit goes to github.com

Skip to content

WorktreeCollection.Add throws exception on NET6 Preview 4 (0.27.0-preview-0096) #1885

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

Closed
DvdKhl opened this issue May 29, 2021 · 3 comments · Fixed by #1890
Closed

WorktreeCollection.Add throws exception on NET6 Preview 4 (0.27.0-preview-0096) #1885

DvdKhl opened this issue May 29, 2021 · 3 comments · Fixed by #1890

Comments

@DvdKhl
Copy link

DvdKhl commented May 29, 2021

Reproduction steps

  • Download and open attached Project
  • Set target framework to net6 and run the project

Expected behavior

Worktrees.Add executes without exception

Actual behavior

Throws "LibGit2Sharp.LibGit2SharpException: reference is not a branch".

Version of LibGit2Sharp (release number or SHA1)

0.27.0-preview-0096

Operating system(s) tested; .NET runtime tested

Windows 10 Version 2004 (OS Build 19041.985) x64

using LibGit2Sharp;
using System;
using System.IO;
using System.Linq;
namespace LibGit2SharpWorktreeIssue {
  class Program {
    static void Main(string[] args) {
      var mainRepoPath = Path.Combine("..", "RepoTest", "Main");
      using(var repo = new Repository(mainRepoPath)) {
        try {
          var branchName = "r" + DateTimeOffset.Now.ToUnixTimeSeconds();
          var workTreePath = Path.GetFullPath(Path.Combine(mainRepoPath, "..", branchName));
          //While debugging retry this line. It often works the second time.
          var newWorkTree = repo.Worktrees.Add(branchName, workTreePath, false);
        } catch(Exception ex) {
          Console.WriteLine(ex);
        }
      }
    }
  }
}

LibGit2SharpWorktreeIssue.zip

@zentron
Copy link
Contributor

zentron commented Jul 1, 2021

This same problem is appearing for me after the .NET upgrade was automatically pushed out for release 5.0.7 (SDK 5.0.301, Runtime 5.0.7)

I believe the below previously worked with 5.0.6 release (SDK 5.0.300, Runtime 5.0.6)

Expected behavior

Worktrees.Add executes without exception

Actual behavior

Differs between Release mode and Debug mode.
When running the app in Release mode the exception is

Throws "LibGit2Sharp.LibGit2SharpException: reference is not a branch".

When running in Debug mode the exception is

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt

This sounds like a few existing issues logged recently so one of my suspicions was something to do with how calls are being marshalled to unmanaged code in the new runtime with dlls from an older sdk, but pulling down the LibGit2Sharp repo and rebuilding results in the same error.

Version of LibGit2Sharp (release number or SHA1)
0.27.0-preview-0096
0.27.0-preview-0100

Reproduction Steps

Same principal as above, but have included a empty repo to pull down to make reproducing a little bit easier. Included as LinqPad script

<Query Kind="Program">
  <NuGetReference Version="0.27.0-preview-0100" Prerelease="true">LibGit2Sharp</NuGetReference>
  <Namespace>LibGit2Sharp</Namespace>
  <RuntimeVersion>5.0</RuntimeVersion>
</Query>

void Main()
{
	var basePath = @"C:\Temp\Something\"+ Guid.NewGuid().ToString().Substring(0,5);
	Directory.CreateDirectory(basePath);
	//var rootWorktree = Path.Combine(basePath, "master");
	var worktreeName = "random-tree";	
	Repository.Clone("https://github.com/zentron/libg2sharp-bugtest", basePath, new CloneOptions { IsBare = false, RecurseSubmodules = false, BranchName = "main" });


	using (var repo = new Repository(basePath))
	{	
		var workingDirectory = new DirectoryInfo(Path.Combine(basePath, worktreeName));	
		var remoteBranchName = $"origin/second-branch";
		repo.Worktrees.Add(remoteBranchName, worktreeName, workingDirectory.FullName, false);
		Console.Write($"Branch in {workingDirectory.FullName}");
	}
}

Interestingly when I run the above code in LinqPad and toggle between .NET 5 (3.0.7) and .NET 3.1 (3.1.6) it works in the older runtime. If I run the repo.Worktrees.Add in a try catch loop, it also tends to work on the second try.

@zentron
Copy link
Contributor

zentron commented Jul 1, 2021

Located fix and added a PR 👇 @bording Let me know if there is anything else I can help with to get this merged and published.

@bording
Copy link
Member

bording commented Jul 3, 2021

@zentron Thanks for the PR. I suspect there might more of these lurking around now that .NET seems to be caring about them.

0.27.0-preview-0102 includes the fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants