0
votes

I have ProjectA and ProjectB

ProjectA

Is a dotnet standard project with output and exe file to be used as as tool. This generates a nuget package on build, using property and property to mark the package as a tool. It also is marked as to auto exclude from projects which installs the nuget package, when they them also generate a nuget package.

<Project Sdk="Microsoft.Net.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <AssemblyName>ProjectA</AssemblyName>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <IsTool>true</IsTool>
    <DevelopmentDependency>true</DevelopmentDependency>
  </PropertyGroup>

   <ItemGroup>
      <None Include="build\ProjectA.props" Pack="True" PackagePath="build" />
      <None Include="build\net461\ProjectA.targets" Pack="True" PackagePath="build" />
    </ItemGroup>
  <PropertyGroup>
</Project>

ProjectA.targets

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <GetPackageVersionDependsOn>MyCustomTask;$(GetPackageVersionDependsOn)</GetPackageVersionDependsOn>
    <MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
  </PropertyGroup>

  <Target Name="MyCustomTask">
    <!-- Idealy i would use a custom task to set PackageVersion to something different. Like 5.0.99-alpha1+102435 -->
    <PropertyGroup>
      <PackageVersion>5.0.99-alpha1+102435</PackageVersion>
    </PropertyGroup>
  </Target>
</Project>

ProjectB

Installs a reference to the nuget package created by ProjectA.

<PropertyGroup>
  <TargetFrameworks>net461</TargetFrameworks>
  <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="ProjectA" Version="1.0.0" PrivateAssets="all" />
</ItemGroup>

When building ProjectB, the following files are genearted in obj/ folder:

  • .csproj.nuget.g.props
  • .csproj.nuget.g.targets

These files include import to ProjectA targets and props from the nuget package cache at: %userprofile%.nuget\projecta\1.0.0\build\ProjectA.targets etc

Msbuild -t:pack ignores these imports when running, and hence the build ProjectB never sets the PackageVersion to 5.0.99-alpha1+102435 as i would expect.

Adding the above content of ProjectA.targets directly into ProjectB.csproj works.

Anyone have any suggestions what I'm missing or if the might be an issue?

1

1 Answers

1
votes

If you use *.targets to pack the nuget project into a nuget package based on that file, you should make this file recognized along with csproj file.

Based on the description, it seems that you used that targets file to pack your nuget project into a version 5.0.99.

So you should make your csproj to work along with that file like this:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>
    
    <!--the path of the *.targets file-->
    <Import Project="xxx\xxx.targets"/>
</Project>

Update 1

clean nuget caches or delete all files under C:\Users\xxx\.nuget\packages) and msbuild -t:pack ProjectB.csproj to check it.

Update 1

Instead, modify Project B.csproj file:

use <TargetFramework>net461</TargetFramework> instead of <TargetFrameworks>net461</TargetFrameworks>.

The multi-platform framework seems to break the performance of the targets file, I'm also curious why this is happening. But this is the tip. You should use that.