5

NuGet version: 4.5.1.4879

When using "project-driven" NuGet package building (i.e., passing the .csproj file to the nuget pack command), I thought I understood how NuGet will determine the version to use in the generated package. Specifically, that it will use AssemblyInformationalVersion if it exists in the AssemblyInfo, or AssemblyVersion if AssemblyInformationalVersion is not present.

That seems to work as expected in some of my assemblies (I have a simple 1:1 relationship between assemblies and packages - 1 assembly produces 1 package of the same name), but not in others.

In a problematic assembly, the AssemblyInfo includes this:

[assembly: AssemblyVersion("2.0.0.0")]
[assembly: AssemblyFileVersion("2.0.0.0")]
[assembly: AssemblyInformationalVersion("2.0.0")]

In the .csproj file I also have this, among other stuff (note the <Version> tag):

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{E233E8E2-EB9E-461F-80C2-63F9AFCC425D}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>XYZ.Core.Messaging</RootNamespace>
    <AssemblyName>XYZ.Core.Messaging</AssemblyName>
    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>

    <Version>2.0.0</Version>

    <NuGetPackageImportStamp>
    </NuGetPackageImportStamp>
    <TargetFrameworkProfile />
  </PropertyGroup>

BTW, I have no .nuspec file.

Despite those settings, when I try to build the package, using this command:

nuget pack XYZ.Core.Messaging.csproj  -OutputDirectory "c:\build\nuget-local" -Suffix "local"

It builds the package as XYZ.Core.Messaging.1.0.0-local.nupkg.

For some other packages I build with a similar command (just a different .csproj file), with the same AssemblyInfo contents, those build the package with version 2.0.0 correctly.

So where could NuGet be getting the 1.0.0 version from?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
E-Riz
  • 31,431
  • 9
  • 97
  • 134
  • See this possible duplicate: https://stackoverflow.com/questions/64602/what-are-differences-between-assemblyversion-assemblyfileversion-and-assemblyin – RBreuer Apr 04 '18 at 18:39
  • 1
    @RBreuer Definitely NOT a duplicate. That info is understood, but as I wrote above, I'm using the AssemblyInformationalVersion - problem is that nuget is ignoring it. – E-Riz Apr 04 '18 at 18:41
  • is the `nuspec` file updated with the `` correcly inside ``? – RBreuer Apr 04 '18 at 18:46
  • @RBreuer No. I just double-checked by unzipping the .nupkg file and examining its .nuspec: `1.0.0-local` – E-Riz Apr 04 '18 at 18:49
  • 1
    Ensure that you pack correct configuration. For example in your csproj Version is set for Debug configuration, so if you pack Release - that will have no effect. Same with assembly attributes. If after you changed them you did not ever manually built project in Release configuration - nuget pack will not see them (if you are packing release). So to bw sure, run nuget pack with -Build and specify configuration explicitly. – Evk Apr 04 '18 at 19:14
  • @Evk You've nailed it. Without specifying the Configuration, nuget was pulling the Debug version. Please make an Answer out of your Comment so I can accept it. – E-Riz Apr 05 '18 at 13:55

1 Answers1

4

Ensure that you pack the correct configuration. You might pack a Release configuration while only setting the Version element in the .csproj section related to the Debug configuration, or visa versa.

With assembly attributes (AssemblyVersion, etc.) - note that they are embedded inside the compiled assembly, so for them to take effect you need to build the project, and do it in a correct configuration. Often it happens that you test your code in the Debug configuration, then change version and build without switching to Release, and then pack the Release configuration.

To avoid all those problems it's good to always build when packing, and always pack an explicit configuration:

nuget pack XYZ.Core.Messaging.csproj  -Build -Properties Configuration=Release ...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Evk
  • 98,527
  • 8
  • 141
  • 191
  • After many frustrating attempts, this solution finally worked for me. My postbuild event now looks like `nuget pack MyProject.csproj -Properties Configuration=Release -IncludeReferencedProjects` and it finally always builds the right version with referenced projects – Joep Geevers Feb 10 '22 at 15:23