Nuget package generated with `dotnet pack` is missing its own files - nuget

N.B. This question is about packaging the project's own files, as opposed to external depencencies. I am not a developer and my guess is that this is so obvious to a developer, it's not documented.
I have a basic web app that is being used to demonstrate a pipeline. The only change I have made is to add <IsPackable>true</IsPackable> to the .csproj file.
Using dotnet pack creates a nupkg file, but it is missing the main website files such as wwwroot and the files therein.
I'm used to using octo pack which references the "debug" build/publish folder as opposed to dotnet pack that uses the .csproj file, therefore I believe my .csproj file is missing something, however, my Googling always yields results about referencing 3rd-party Nuget packages. In this instance, I am not trying to reference external packages, I want it to pack its own files.
Contents of .nupkg file:
octo pack
dotnet pack
Steps to reproduce (assuming you have the required .Net SDK installed).
Download basic web app and extract to C:\Temp\RandomQuotes-master.
Edit C:\Temp\RandomQuotes-master\RandomQuotes\RandomQuotes.csproj
Add a new element under <PropertyGroup>: <IsPackable>true</IsPackable>
cd to C:\Temp\RandomQuotes-master\RandomQuotes
Run dotnet build.
Run dotnet run if you want to check the app works.
Package the app. Run dotnet pack.
Result:
PS C:\Temp\RandomQuotes-master\RandomQuotes> dotnet pack
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
RandomQuotes -> C:\Temp\RandomQuotes-master\RandomQuotes\bin\Debug\netcoreapp3.1\RandomQuotes.dll
RandomQuotes -> C:\Temp\RandomQuotes-master\RandomQuotes\bin\Debug\netcoreapp3.1\RandomQuotes.Views.dll
Successfully created package 'C:\Temp\RandomQuotes-master\RandomQuotes\bin\Debug\RandomQuotes.1.0.0.nupkg'.
View the generated .nupkg file:
If anyone can help, I'd be grateful if you could share your ideas.
T.I.A.

Related

Create a NuGet package with only javascript files

I want to create a NuGet package that contains only javascript. I have no assembly that needs to be built and included.
So to do this I created an empty solution in vs, and created an empty project. In the project I created a Scripts directory with my javascript files. I have a package.nuspec and a packages.config
When I run "nuget pack" commandline I get an error:
C:\MyProject>nuget
pack
Attempting to build package from 'TestProject.csproj'.
MSBuild auto-detection: using msbuild version '16.1.76.45076' from
'C:\Program Files (x86)\Microsoft Visual
Studio\2019\Enterprise\MSBuild\Current\bin'.
Error NU5012: Unable to find 'C:\MyProject\TestProject.exe'. Make sure the project has been built.
C:\MyProject>
If I try to build the project in visual studio, I get an error...
Program does not contain a static 'Main' method suitable for an entry
point.
...which makes sense because I made this from an empty project.
I am not trying to deploy the assembly, just javascript, can I stop it from trying to build somehow?
Don't use a project. Have have your .nuspec and javascript files in the directory, then run nuget pack. If the nuspec doesn't contain a <files> section, then it will just copy the filesystem structure directly into the nupkg.
Having said that, new ASP.NET Core projects from Microsoft's templates no longer use front end resources (html, javascript, css) from NuGet packages, but rather use either LibMan or npm, to be more in-line with what the non-.NET front-end ecosystem uses. I suggest investigating those technologies and using them if possible.

Error NU5012 - nuget pack unable to find path (/bin/release/MyProject/bin/release)

I'm trying to update my nuget package by running the command nuget pack -properties Configuration=Release but this gives me the following error:
Unable to find 'bin/Release/{project-name}/bin/Release'. Mae sure the project has been built
I'm not quite sure why it goes to my Release folder and then continues to go down to another Release folder as this doesn't exist? I'm quite lost here, and I'm not sure what to do.
TL;DR
For the new <Project Sdk="Microsoft.NET.Sdk"> .csproj file format, use dotnet pack to build NuGet packages, even if they target .Net Framework or if the project is multi-targeted. Run this from the directory containing your .csproj (and optionally a .nuspec)
dotnet pack MyProject.csproj -c Release
Note that by default dotnet pack places the output .nupkg in the /bin/Release folder, which is different from the current default folder where older nuget pack placed it.
I would suggest you don't create explicit .nuspec files for the new .csproj formats, since most settings can be set in the "Packages" tab of the new Project format. However, if you do have a .nuspec, then dotnet pack will also combine any .nuspec file matching the project name, allowing for the $ symbol substitution from the version and metadata set in your .csproj.
More Detail
I also experienced this problem - in previous versions, I typically use to use this approach to pack against a .csproj with the .nuspec file of the same name, so that I can use all of the $ tokens like $id$, $version$.
However when attempting this against the newer <Project Sdk="Microsoft.NET.Sdk"> csproj formats:
nuget pack {MyProject}.csproj -Prop Configuration=Release
I received the same error (note the "duplication" of bin\release):
Error NU5012: Unable to find 'bin\Release{MyProject}\bin\Release\'. Make sure the project has been built.
And if I tried packing against a .nuspec containing symbols (NuGet Version: 4.7.1.5393)
nuget pack {MyProject}.nuspec -Prop Configuration=Release
I just get the unhelpful message because nuget isn't able to resolve the $ tokens automatically.
Value cannot be null or an empty string.
Parameter name: value
However, by using dotnet pack instead of nuget
dotnet pack MyProject.csproj -c Release
I get to where I want to be, i.e. a multi-targeted NuGet package with the $ symbols in the .nuspec correctly synchronized from the .csproj (you can verify this by opening up the .nupkg file in e.g. 7zip and examining the final .nuspec file in the package root)
I had the same problem on a multi-targeted project (targetting both net452 and netstandard2.0).
My fix/hack was to add 'TargetFramework=net452' to the list of properties passed to Nuget, and manually adding the netstandard2.0-output to the files-section in the .nuspec-file
Hope someone finds a better solution
Part of Rune's answer (setting TargetFramework=net471 NuGet property)
and using NuGet version 4.9.1 (instruction here: https://stackoverflow.com/a/53614798/1813219)
was a way to go for me.
Actually I just solved the problem on my end. Nuget may not find hidden dll. Here is my solution:
Build the project;
Show all files in visual studio so you can see the hidden files, so that you can see the dll under bin folder;
Right click, open folder in file explorer, then copy it to current folder let them replace/ Or unhidden the files;
Do "Nuget Pack" again.

VSTS build error: non-whitespace before first tag

VSTS Build definition is failing and I can't make heads or tails of this error...
C:\Windows\system32\chcp.com 65001
Active code page: 65001
Detected NuGet version 3.3.0.212 / 3.3.0
SYSTEMVSSCONNECTION exists true
Error: Non-whitespace before first tag.
Nuget Installer build step, Visual Studio Build build step
Are the build steps out of order? Am I not referencing the right file on the Nuget Installer step? The Nuget Installer calls for a nuget.config xml file but my .Net Core app houses its dependencies in a project.json file. Please let me know if I need to provide more information, or what's missing from the post that would make this easier to diagnose.
For .Net Core app, using dotnet-restore command to restore package. The dotnet restore command uses NuGet to restore dependencies as well as project-specific tools that are specified in the project.json file.
Add Command Line step to your build definition. (Tool: dotnet, Argument: restore, Working folder: [folder path, which contains the project.json file])
There is an article that can help you.

Native binaries in NuGet package installed into DNX project

I've created NuGet package containing native x86 binaries. Binaries are copying to bin directory by MSBuild script from this answer.
But this approach does not work with DNX projects (ASP.NET 5 web app for example), because MSBuild script is not installing. So I have FileNotFoundException.
How to make it alive?
Solution for me was to make runtimes\win\native directory in NuGet package structure, as described here: https://docs.nuget.org/Create/uwp-create
In runtime DNX adds this directory to PATH variable, so managed dll can load it.

Nuget package restore is not working on TFS build server

I followed this blog post and I expected Nuget to just work. It obviously has not. I get errors on all of my third party dll's that I expect NuGet to update automatically:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\
Microsoft.WebApplication.targets (182): Could not copy the file "bin\AutoMapper.dll"
because it was not found.
I have set everything up correctly and I have ensured that the solution has Package restore enabled. If I delete a package locally and build, NuGet restores that package for me.
I am not sure why it is not fuctioning on the build server though.
Any Suggestions?
Try to add NugetRestore Activity to the TFS workfolw
Note:
For Team Foundation Build 2013 on-premises and Visual Studio Team Services (formerly Team Foundation Service), the default Build Process Templates already implement the NuGet Package Restore workflow without any special configuration.
I came across a similar error for log4net nuget package and I solved it in a different way. May be this will be useful to someone some day.
The error from TFS Team Build 2015
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\
Microsoft.WebApplication.targets (182): Could not copy the file "bin\Log4net.dll"
because it was not found.
Cause
The path to the nuget package in the .csproj file for referencing the package in the project cannot be found
Resolution
Edit the .csproj file of the project that has the error.
Look for the node ItemGroupin the .csproj file
You will see the HintPath pointing to a folder, most like the folder contain the name of solution as where it will file the DLL.
Replace the HintPath with <HintPath>..\..\Assemblies\3rd party\log4net\1.2.10.0\2.0\log4net.dll</HintPath>
You nuget package library may not be log4net but a different library. A very easy way would be to be edit the .csproj file of a project in the solution that contains the same nuget package library, copy the reference of the nuget in the ItemGroup and replace in the project that has the error. The reference should start contain ...\Assemblies\3rd party\ ... instead of a physical file location on your machine