4

I have a .NET Standard 2.0 project called ProjectName.Logging with a ProjectName.Logging.nuspec file.

This file file is referenced in the .csproj of my project

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
  <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
  <NuspecFile>ProjectName.Logging.nuspec</NuspecFile>  
</PropertyGroup>

Here is my .nuspec

<?xml version="1.0"?>
<package >
    <metadata>
        <id>ProjectName.Logging</id>
        <version>1.2.1</version>
        <authors>Jérôme MEVEL</authors>
        <description>Just a logging component</description>
        <dependencies>
            <dependency id="Dapper" version="1.50.5" />
            <dependency id="Another.Project" version="1.1.1" />
            <dependency id="Microsoft.Extensions.DependencyInjection" version="2.1.1" />
            <dependency id="Microsoft.Extensions.Logging" version="2.1.1" />
            <dependency id="Microsoft.Extensions.Logging.Abstractions" version="2.1.1" />
            <dependency id="NLog" version="4.5.8" />
            <dependency id="NLog.Extensions.Logging" version="1.2.1" />
            <dependency id="NLog.Web.AspNetCore" version="4.6.0" />
            <dependency id="System.Data.SqlClient" version="4.5.1" />
        </dependencies>
    </metadata>
    <files>
        <file src="bin\Release\netstandard2.0\ProjectName.Logging.dll" target="lib/netstandard2.0/ESHCloud.Logging.dll" />
        <file src="ProjectName.Logging.targets" target="build/ProjectName.Logging.targets" />
        <file src="NLog/Nlog.config" target="content/Nlog.config" />
    </files>
</package>

As you can see I manually include the ProjectName.Logging.dll file and I even have to include Release because the usage of the MsBuild variable $(Configuration) doesn't work in my .nuspec file.

I pack the project by executing this command in my project's directory

dotnet pack --output C:/Nuget --force

and if I remove the <files> element from my .nuspec when I run the dotnet pack command, my generated Nuget package is completely empty.

On the other hand if I run the dotnet pack command without a .nuspec file, absolutely everything in my project is included in the Nuget package.

So what's the deal with the dotnet pack command? I don't understand what am I doing wrong.

How to include my project's DLL in the Nuget package without having to specify the configuration (Debug or Release)?

One last thing: I don't want to use the nuget pack command.

I played enough with Nuget on our VSTS servers (recently renamed Azure DevOps) and I don't have full control over these build servers (my manager has but he seems too busy to care about it).

So to sum up, nuget pack is not an option for me. I want to use dotnet pack along with a .nuspec file.

Thank you

Jérôme MEVEL
  • 7,031
  • 6
  • 46
  • 78

1 Answers1

4

To add your DLL without specifying the configuration use a glob pattern:

<file src="runtimes\**" target="runtimes/"/>

If you use nuget.exe you could use a replacement token:

<file src="bin\$configuration$\netstandard2.0\*.dll" target="lib\netstandard2.0\"/>

If you really need to use the nuspec file you're better off using nuget.exe. If you don't want to use nuget.exe drop the nuspec. I've done a fair amount of work with dotnet and nupkgs the last few weeks and mixing nuspec with project files is fiddly.

You can put the majority of the nupkg meta-data directly in the csproj:

<PropertyGroup>
  <PackageId>Subor.nng.NETCore</PackageId>
  <PackageVersion>0.0.2</PackageVersion>
  <Authors>Subor</Authors>
  ...
</PropertyGroup>

This is the recommended approach when using the dotnet CLI. Likewise for msbuild with the PackageReference format.

Here's an example of a project I've been working on. dotnet pack builds a package bin/Debug/ and dotnet pack --configuration Release builds this nuget.org package.

Everything in the project should not get copied into the nupkg. In Visual Studio check the Properties of the files and make sure everything isn't flagged as "Content", etc.. Also check the project files to see if files are getting added that shouldn't be. Look for (miss-)use of <PackagePath> and <IsPackable>.

Without knowing the particulars of your project, I'll mention trying some of the arguments to dotnet pack.

Jake
  • 1,304
  • 9
  • 11
  • Thanks a lot for your answer but as I said in my question, I can't use `nuget pack` on our VSTS server simply because of [this bug](https://github.com/NuGet/Home/issues/4808). And I need to use a `.nuspec` file along with a `.targets` file because I need to extract the `NLog.config` file in the Output Directory – Jérôme MEVEL Sep 14 '18 at 03:39
  • Use the `$configuration$` replacement token or glob pattern as I added to the top of my answer. – Jake Sep 14 '18 at 03:50
  • Hey @Jake sorry for the late reply. Sadly the `$configuration` replacement token doesn't work. If I use `dotnet pack` this should give me the default `Debug` configuration but it doesn't. I get this error message `error : Could not find a part of the path 'C:\Git\ProjetName.Logging\bin\netstandard2.0'` even if I explicitly specify the configuration it still doesn't work `dotnet pack --configuration Release`. Are replacement tokens only usable using the Nuget? If yes then this doesn't solve my problem – Jérôme MEVEL Sep 20 '18 at 01:38
  • But I actually solved my problem for now. I downloaded the Nuget CLI 4.7.1 and included it in my solution. Then in Azure DevOps I added a command line task which is calling this nuget.exe to execute the pack command. No more bug... But still if you could tell me how to get the replacement token working with the dotnet CLI, I'm curious about why it doesn't work. Thanks a lot – Jérôme MEVEL Sep 20 '18 at 01:43
  • Replacement tokens might only work with nuget, in which case you would use the glob pattern. I'll re-iterate the suggestion to either use nuget or drop the nuspec. This almost certainly isn't the end of your troubles. – Jake Sep 20 '18 at 02:16
  • Ok I misunderstood you about replacement tokens. I thought this was supported by the dotnet CLI. Yes as I said in my previous comment, I successfully switched to Nuget CLI. The only thing preventing me for using it before was a bug in older version of the CLI. Azure DevOps servers don't offer any _official way_ of using the latest CLI version except with some workarounds like I did. I can't drop the `.nuspec` file simply because I need to include a `.targets` file that will perform a `CopyToOutputDirectory` of the `NLog.config` file to the referencing projects. Thanks for your help – Jérôme MEVEL Sep 20 '18 at 02:48
  • Glad to hear! I tried to edit my answer accordingly. Maybe I should just edit it down... Out of curiosity, if you need to copy `NLog.config` to a particular place, can you not include it as `` and then `` along with `` ([like this](https://github.com/subor/nng.NETCore/blob/master/nng.NETCore/nng.NETCore.csproj#L34)). Or, are you not really allowed to touch the project files? – Jake Sep 20 '18 at 03:29
  • I had lots of troubles with `content` or `contentFiles`, actually this is another of my SO questions: [https://stackoverflow.com/questions/51959638/nuget-package-contentfiles-not-copied-to-net-core-project](https://stackoverflow.com/questions/51959638/nuget-package-contentfiles-not-copied-to-net-core-project) I'd be really thankful if you could solve my 2nd Nuget issue as well :-) – Jérôme MEVEL Sep 20 '18 at 03:49
  • 1
    Ah... I see. Yeah, I've had more than my fair share of problems with all of it. =) – Jake Sep 20 '18 at 04:22