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:
- First, I run a regular build command:
dotnet build -c Release - Then, I attempt to publish the project for
win-x64using 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
- Finally, I run the same
MSBuild.execommand again forlinux-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 thelinux-x64target, whereasapphost.exeis generated for Windows.
How can I resolve this error and successfully publish my project for bothwin-x64andlinux-x64runtimes using MSBuild.exe with the/p:NoBuild=trueoption while ensuring that apphost is generated for thelinux-x64target 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):
side note: I need to use `msbuild.exe` instead of `dotnet publish`, because I reference a C++/CLI project.

Take a look of these command, those works fine on my side:
If still doesn't work, please share more info about your project(a minimal reproduce sample is ok, such as a .csproj file).