dotnet publish with --no-build and PublishSingleFile fails with "Could not find file singlefilehost.exe" error
See original GitHub issueDescription
Running the following on Windows 10 or Server Core 2019:
mkdir test
cd test
dotnet new console
dotnet restore -r win-x64
dotnet build --no-restore -r win-x64 --configuration Debug
dotnet publish --no-build -r win-x64 --configuration Debug -p:PublishSingleFile=true --output ./publish
results in the following error message (truncated):
The "GenerateBundle" task failed unexpectedly.
System.IO.FileNotFoundException: Could not find file 'D:\dev\test\obj\Debug\net5.0\win-x64\singlefilehost.exe'.
Removing the --no-build
parameter or using --no-restore
fixes the issue.
Configuration
- .NET 5.0.101
- Windows 10.0.19042
Can also be reproduced on docker images:
mcr.microsoft.com/dotnet/sdk:5.0
mcr.microsoft.com/dotnet/sdk:5.0-windowsservercore-ltsc2019
Other information
Full error messages / stack trace:
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: System.IO.FileNotFoundException: Could not find file 'D:\dev\test\obj\Debug\net5.0\win-x64\singlefilehost.exe'. [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: File name: 'D:\dev\test\obj\Debug\net5.0\win-x64\singlefilehost.exe' [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at System.IO.FileSystem.CopyFile(String sourceFullPath, String destFullPath, Boolean overwrite) [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite) [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.NET.HostModel.AppHost.BinaryUtils.CopyFile(String sourcePath, String destinationPath) [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.NET.HostModel.Bundle.Bundler.GenerateBundle(IReadOnlyList`1 fileSpecs) [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.NET.Build.Tasks.GenerateBundle.ExecuteCore() [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.NET.Build.Tasks.TaskBase.Execute() [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [D:\dev\test\test.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1016,5): error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [D:\dev\test\test.csproj]
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (2 by maintainers)
Top Results From Across the Web
no-build and PublishSingleFile fails with "Could not find file ...
dotnet publish with --no-build and PublishSingleFile fails with "Could not find file singlefilehost.exe" error #17526.
Read more >PublishSingleFile error on upgrading from netcoreapp3.1 ...
If we make PublishSingleFile = false , Publish command is working and creating application and dll files in Artifacts. But we need to...
Read more >Self-Contained Single-File does not produce a single file
I have a small .Net 5 console application with no dependencies on third party components. I have set it up to publish as...
Read more >Publish single executable not working for .NET 5 or .NET 6
Solved: Publishing a WPF application as a single executable does not work ... If the exe is moved to another folder it cannot...
Read more >A Guide to Missing Publish Directory – Write it Down
Upon running this pipeline, the “Dotnet Publish” step fails with the following error: ##[error]No web project was found in the repository. Web projects...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
If you add
-p:PublishSingleFile=true
to the build step as well as the publish step, this seems to solve it. I guess defining thePublishSingleFile
property in the build outputs the file that’s missing. So the working version is:This is basically a duplicate of https://github.com/dotnet/sdk/issues/14827
The problem is that you can’t set new properties in Publish because they may change things during the build which later get consumed during Publish.