C#C
C#3y ago
Byron

✅ dotnet Restore and Targets in NuGet packages not found until second build

Hey folks,

I have a NoTargets project file which contains logic for running our Ci/Cd. This file has a series of functions in there for build automation and running locally. It has logic for using the NuGet package GitVersion which is pulled in as a project reference.

<Project Sdk="Microsoft.Build.NoTargets/3.7.0">
 <ItemGroup>
    <PackageReference Include="GitVersion.MsBuild" Version="5.10.3"/>
 </ItemGroup>

Then my custom target has a dependency on a target defined within that package called GetVersion https://github.com/GitTools/GitVersion/blob/main/src/GitVersion.MsBuild/msbuild/tools/GitVersion.MsBuild.targets#L24. The target looks like this

<Target Name="__GenerateSharedGitAssemblyInfo" DependsOnTargets="WriteGitShaHash;GetVersion">
<!-- other stuff -->
</Target>

The solution I am working with has 50+ projects which all need to be versioned so instead of running gitversion per assembly, I run it once and cache it with incremental building by sharing a single generated C# class. Each project calls into the build script using the MSBuild method to make sure the shared class in generated. It looks like this

<MSBuild Projects="$(MSBuildThisFileDirectory)Build.targets" Targets="Restore;GenerateSharedGitAssemblyInfo"/>


The problem is that Restore will restore the packages for Build.targets however the targets defined in NuGet packages are not loaded into the current context, so they fail to resolve. The user has to build twice. Is there a simple way to force them to be resolved?
GitHub
From git log to SemVer in no time. Contribute to GitTools/GitVersion development by creating an account on GitHub.
Was this page helpful?