From 6c4007ab2245fa25622de8ad6d40bfb58224fc97 Mon Sep 17 00:00:00 2001 From: Swaroop Sridhar Date: Thu, 30 Apr 2020 00:27:25 -0700 Subject: [PATCH] [release/5.0.1xx-preview4] Single-File: Close AppHost stream when reusing extraction Customer Scenario Self-contained Apps published as a single-file fail at run-time. Problem Single-file bundles are now processed in the framework rather than the apphost (https://github.com/dotnet/runtime/pull/34274). This means that hostpolicy and hostfxr DLLs are excluded from being bundled themselves. In the case of self-contained single-file apps, these files need to be separate files until static-apphost is available. This needs to be ensured by the SDK; otherwise app execution will fail. Solution This change fixes the problem by adapting the SDK to: * Pass the correct target-OS settings (otherwise cross-targetting builds will not work correctly) * Copy files excluded by the Bundler to the publish directory. The stage-0 netcoreapp is updated to preview4, because preview2 apphost is not compatible with preview4 bundler. Risk Low --- global.json | 2 +- .../GenerateBundle.cs | 21 +++++++++- .../targets/Microsoft.NET.Publish.targets | 16 +++++--- .../GivenThatWeWantToPublishASingleFileApp.cs | 40 ++++++++++++++++++- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/global.json b/global.json index f08276344578..4b9b14d91385 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "tools": { - "dotnet": "5.0.100-preview.2.20152.7", + "dotnet": "5.0.100-preview.4.20229.10", "runtimes": { "dotnet": [ "$(MicrosoftNETCoreAppRuntimePackageVersion)" diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateBundle.cs b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateBundle.cs index 8d7bb98d39b6..97da09c9a78d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/GenerateBundle.cs +++ b/src/Tasks/Microsoft.NET.Build.Tasks/GenerateBundle.cs @@ -5,6 +5,8 @@ using Microsoft.NET.HostModel.Bundle; using System; using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; namespace Microsoft.NET.Build.Tasks { @@ -18,13 +20,24 @@ public class GenerateBundle : TaskBase public bool IncludeSymbols { get; set; } [Required] public string OutputDir { get; set; } + [Required] + public string TargetFrameworkVersion { get; set; } + [Required] + public string RuntimeIdentifier { get; set; } + [Required] public bool ShowDiagnosticOutput { get; set; } + [Output] + public ITaskItem[] ExcludedFiles { get; set; } + protected override void ExecuteCore() { + OSPlatform targetOS = RuntimeIdentifier.StartsWith("win") ? OSPlatform.Windows : + RuntimeIdentifier.StartsWith("osx") ? OSPlatform.OSX : OSPlatform.Linux; + BundleOptions options = BundleOptions.BundleAllContent | (IncludeSymbols ? BundleOptions.BundleSymbolFiles : BundleOptions.None); - var bundler = new Bundler(AppHostName, OutputDir, options, diagnosticOutput: ShowDiagnosticOutput); + var bundler = new Bundler(AppHostName, OutputDir, options, targetOS, new Version(TargetFrameworkVersion), ShowDiagnosticOutput); var fileSpec = new List(FilesToBundle.Length); foreach (var item in FilesToBundle) @@ -34,6 +47,12 @@ protected override void ExecuteCore() } bundler.GenerateBundle(fileSpec); + + // Certain files are excluded from the bundle, based on BundleOptions. + // For example, native files and contents files are excluded by default. + // Return the set of excluded files in ExcludedFiles, so that they can be placed in the publish directory. + + ExcludedFiles = FilesToBundle.Zip(fileSpec, (item, spec) => (spec.Excluded) ? item : null).Where(x => x != null).ToArray(); } } } diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets index a0117a1d086d..e9a181c3f06d 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets @@ -945,11 +945,6 @@ Copyright (c) .NET Foundation. All rights reserved. $(PublishDir)$(PublishedSingleFileName) - - - - - @@ -963,8 +958,17 @@ Copyright (c) .NET Foundation. All rights reserved. AppHostName="$(PublishedSingleFileName)" IncludeSymbols="$(IncludeSymbolsInSingleFile)" OutputDir="$(PublishDir)" - ShowDiagnosticOutput="false"/> + TargetFrameworkVersion="$(_TargetFrameworkVersionWithoutV)" + RuntimeIdentifier="$(RuntimeIdentifier)" + ShowDiagnosticOutput="false"> + + + + + + +