Using 'husky attach' inside multi-project solutions leads to compile errors due to 'husky install' running multiple times
See original GitHub issueVersion
v0.4.4
Details
After you run dotnet husky attach someproject.csproj, and doing so on multiple projects inside a solution to ensure people working on individual projects are forced to use Husky (in my case: formatting in pre-commit hook), leads to the following error:
The process cannot access the file 'D:\Projects\MyProject\.husky\_\.gitignore' because it is being used by another process.
error MSB3073: The command "dotnet husky install" exited with code 1.
It is clear that the error is caused by the parallel build feature of MSBuild, as each project in a solution is being build. I have not yet found a stable, different way of ensuring the dotnet husky install is run only once.
Hand-crafted ways that probably work is do a file-exists test of the .husky folder. But by itself, that is not enough, as Husky won’t run unless core.hooksPath of Git is set to .husky, which this command so conveniently does.
Ideally, dotnet husky install should not lock files it reads, or use a mutex of sorts (available cross-platform in dotnet nowadays) to prevent it from running multiple times.
Alternative, without using exclusive locks, dotnet husky install could just check if Husky is already installed and of the correct version. But that still leaves the issue that, if it is not installed yet, that “Rebuild All” will have multiple parallel dotnet husky install running.
Steps to reproduce
- Create a solution with 2 or more projects (more is better)
- Run
dotnet husky attachfor each project - Clean project, then Build All.
You will now get the above mentioned error.
PS: I really like Husky.Net. The above is just a minor inconvenience. After trying many different ways of creating cross-platform auto-installed pre-commit hooks for Git, basically nothing worked out of the box, but Husky did. It is awesome!!!
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Related StackOverflow Question
Thanks for sharing your configuration, something like this could be an additional advanced feature to the
husky attachcommand, I’ll be happy if you test this and share your final thoughts on another issue that you think is necessary for others facing a similar problem.Yeah 😉, there are a lot of Alireza in Iran, but firoozja is originally from the city I’m living now. 😃 we have at least one thing in common 😅, plus I also like chess but my rating is half of his.
Have a nice day
To answer my own question, it turns out that
Target Batchingworks. With your mutex (which prevents problems when run in parallel) and theOutputsvsInputsbehavior (where MSbuild diligently tests whether source has been updated), this indeed prevents the execution of the tasks.Note that the current code still prevents unnecessary running of
dotnet husky installif it is already installed. But tbh, the overhead of this is much less now that MSBuild takes care of dirty-flagging the installation (it only runs when the version is updated).There’s now probably some code that’s redundant and hopefully a smaller version is possible. I’ll soon test with your mutexed version. For posterity, this is the current version (paths may need to be adjusted if people try to use this, i.e. like the path to
dotnet-tools.json):