alirezanet / Husky.Net

Git hooks made easy with Husky.Net internal task runner! 🐶 It brings the dev-dependency concept to the .NET world!
https://alirezanet.github.io/Husky.Net/
MIT License
632 stars 29 forks source link

Husky throws an ArgumentException when I try to commit through Sourcetree #96

Closed AntonisManiatis closed 7 months ago

AntonisManiatis commented 7 months ago

Version

0.6.2

Details

I am not so sure if this should be opened as a bug or a discussion would be a better fit so apologies in advance but basically whenever I try to commit changes through Sourcetree Husky throws an ArgumentException.

husky-exception

Here's the stack trace:

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: HOME
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)
   at CliFx.Utils.Extensions.CollectionExtensions.ToDictionary[TKey,TValue](IDictionary dictionary, IEqualityComparer`1 comparer) in /_/CliFx/Utils/Extensions/CollectionExtensions.cs:line 38
   at CliFx.CliApplication.RunAsync(IReadOnlyList`1 commandLineArguments) in /_/CliFx/CliApplication.cs:line 216
   at Program.<Main>$(String[] args) in /home/runner/work/Husky.Net/Husky.Net/src/Husky/Program.cs:line 31
   at Program.<Main>(String[] args)

It works as expected if I try to commit either from the CLI, VS or VSCode but not on Sourcetree. I had a very quick look at the source code to try and figure out what could be happening but couldn't figure anything out immediately. Any idea what that Key: HOME might be or where it comes from? is it an environment variable? I'd appreciate any feedback on this, we'd really like to use your tool but without having to sacrificing Sourcetree in the process 😄. Thanks!

OS: Windows 11 Pro 22621.2428 Sourcetree version: 3.4.15

Steps to reproduce

alirezanet commented 7 months ago

Hi @AntonisManiatis, I think for some reason sourcetree is sending additional arguments to the hooks! (I never used this app before so I need to research what it is doing) can you share your git and credential manager versions?

image

Also, please check your Environment variables, This might imply you have two identical environment variables (on Windows that also ignores casing)

AntonisManiatis commented 7 months ago

Hi @alirezanet,

Here's my git & git credential-manager versions:

image

I checked my environment variables and nothing seems out of the ordinary, no duplicates etc. Also a colleague of mine tried on his machine (again windows 11 & latest Sourcetree) and it's the same story.

I wrote a little program to print command line arguments and environment variables to standard out, changed the pre-commit hook to call that program instead of doing dotnet husky run --group pre-commit , commited a change through Sourcetree and I didn't find any duplicate env variables printed.

alirezanet commented 7 months ago

Hi @AntonisManiatis, I found the problem, for some reason, Sourcetree is adding a lower-case home environment variable and that is causing the issue, I'm working on a fix for this issue here... if you want to also test it you can change your pre-commit hook to log the environment variables this way if you want:

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

# Log all environment variables
printenv

dotnet husky run -v --group "pre-commit"

more info: https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables

alirezanet commented 7 months ago

Hi @AntonisManiatis, This is fixed in v0.6.3.

AntonisManiatis commented 7 months ago

thank you very much @alirezanet, cheers!