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.71k stars 1.06k forks source link

A combination of dotnet build and dotnet publish --no-build -p:PublishSingleFile=true does not work in .net8.0 #37367

Open goldcode opened 11 months ago

goldcode commented 11 months ago

Describe the bug

After migrating from .net6.0 to .net8.0, our builds fail when publishing with PublishSingleFile using dotnet publish --no-build -r win-x64 --configuration Debug -p:PublishSingleFile=true --output ./publish

we get following error:

11>C:\Program Files\dotnet\sdk\8.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1038,5): error MSB4018: The "GenerateBundle" task failed unexpectedly. [E:\Software\trunk\Applications\test\test.csproj]

11>C:\Program Files\dotnet\sdk\8.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(1038,5): error MSB4018: System.IO.FileNotFoundException: Could not find file 'E:\Software\trunk\Applications\test\obj\Release\net8.0-windows\win-x64\singlefilehost.exe'. [E:\Software\trunk\Applications\test\test.csproj]

To Reproduce

Fails

Works

what currently works is unified building and publishing.

mkdir Works
cd Works
dotnet new console
dotnet publish -r win-x64 --configuration Release -p:PublishSingleFile=true --output ./publish

I would presume dotnet build does not respect the project property PublishSingleFile and creates an apphost.exe (instead of singlehost.exe) in obj\Release\net6.0-windows\win-x64\

Further technical details

.NET SDK: Version: 8.0.100 Commit: 57efcf1350 Workload version: 8.0.100-manifests.8d38d0cc

Runtime Environment: OS Name: Windows OS Version: 10.0.22621 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed: Workload version: 8.0.100-manifests.8d38d0cc There are no installed workloads to display.

Host: Version: 8.0.0 Architecture: x64 Commit: 5535e31a71

.NET SDKs installed: 5.0.414 [C:\Program Files\dotnet\sdk] 6.0.100 [C:\Program Files\dotnet\sdk] 6.0.408 [C:\Program Files\dotnet\sdk] 7.0.400 [C:\Program Files\dotnet\sdk] 8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found: arm64 [C:\Program Files\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation] x86 [C:\Program Files (x86)\dotnet] registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables: Not set

global.json file: Not found

Visual Studio

Microsoft Visual Studio Professional 2022 (3) Version 17.8.2 VisualStudio.17.Release/17.8.2+34322.80 Microsoft .NET Framework Version 4.8.09032

Installed Version: Professional

Vogel612 commented 11 months ago

This is IMO likely to have a very similar cause to #37352

IMDiSc commented 10 months ago

We are experiencing the same behavior. Initially we were on .NET 7, with the exact same problem.

The solution however mentioned here solved it for us.

A few days ago the project got upgraded to .NET 8 and the publish step failed initially with the following error: error NETSDK1112: The runtime pack for Microsoft.NETCore.App.Runtime.win-x64 was not downloaded. Try running a NuGet restore with the RuntimeIdentifier 'win-x64'.

I then attempted to add the argument --runtime win-x64 to the dotnet restore step in our build and again ended up with the error message above.

We build with the following command:

dotnet build <proj> --configuration Release -p:PublishSingleFile=true

We publish with the following command:

dotnet publish <proj> --no-build --configuration "Release" -p:PublishProfile="<path to pubxml>" -p:PublishDir="<path to dir>" -p:SolutionDir="../"

Contents of pubxml:

<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121
-->
<Project>
  <PropertyGroup>
    <Configuration>Release</Configuration>
    <!-- Not used by CLI, only VS -->
    <Platform>Any CPU</Platform>
    <!-- Not used by CLI -->
    <PublishDir>bin\Release\net8.0\win-x64\publish\win-x64\</PublishDir>
    <PublishProtocol>FileSystem</PublishProtocol>
    <_TargetId>Folder</_TargetId>
    <TargetFramework>net8.0</TargetFramework>
    <!-- Not used by CLI -->
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <!-- Not used by CLI -->
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
    <PublishReadyToRun>false</PublishReadyToRun>
    <PublishTrimmed>false</PublishTrimmed>
  </PropertyGroup>
</Project>

So in short, this worked for .NET 7 but no longer works for .NET 8

goldcode commented 9 months ago

I managed to created a single file exe using the property <SelfContained>true</SelfContained> I can also verify that recommended property PublishSelfContained didn't work for me.

Also when building in studio, you may need to manually delete obj and bin folders before building. make sure you don't have any autogenerated obj folders just before building. Presumably the visual studio/msbuild toolset watches for csproj file for changes (like editing project properties) and updates the obj folder.

like so (WPF App):


<Project>
    <PropertyGroup>
        <OutputType>WinExe</OutputType>
        <TargetFramework>net8.0-windows</TargetFramework>
        <Nullable>enable</Nullable>
        <UseWPF>true</UseWPF>
        <PublishSingleFile>true</PublishSingleFile>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
        <PlatformTarget>x64</PlatformTarget>
        <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
        <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
        <DebugType>embedded</DebugType>
        <DebugSymbols>true</DebugSymbols>
        <EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
        <SelfContained>true</SelfContained>
    </PropertyGroup>
</Project>
dehghani-mehdi commented 8 months ago

Same problem here, in my case, this command dotnet publish -c Release works, but if I add -p:PublishSingleFile=true or -r win-x64 I get the following error:

error NETSDK1047: Assets file '...\obj\project.assets.json' doesn't have a target for 'net8.0/win-x64'. Ensure that restore has run and that you have included 'net8.0' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers. [....csproj]