Skip to content

Guide for automating installation causes unnecessary work to be done when applied to multiple projects #155

@baronfel

Description

@baronfel

Details

The guide for automating Husky installs has users add a per-project Target to ensure the tool is restored and installed:

<Target Name="husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0">
   <Exec Command="dotnet tool restore"  StandardOutputImportance="Low" StandardErrorImportance="High"/>
   <Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High"
         WorkingDirectory="../../" />  <!--Update this to the relative path to your project root dir -->
</Target>

This means that if multiple projects add this target, the same logic will be run multiple times over the course of a build. In general, for actions that should happen once over a logical build, we encourage a couple patterns:

Use Directory.Solution.targets

A Directory.Solution.targets file is an MSBuild logic file that is loaded implicitly by a solution when the solution is used for an MSbuild command (e.g. dotnet build mysln.sln). Since the solution is the top-level orchestrator, if you added the same husky target to this file, it would only execute once, shortening your overall build cycles.

Improve the incrementality of the Target

Right now your Target executes on every build (that doesn't explicitly add -no-restore. This means that husky will drastically impact the performance of even very-incremental builds - slowing down the developer's inner loop, impacting VS builds, and so on. I strongly suggest that

  • some kind of marker file be created - either by husky install itself, or by the target that you're asking users to copy
  • that marker file be added to the FileWrites Item group, so that dotnet clean will remove it
  • that marker file be added as an Output to the husky Target
  • the tool configuration for the project be added as an Input to the husky Target

Doing this will allow MSBuild to see if your target has been run before or not, and only re-run it when

  • the input files change
  • the output file is out of date with the input (or doesn't exist at all)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions