Packing NuGet projects compiled in release mode? - nuget

Is there some way to make a NuGet package using code compiled in release mode? Or is there some reason I should only publish (make available locally, in this case) packages compiled in debug mode?
Every time I call nuget pack from my project directory, where I have the nuspec file below, on code I have only compiled in release mode, it complains about not finding the DLL in the debug folder ("\bin\Debug\SomeProject.dll"). If I compile it in debug mode, those files are there and it packs them up as it should.
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<authors>$author$</authors>
<owners>$author$</owners>
<iconUrl>http://somewhere/project.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
</metadata>
</package>

You can solve it like this:
NuGet.exe pack Foo.csproj -Prop Configuration=Release
(reference)

If you are using a post-build event and you want to create a package whether using Debug or Release configuration you can setup the post-build event commandline like so:
"<path to nuget tools>\NuGet.exe" pack "$(ProjectPath)" -Prop Configuration=$(ConfigurationName)

To have NuGet automatically use Release mode when you run nuget pack, do the following:
Open your .csproj file in a text editor.
Find the following line:
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
In this line, replace Debug with Release.
Save changes.

The answers here are good, but I was having a lot of problems with this for a .NET Standard project. I had a project that was only going to publish Release binaries, but it wasn't respecting my default build output path.
I added this to my CSProj which then enabled me to use the accepted answer here.
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
<OutputPath>$(SolutionDir)bin\$(PlatformTarget)\Release</OutputPath>
</PropertyGroup>

Chiming in here.
My build profile would build the DLLs to bin\<arch>\Debug|Release.
I was able to point to my folders by running the nuget command as follows:
Notice how I used the -p option.
PS > nuget pack -p Configuration="x64\Release"
Attempting to build package from ...
...
Found packages.config. Using packages listed as dependencies
...
- Add a dependency group for .NETFramework4.7.2 to the nuspec
Successfully created package...

Related

Nuget Packages Not Restoring

I recently migrated my projects from using PackageReferences to a package.config file. For awhile I've been able to call nuget.exe restore ...\Solution.sln and everything has looked fine. Now when I build I appear to be missing references to my packages used in my package.config files.
I removed all of the 'PackageReferences' from the .csproj file and added a 'packages.config' to each project that look like the following:
Ex 1:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net31"/>
</packages>
Ex 2:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="AspNetCore.HealthChecks.SqlServer" version="3.2.0" targetFramework="net31"/>
<package id="Newtonsoft.Json" version="13.0.0" targetFramework="net31"/>
<package id="System.ServiceModel.Security" version="4.9.0" targetFramework="net31"/>
</packages>
I have Automatically check for missing packages during build in Visual Studio checked in the NuGet Package Manager options of Visual Studio. I am using Visual Studio 2019 16.11.15 and NuGet 6.2.0.
Going to package manager and putting in update-package -reinstall outputs No package updates are available from the current package source for project 'ProjectName'.
If I right-click a packages.config file and select Migrate packages.config to PackageReference... I get an Operation Failed error. Is something wrong with my packages.config files? Is there anything else I can check?
Edit: I have attempted to delete all of my .vs, obj, and bin files. Also my csproj files begin with either <Project Sdk="Microsoft.NET.Sdk"> or <Project Sdk="Microsoft.NET.Sdk.Web">
We can only see migrate from packages.config to PackageReference in offical documatation because PackageReference is something newer. Reverse migration is not a good option.
You said you want to use packages.config with nuget restore so all dependencies can always be downloaded/updated after a fresh pull. You can refer to this page, it gives us many methods to restore packages.
For example, you can use “msbuild -t:restore” in PackageReference to restore packages. See this.

Nuget Pack Failure - The process cannot access the file 'D:\a\1\a\*.nupkg'

Error on Azure Pipeline for NuGet Pack task, using a SDK format .csproj, which autogenerates the .nuspec file:
The process cannot access the file 'D:\a\1\a\*.nupkg' because it is being used by another process.
System.IO.IOException: The process cannot access the file 'D:\a\1\a\*.nupkg' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalDelete(String path, Boolean checkHost)
at NuGet.Commands.PackCommandRunner.BuildPackage(PackageBuilder builder, String outputPath, Boolean symbolsPackage)
at NuGet.Commands.PackCommandRunner.BuildFromProjectFile(String path)
at NuGet.CommandLine.PackCommand.ExecuteCommand()
at NuGet.CommandLine.Command.ExecuteCommandAsync()
at NuGet.CommandLine.Command.Execute()
at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args))
##[error]An error occurred while trying to pack the files.
The .csproj file being built, uses TargetsForTfmSpecificBuildOutput:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Provides a .....</Description>
</PropertyGroup>
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
<ItemGroup>
<!-- Filter out unnecessary files -->
<_ReferenceCopyLocalPaths Include="#(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" />
</ItemGroup>
<!-- Print batches for debug purposes -->
<Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = #(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'#(_ReferenceCopyLocalPaths)' != ''" />
<ItemGroup>
<!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
<BuildOutputInPackage Include="#(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)" />
</ItemGroup>
</Target>
The above modification to the .csproj file is needed due to legacy dll's being required to be built which can't be packaged up on their own. But is based on this answer: https://stackoverflow.com/a/59893520/1231374
Note: Removing the custom package steps still causes the error.
There is an additional error before this, not sure if this could be related.
Error NU5128: Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other location. Consult the list of actions below:
- Add a dependency group for .NETStandard2.0 to the nuspec
See the task configuration below:
See the Nuget installer task, which is the first task the installer runs:
I finialy found a "reason" to this problem.
I can't use dotnet cli on my side because my project is not compatible, but I find a workaround.
It seems related to that https://github.com/NuGet/Home/issues/8713, so I used the "Nuget Tool Installer" to force version "5.2.x" and it just works as expected.
I don't understand why this problem is present since 5 minor versions !
The resolution for my problem was to use dotnet pack (as I am working with .NetStandard and .NetCore projects) instead of nuget pack.
In particular to enable the Do not build option. As a pervious step builds the solution and projects within it.

nuget pack ignores package.config dependencies

I cannot get nuget pack X.csproj to recognize package dependencies in a project. Amazingly, when packaging, the diagnostic message “Found packages.config. Using packages listed as dependencies” is printed, but in the end the <dependencies/> tag in the .nuspec file inside the package is empty.
The packages.config for the project does indeed contain references:
<packages>
<package id="SmartAction.Logger" version="1.0.2.0" targetFramework="net40" />
<package id="SmartAction.Pervasive" version="1.0.1.0" targetFramework="net40" />
</packages>
To narrow the problem down, I removed my own parallel .nuspec file, and mostly all switches from the nuget pack command:
> nuget pack libToneDetection.csproj -prop Configuration=Release
MSBuild auto-detection: using msbuild version '14.0' from 'C:\Program Files (x86)\MSBuild\14.0\bin'.
Attempting to build package from 'libToneDetection.csproj'.
Packing files from '[snip]\Core\ToneDetection\libToneDetection\bin\Release'.
Found packages.config. Using packages listed as dependencies
Successfully created package '[snip]\Core\ToneDetection\libToneDetection\SmartAction.Audio.ToneDetection.1.0.0.0.nupkg'.
NuGet Version: 3.3.0.212
The only difference I can spot with this project is that its name is different from package name (I am trying to maintain them in sync but this is older stuff I am repackaging).
I doubt I had ever seen this before. I am finding questions on SO from people trying to prevent references in packages.config from becoming dependencies of the package, but none from those trying, like me, to beat the reverse problem. Help!
Addendum. I copied the project out of the solution with other projects to a temporary directory and rebuilt the package from there. Now one of the two dependencies from packages.config was added to the package:
<dependencies>
<dependency id="SmartAction.Logger" version="1.0.2.0" />
</dependencies>
Thinking of the differences between the two, the SmartAction.Logger package depends on SmartAction.Pervasive. But the package I am compiling really uses both.
To me, either behavior looks incorrect. Am I hitting a nuget bug, or a cryptic complex feature?
Xref: Opened https://github.com/NuGet/Home/issues/1867

NuGet pack fails with "Unable to find '#(_OutputPathItem->'%(FullPath)..."

I'm attempting my first NuGet package, and I'm running into some trouble. I have a fairly simple project, and a very simple .nuspec file:
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<description>$description$</description>
</metadata>
</package>
When I run NuGet pack with this command line:
NuGet.exe pack mylibrary.csproj -Verbosity detailed -Properties Configuration=Debug
I get this error:
NuGet.CommandLineException: Unable to find '#(_OutputPathItem->'%(FullPath)mylibrary.dll')'. Make sure the project has been built.
at NuGet.Commands.ProjectFactory.BuildProject()
at NuGet.Commands.ProjectFactory.CreateBuilder(String basePath)
at NuGet.Commands.PackCommand.BuildFromProjectFile(String path)
at NuGet.Commands.PackCommand.BuildPackage(String path)
at NuGet.Commands.PackCommand.ExecuteCommand()
at NuGet.Commands.Command.Execute()
at NuGet.Program.Main(String[] args)
The output files are definitely in the bin\Debug folder, but NuGet is apparently not finding them.
This apparently only happens when the .csproj file's ToolsVersion is set to 3.5 or lower. Setting ToolsVersion to 4.0 resolves the problem.
It seems that MSBuild 3.5 returns the unexpanded property value when calling _project.GetPropertyValue("TargetPath") (ProjectFactory.cs ~296), where MSBuild 4.0 returns the expanded property value.
We had the same problem. adding
-Prop Platform=AnyCPU
to the command line made it work for us.
This apparently only happens when the .csproj file's ToolsVersion is set to 3.5 or lower. Setting ToolsVersion to 4.0 resolves the problem.
I have created an issue for the NuGet project team here: https://nuget.codeplex.com/workitem/4012
This could also have the same error if the .NET Framework version is set to 4.0 for the assembly.

An item with the same key has already been added nuget error

I am currently creating a nuget package with a nuspec file but getting the following error:
An item with the same key has already been added.
My command I am using is:
nuget pack "MyProject.csproj" -o "..\Packages"
This is my nuspec file:
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<language>$language$</language>
</metadata>
<files>
<file src="bin\MyLibrary*.dll" target="lib\net45" />
</files>
</package>
The nuspec file I am using is also used when packaging other packages withing the same library. Could this be the reason why the above error is occurring? Any ideas?
It might be that you are adding files in nuspec that are also getting added when you call pack on the .csproj (files/dlls referenced by the csproj). If so you can remove the file references from the nuspec file and give it a try.
how does your nuspec file look like?
I created it with the following steps
1. create the Mylibrary project
2. let me add a dependency, I installed ninject package to the project
3. build
4. nuget spec mylibrary.csproj
5. nuspec file generated, I didn't add any file or dependency manually to the file
6. nuget pack mylibrary.csproj
7. nuget pack would automatically add ninject as a dependency and also add mylibrary.dll into the correct folder
8. http://npe.codeplex.com/ is a nice tool to open the nupkg file and see what got generated inside the package.
I had this error trying to use the package visualizer and it ended up that my packages.config had the same package name in it more than once with different versions.