1

I'm following along this official doc about creating a nuget package with nuget.exe. I'm trying to create a nuspec file out of a Visual Studio project.

According to the docs, when you run

> nuget spec

From the directory where your .csproj file resides, it creates a tokenized .nuspec file. Then, when you run nuget pack <project-name>.csproj the values from the project file are used to replace the tokens in the nuspec file.

So in my example, I have a project called Logger1. After running nuget spec, I have the following .nuspec

<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <title>$title$</title>
    <authors>Yoav Klein</authors>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <license type="expression">MIT</license>
...

Now, this doc says that:

To use these tokens, run nuget pack with the project file rather than just the .nuspec. For example, when using the following command, the $id$ and $version$ tokens in a .nuspec file are replaced with the project's AssemblyName and AssemblyVersion values:

nuget pack MyProject.csproj

My Assembly version is:

# AssemblyInfo.cs

// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.6.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

Now I run nuget pack Logger1.csproj and I expect the version of the package to be drawn from the AssemblyVersion of the project, as promised by the docs.

After creating the package, I examine the .nuspec file in the package created:

<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
  <metadata>
    <id>Logger1</id>
    <version>1.0.0</version>

As you can see, for some reason, it doesn't take the correct value of the AssemblyVersion which is 1.6.0, but rather always uses the value 1.0.0.

Can someone tell me what am I doing wrong?

NuGet Version: 5.8.1.7021


Just to confirm, after building the dll, I can see that the assembly version is 1.6.0:

PS> [Reflection.AssemblyName]::GetAssemblyName("$(pwd)\bin\Debug\Logger1.dll")

Version        Name
-------        ----
1.6.0.0        Logger1
YoavKlein
  • 2,005
  • 9
  • 38
  • I’m not sure, but whether you know the differences among `AssemblyVersion`, `File version` and `Package version`? Actually, if you want to confirm whether the `AssemblyVersion` has changed, you can install this packed NuGet package to a project, and build the project. The project will generate a `.dll` file which is related to this NuGet package, and usually it stores under `..\bin\Debug`. You can open PowerShell and enter command like `[Reflection.AssemblyName]::GetAssemblyName("full path to this dll file").Version` and you can see the `AssemblyVersion`. – Tianyu Mar 18 '22 at 07:16
  • But perhaps you just want to change the [File Version/Package Version](https://stackoverflow.com/questions/64602/what-are-differences-between-assemblyversion-assemblyfileversion-and-assemblyin)? I guess :D – Tianyu Mar 18 '22 at 07:17
  • @Tianyu, see the edit – YoavKlein Mar 18 '22 at 08:25
  • I’m a little confused, I thought you want to change the assembly version but it seems not what you want. Do you mean you want to change `1.0.0`? And to confirm, where did you see this `1.0.0`? – Tianyu Mar 18 '22 at 08:57
  • @Tianyu, take a look at the edit again. Hope it's clear enough this time – YoavKlein Mar 18 '22 at 11:42
  • Thanks for clarifying, may I ask where did you find the created .nuspec file when you were trying to examine it? Is it the same `.nuspec` file that created by `nuget spec` and modified by `nuget pack MyProject.csproj` command? As, tested on my side, after running `pack` command, I didn’t see that my .nuspec file was changed(they are still `$id$`, `$version$`). – Tianyu Mar 21 '22 at 08:08
  • @Tianyu, the original nuspec file should not change. The .nuspec file I'm talking about is the one that's contained in the created .nupkg. – YoavKlein Mar 24 '22 at 17:18
  • I got it, see my answer below. – Tianyu Mar 25 '22 at 07:40

1 Answers1

2

This issue looks quite like a potential issue.

I did more tests and, after analyzing, I saw that, during the packing process, always, there was an error message like XXXXX is required. I confirmed several times, and referred to the official documents, which mentioned that the tokens will be replaced with the value which set in my AssemblyInfo.cs file.

For example, if I set [assembly: AssemblyCompany("Company")], as promised, the $author$ token should be replaced with "Company" but it wasn’t replaced, instead it reported Authors is required error, which looks like nuget.exe didn’t check my AssemblyInfo.cs file at all.

I then found this thread: NuGet again throwing exceptions "authors is required"… ignoring csproj/nuspec replacement tokens. And I saw one workaround/solution > name the downloaded nuget.exe file to nuget.exe file and right-click the file > property > check the Unblock option beside This file came from another computer and might be blocked to help protect this computer. message. And it seems it’s the solution and let nuget.exe work normally to find and check AssemblyInfo.cs file then replace the tokens.

In short(whole solution)

Download nuget.exe from official website and make sure that its name is nuget.exe. Then right-click the nuget.exe file and choose Properties. Switch to General tab, check the Unblock option. Go to your project folder, clear the project cache first(delete bin, obj folders, close your solution in VS and delete the hidden .vs folder, remove the .nuspec file and .nupkg file), and then use nuget.exe spec in command prompt to generate a new .nuspec file, edit it to what you want. After that run nuget.exe pack yourproject.csproj command line to pack your NuGet package.

enter image description here

In short(reason)

In my opinion, the downloaded nuget.exe file is blocked.

Tianyu
  • 895
  • 3
  • 7