12

I'm trying to design a build script for a dotnet core 2.0 project that does the following actions

  1. Cleans output directories
  2. Builds Solution with -o bin\Publish
  3. Runs Unit Tests with dotnet vstest
  4. Creates a Nuget package with dotnet pack

Because I know the source code has been built and tested in steps 1-3 I do not want to rebuild my code for the nuget package, so I am specifying --no-build and --no-restore

The difficulty I'm having is that when creating the package, because I am not building and the output directory is set to be bin\Publish - the pack command is looking for items in the bin\Debug directory.

Is there a way I can set the dotnet pack command to know where to look for the already compiled objects?

Here is a sample of my build script

dotnet clean ..\MySolution.sln -o bin/Publish/
dotnet build ..\MySolution.sln /p:Configuration=Release -o bin/Publish/
dotnet vstest ..\MySolution.UnitTests\bin\Publish\MySolution.UnitTests.dll
dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\

....

error : The file 'D:\MySolution.ConsoleApp\bin\Debug\netcoreapp2.0\MySolution.ConsoleApp.runtimeconfig.json' to be packed was not found on disk

According to the Microsoft Docs on dotnet pack

"By default, dotnet pack builds the project first. If you wish to avoid this behavior, pass the --no-build option. This is often useful in Continuous Integration (CI) build scenarios where you know the code was previously built."

So I'm hoping that this is possible and I'm missing something obvious. Any help would be appreciated.

user247702
  • 23,641
  • 15
  • 110
  • 157
Joe van de Bilt
  • 225
  • 1
  • 3
  • 12

1 Answers1

23

The reason for this is that the dotnet pack --no-build option will try to use the previously built output, but can't find it since you built to a non-standard output path and the pack logic needs to locate some assets generated into the build output.

The -o options are internally different for the pack and build commands but you can change the pack command to:

dotnet pack --no-build --no-restore ..\MySolution.ConsoleApp\MySolution.csproj -o bin\Publish\Nuget\ /p:OutputPath=bin\Publish\

This will use the output built into the bin\Publish directory and put it into the Nuget output directory in bin\Publish\Nuget\

Martin Ullrich
  • 94,744
  • 25
  • 252
  • 217
  • I was just about there - I had only had the thought to override the MSBuild internal parameters just as you commented - thank you for your answer! – Joe van de Bilt May 08 '18 at 12:27
  • ``--no-build`` will also implicitly sets ``--no-restore`` (as per documentation) – Alex 75 Feb 04 '20 at 17:04
  • 1
    Yes this was "fixed" in 2.1/2.2, do note that this question originally was about 2.0 which didn't yet do this - https://github.com/dotnet/cli/pull/8100 (which is why I deliberately listed both in the answer) – Martin Ullrich Feb 04 '20 at 18:28
  • Hello. I tried this solution and it worked up to a specific point, here meaning that the content of the Nuget package is all scrambled and not all dependencies are included. Does anyone have an idea why? Or how should I do it to include the exact contents and structure of the \Publish folder? – Marius Popa Mar 19 '20 at 19:13
  • nuget packages with `dotnet pack` only contain the own build output, not any of dependencies (those are included as NuGet dependencies, so you get a .nupkg for each csproj). If you build tools, you can build tool packages instead [tutorial doc](https://learn.microsoft.com/en-us/dotnet/core/tools/global-tools-how-to-create) – Martin Ullrich Mar 19 '20 at 21:13