Skip to content
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

Clean & Rebuild targets appear to calculate incorrect version when using GeneratePackageOnBuild #1067

Closed
MattKotsenas opened this issue Jul 29, 2024 · 2 comments · Fixed by #1107
Assignees

Comments

@MattKotsenas
Copy link
Member

I'm not 100% sure this is a bug and not user error, but it feels like a bug, so I apologize if I've misunderstood the scenario 😊.

Repro setup

Use a simple project setup like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <RootNamespace>nbgv_rebuild</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsPackable>true</IsPackable>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Nerdbank.GitVersioning" Version="3.6.139">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <Target Name="PrintPackageVersion" AfterTargets="_GetOutputItemsFromPack">
    <!-- Stand in for custom targets that use the package -->
    <Message Text="_OutputPackItems = @(_OutputPackItems)" Importance="High" />
  </Target>

</Project>

In my case I'm inside a git repo, but with no version.json and no commits.

Correct output

If I do a standard build like this:

dotnet clean && dotnet build /tl:off

I get output like this

Build succeeded in 0.5s
  Determining projects to restore...
  All projects are up-to-date for restore.
  nbgv-rebuild -> D:\Projects\nbgv-rebuild\bin\Debug\net8.0\nbgv-rebuild.dll
  _OutputPackItems = D:\Projects\nbgv-rebuild\bin\Debug\nbgv-rebuild.0.0.0-g.nupkg;D:\Projects\nbgv-rebuild\obj\Debug\nbgv-rebuild.0.0.0-g.nuspec
  The package nbgv-rebuild.0.0.0-g is missing a readme. Go to https://aka.ms/nuget/authoring-best-practices/readme to learn why package readmes are important.
  Successfully created package 'D:\Projects\nbgv-rebuild\bin\Debug\nbgv-rebuild.0.0.0-g.nupkg'.

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.24

Note the version of 0.0.0-g. That's expected given the setup.

Incorrect output

If I do a clean (or a Rebuild) like this:

dotnet clean /tl:off

I get output like this:

Build started 7/29/2024 11:58:40 AM.
     1>Project "D:\Projects\nbgv-rebuild\nbgv-rebuild.sln" on node 1 (Clean target(s)).
     1>ValidateSolutionConfiguration:
         Building solution configuration "Debug|Any CPU".
     1>Project "D:\Projects\nbgv-rebuild\nbgv-rebuild.sln" (1) is building "D:\Projects\nbgv-rebuild\nbgv-rebuild.csproj" (2) on node 1 (Clean target(s)).
       CoreClean:
         Deleting file "D:\Projects\nbgv-rebuild\bin\Debug\net8.0\nbgv-rebuild.deps.json".
         Deleting file "D:\Projects\nbgv-rebuild\bin\Debug\net8.0\nbgv-rebuild.dll".
         Deleting file "D:\Projects\nbgv-rebuild\bin\Debug\net8.0\nbgv-rebuild.pdb".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.GeneratedMSBuildEditorConfig.editorconfig".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.AssemblyInfoInputs.cache".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.AssemblyInfo.cs".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.csproj.CoreCompileInputs.cache".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.dll".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\refint\nbgv-rebuild.dll".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\nbgv-rebuild.pdb".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\net8.0\ref\nbgv-rebuild.dll".
       PrintPackageVersion:
         _OutputPackItems = D:\Projects\nbgv-rebuild\bin\Debug\nbgv-rebuild.1.0.0.nupkg;D:\Projects\nbgv-rebuild\obj\Debug\nbgv-rebuild.1.0.0.nuspec
     2>Done Building Project "D:\Projects\nbgv-rebuild\nbgv-rebuild.csproj" (Clean target(s)).
     1>Done Building Project "D:\Projects\nbgv-rebuild\nbgv-rebuild.sln" (Clean target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.51

Note that now the version of the package was 1.0.0 and that the package wasn't cleaned. I'd expect to also see this section in the output:

      _CleanPackageFiles:
         Deleting file "D:\Projects\nbgv-rebuild\bin\Debug\nbgv-rebuild.0.0.0-g.nupkg".
         Deleting file "D:\Projects\nbgv-rebuild\obj\Debug\nbgv-rebuild.0.0.0-g.nuspec".

but I don't.

My (likely incorrect) diagnosis

It seems that nbgv isn't hooking into Clean targets correctly and thus the default and incorrect version is being used.

@MattKotsenas
Copy link
Member Author

I'm happy to pursue a fix here if desired, but wanted to validate that I correctly understand the expected behavior before digging in further.

@AArnott
Copy link
Collaborator

AArnott commented Jul 31, 2024

Thank you for the report and volunteering to fix. I guess you're probably correct. Certainly an interesting case.

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

Successfully merging a pull request may close this issue.

2 participants