Error when using MSBuild.exe to publish a .NET 6.0 C# project for Windows and Linux x64 with /p:NoBuild=true

400 Views Asked by At

I have a plain .csproj C# project targeting .NET 6.0, and I'm trying to streamline the process of building and publishing it for both win-x64 and linux-x64 runtimes. To avoid building the project twice, I'm using MSBuild.exe with the /p:NoBuild=true flag. However, I'm encountering the following error:

C:\Program Files\dotnet\sdk\7.0.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(296,5): error MSB3030: Could not copy the file C:\Users\john.doe\source\repos\ConsoleApp9\ConsoleApp9\obj\x64\Release\apphost" because it was not found.

Here are the commands I'm using:

  1. First, I run a regular build command: dotnet build -c Release
  2. Then, I attempt to publish the project for win-x64 using MSBuild.exe with /p:NoBuild=true:
MSBuild.exe ConsoleApp9.csproj /p:Platform=x64 /p:TargetFramework=net6.0 /p:Configuration=Release /t:publish /p:NoBuild=true /p:RuntimeIdentifier=win-x64
  1. Finally, I run the same MSBuild.exe command again for linux-x64, but it fails with the error mentioned above. I noticed that the apphost file is not being generated in the obj directory during the build process for the linux-x64 target, whereas apphost.exe is generated for Windows.
    How can I resolve this error and successfully publish my project for both win-x64 and linux-x64 runtimes using MSBuild.exe with the /p:NoBuild=true option while ensuring that apphost is generated for the linux-x64 target during the build process?
MSBuild.exe ConsoleApp9.csproj /p:Platform=x64 /p:TargetFramework=net6.0 /p:Configuration=Release /t:publish /p:NoBuild=true /p:RuntimeIdentifier=linux-x64

I believe this should be possible, because Visual Studio can already do this (see at the bottom, it says 1 project up-to-date):

enter image description here


side note: I need to use `msbuild.exe` instead of `dotnet publish`, because I reference a C++/CLI project.
1

There are 1 best solutions below

2
Bowman Zhu-MSFT On

Take a look of these command, those works fine on my side:

MSBuild.exe ConsoleApp9.csproj /t:Restore /p:RuntimeIdentifier=linux-x64
MSBuild.exe ConsoleApp9.csproj /p:Platform=x64 /p:TargetFramework=net6.0 /p:Configuration=Release /t:publish /p:NoBuild=true /p:RuntimeIdentifier=linux-x64

enter image description here

If still doesn't work, please share more info about your project(a minimal reproduce sample is ok, such as a .csproj file).