dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.73k stars 1.07k forks source link

dotnet publish with --no-build and PublishSingleFile fails with "Could not find file singlefilehost.exe" error #17526

Open antonyoni opened 3 years ago

antonyoni commented 3 years ago

Description

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

Can also be reproduced on docker images:

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]
ghost commented 3 years ago

Tagging subscribers to this area: @vitek-karas, @agocke See info in area-owners.md if you want to be subscribed.

Issue Details
### Description 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] ```
Author: antonyoni
Assignees: -
Labels: `area-HostModel`, `untriaged`
Milestone: -
antonyoni commented 3 years ago

This issue appears to be related: https://github.com/dotnet/wpf/issues/3711

mkielek commented 3 years ago

any news on the issue?

rupertbenbrook commented 3 years ago

If you add -p:PublishSingleFile=true to the build step as well as the publish step, this seems to solve it. I guess defining the PublishSingleFile property in the build outputs the file that's missing. So the working version is:

mkdir test
cd test
dotnet new console
dotnet restore -r win-x64
dotnet build --no-restore -r win-x64 --configuration Debug -p:PublishSingleFile=true
dotnet publish --no-build -r win-x64 --configuration Debug -p:PublishSingleFile=true --output ./publish
dotnet-issue-labeler[bot] commented 3 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

antonyoni commented 3 years ago

If you add -p:PublishSingleFile=true to the build step as well as the publish step, this seems to solve it. I guess defining the PublishSingleFile property in the build outputs the file that's missing. So the working version is:

mkdir test
cd test
dotnet new console
dotnet restore -r win-x64
dotnet build --no-restore -r win-x64 --configuration Debug -p:PublishSingleFile=true
dotnet publish --no-build -r win-x64 --configuration Debug -p:PublishSingleFile=true --output ./publish

Yep, that solves it. Thank you!

Is this something that still needs to be fixed then or just documented?

GOATS2K commented 1 year ago

If you add -p:PublishSingleFile=true to the build step as well as the publish step, this seems to solve it. I guess defining the PublishSingleFile property in the build outputs the file that's missing. So the working version is:

mkdir test
cd test
dotnet new console
dotnet restore -r win-x64
dotnet build --no-restore -r win-x64 --configuration Debug -p:PublishSingleFile=true
dotnet publish --no-build -r win-x64 --configuration Debug -p:PublishSingleFile=true --output ./publish

This works when you're building both with the same configuration, if build is happening with a different configuration than publish, you run into the same error.

Edit: If you're cross-compiling for a different architecture, it's still broken. This works:

dotnet build -c Release --no-restore -p:PublishSingleFile=true && dotnet publish --no-build -c Release -r osx-x64 -o ~/tools/coral/osx-x64

This does not:

dotnet build -c Release --no-restore -p:PublishSingleFile=true && dotnet publish --no-build -c Release -r osx-arm64 -o ~/tools/coral/osx-arm64

Edit 2: It gets even weirder, Windows builds don't work either!

agocke commented 1 year ago

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.

VictorioBerra commented 10 months ago

How can I do this with Cake's DotNetBuild()?

DanielHWe commented 9 months ago

I had the same problem, I fixed it by adding <SelfContained>true</SelfContained> to the csproj file.

daniol commented 8 months ago

On .NET 8 you have to set the "self contained" property to true, so you have to add -p:PublishSingleFile=true --self-contained true on both build and publish commands in order to work.

thgla commented 7 months ago

On .NET 8 you have to set the "self contained" property to true, so you have to add -p:PublishSingleFile=true --self-contained true on both build and publish commands in order to work.

Wow, I just had the same experience when troubleshooting a build issue. PublishSingleFile is set in the project, self contained should be the default when a runtime identifier is specified (according to docs at least).. yet it refused to build without using both of these. I don't get it, something feels very wrong here.