7

I have several projects that I'm migrating from .NET Framework 4.7 to .NET Standard 2.0. As a result, I'm trying to use the dotnet pack command to create my NuGet package while using my nuspec file with tokens. I have several custom build scripts that generate the version number for me. My generated files are not apart of version control but nuspec file is hence why I'd like to try and find a way to get the tokens to work, otherwise I'm stuck writing new scripts.

The first thing I did was to set the GenerateAssemblyInfo to false in the project files. In my most simplest project, the csproj file looks like this:

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

  <PropertyGroup>
     <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
     <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

</Project>

I then have an AssemblyInfo.cs that I generate as part of my build process. The file looks like this before I compile the project:

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyCompany("Company Name")]
[assembly: AssemblyCopyright("Copyright © 2019")]

#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif

[assembly: AssemblyTitle("The Title")]
[assembly: AssemblyDescription("The Description")]
[assembly: AssemblyProduct("The Product")]
[assembly: ComVisible(false)]
[assembly: Guid("48d6ef54-a8fb-4876-8a9a-2fb8e8cd27e7")]
[assembly: AssemblyVersion("6.0.0.0")]
[assembly: AssemblyFileVersion("6.0.0.0")]

I then have a .nuspec file that I use to create my NuGet package which looks like this:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>$author$</authors>
    <owners>My name</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>$description$</description>
    <releaseNotes>My Release Notes</releaseNotes>
    <copyright>Copyright 2019</copyright>
    <projectUrl>http://url.local</projectUrl>
  </metadata>
</package>

After much reading (not a fan of who ever wrote the new docs for .NET Core) I believe this is the command I need to run in order to use my nuspec file: dotnet pack SubFolder\MyProject.Model.csproj /p:NuspecFile=MyProject.Model.nuspec /p:NuspecBasePath=nuget

The result is this error:

NuGet.Build.Tasks.Pack.targets(202,5): error : An error occured while trying to parse the value '' of property 'version' in the manifest file. [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Value cannot be null or an empty string. [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

NuGet.Build.Tasks.Pack.targets(202,5): error : Parameter name: value [F:\Git\MyProject\Model\MyProject.Model\MyProject.Model.csproj]

Based on the output, it's clear that the underlying targets file isn't grabbing the values out of my AssemblyInfo.cs but rather is trying to grab the values directly out of the csproj file instead. So I tried to simplify the process and ran this command: dotnet pack MyProject.Model\MyProject.Model.csproj which created my NuGet package, but did not have the metadata in my nuspec or in my AssemblyInfo.cs file.

I've read a lot of open issues on GitHub from both the dotnet-cli and NuGet projects that seem to be similar to what I'm experiencing, but none of them have given me a solution that works. Does anyone else think this is a bug or am I missing something?

tj-cappelletti
  • 1,774
  • 12
  • 19
  • You can generate NuGet packages from .Net standard libraries right from their properties -> package -> Generate NuGet package on build. – fstam Feb 07 '19 at 15:28
  • That won't integrate into my build process. I have custom scripts that generates the version for me and to a pattern that makes sense for my assemblies. Using the Visual Studio option won't work as my CI/CD process doesn't use VS and the version number generated by VS doesn't fit with my pattern. – tj-cappelletti Feb 07 '19 at 15:34
  • Can you show an example nuspec file? I mean with $id$ filled in. I imagine version can't be parsed because it's not a dot net version and maybe there is an empty string in there somewhere. – fstam Feb 08 '19 at 07:13
  • I assure you that is not the problem. This process was working fine using .NET Framework 4.7 and using the `nuget` CLI. Only two things have since changed; the target framework is .NET Standard 2.0 and I'm using `dotnet` CLI to pack the project. – tj-cappelletti Feb 08 '19 at 13:38
  • I understand, I can't reproduce. That's why I'm asking for a reproducable case. – fstam Feb 08 '19 at 14:15
  • 1
    So you created a a C# project with the project file, assembly info and the nuspec file I posted here and ran the pack command using the `dotnet` command and got a package with the correct metadata? – tj-cappelletti Feb 08 '19 at 14:18

1 Answers1

9

I had same problem and solved it by adding <NuspecProperties>to csproj as suggested here: https://github.com/NuGet/Home/issues/6636

<NuspecFile>package.nuspec</NuspecFile>
<NuspecProperties>$(NuspecProperties);configuration=$(Configuration)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);version=$(Version)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);id=$(PackageId)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);author=$(Authors)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageProjectUrl=$(PackageProjectUrl)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Description=$(Description)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageReleaseNotes=$(PackageReleaseNotes)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);Copyright=$(Copyright)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);PackageTags=$(PackageTags)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryType=$(RepositoryType)</NuspecProperties>
<NuspecProperties>$(NuspecProperties);RepositoryUrl=$(RepositoryUrl)</NuspecProperties>
Miq
  • 3,931
  • 2
  • 18
  • 32