From b5265ef370a651f8c3458110b804e5cbf869eeb5 Mon Sep 17 00:00:00 2001 From: sujitnayak Date: Fri, 5 Jan 2024 00:04:25 -0800 Subject: [PATCH] [ClickOnce] Handle multiple apphost.exe files that could be published with an EXE to EXE P2P reference (#9447) * [ClickOnce] Handle multiple apphost.exe files that could be published with an EXE to EXE P2P dependency * Bump version --------- Co-authored-by: Ladi Prosek --- eng/Versions.props | 2 +- src/Tasks/ManifestUtil/Manifest.cs | 31 ++++++++++++++++++++---------- src/Tasks/ResolveManifestFiles.cs | 9 ++++++--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/eng/Versions.props b/eng/Versions.props index 6aace9e7fcb..900dcba4252 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -2,7 +2,7 @@ - 17.8.4release + 17.8.5release 17.7.0 15.1.0.0 preview diff --git a/src/Tasks/ManifestUtil/Manifest.cs b/src/Tasks/ManifestUtil/Manifest.cs index 629fcb877e0..0dd8e3bf679 100644 --- a/src/Tasks/ManifestUtil/Manifest.cs +++ b/src/Tasks/ManifestUtil/Manifest.cs @@ -487,7 +487,7 @@ private void UpdateAssemblyReference(AssemblyReference a, string targetFramework private void UpdateFileReference(BaseReference f, string targetFrameworkVersion) { - if (String.IsNullOrEmpty(f.ResolvedPath)) + if (string.IsNullOrEmpty(f.ResolvedPath)) { throw new FileNotFoundException(null, f.SourcePath); } @@ -506,22 +506,33 @@ private void UpdateFileReference(BaseReference f, string targetFrameworkVersion) f.Size = size; // - // .NETCore Launcher.exe based Deployment: If the filereference is for apphost.exe, we need to change - // the ResolvedPath and TargetPath to {assemblyname}.exe before we write the manifest, so that the - // manifest does not have a file reference to apphost.exe + // .NET >= 5 ClickOnce: If the file reference is for apphost.exe, we need to change the filename + // in ResolvedPath to TargetPath so we don't end up publishing the file as apphost.exe. + // If the TargetPath is not present, we will fallback to AssemblyName. // string fileName = Path.GetFileName(f.ResolvedPath); if (LauncherBasedDeployment && - fileName.Equals(Constants.AppHostExe, StringComparison.InvariantCultureIgnoreCase) && - !String.IsNullOrEmpty(AssemblyName)) + fileName.Equals(Constants.AppHostExe, StringComparison.InvariantCultureIgnoreCase)) { - f.ResolvedPath = Path.Combine(Path.GetDirectoryName(f.ResolvedPath), AssemblyName); - f.TargetPath = BaseReference.GetDefaultTargetPath(f.ResolvedPath); + if (!string.IsNullOrEmpty(f.TargetPath)) + { + f.ResolvedPath = Path.Combine(Path.GetDirectoryName(f.ResolvedPath), f.TargetPath); + } + else if (!string.IsNullOrEmpty(AssemblyName)) + { + f.ResolvedPath = Path.Combine(Path.GetDirectoryName(f.ResolvedPath), AssemblyName); + f.TargetPath = BaseReference.GetDefaultTargetPath(f.ResolvedPath); + } + else + { + Debug.Assert(false, "AssemblyName cannot be empty"); + OutputMessages.AddWarningMessage("GenerateManifest.InvalidValue", "AssemblyName"); + } } - if (String.IsNullOrEmpty(f.TargetPath)) + if (string.IsNullOrEmpty(f.TargetPath)) { - if (!String.IsNullOrEmpty(f.SourcePath)) + if (!string.IsNullOrEmpty(f.SourcePath)) { f.TargetPath = BaseReference.GetDefaultTargetPath(f.SourcePath); } diff --git a/src/Tasks/ResolveManifestFiles.cs b/src/Tasks/ResolveManifestFiles.cs index 9b98bcf97a8..9a78f010f16 100644 --- a/src/Tasks/ResolveManifestFiles.cs +++ b/src/Tasks/ResolveManifestFiles.cs @@ -285,14 +285,17 @@ private ITaskItem CreateFileItem(ITaskItem item, string group, string targetPath { targetPath = Path.GetFileName(item.ItemSpec); // - // .NETCore Launcher.exe based deployment: If the file is apphost.exe, we need to set 'TargetPath' metadata - // to {assemblyname}.exe so that the file gets published as {assemblyname}.exe and not apphost.exe. + // .NET >= 5 ClickOnce: If TargetPath metadata is not present in apphost.exe's metadata, we'll fallback to using AssemblyName // if (LauncherBasedDeployment && targetPath.Equals(Constants.AppHostExe, StringComparison.InvariantCultureIgnoreCase) && !String.IsNullOrEmpty(AssemblyName)) { - targetPath = AssemblyName; + targetPath = item.GetMetadata(ItemMetadataNames.targetPath); + if (String.IsNullOrEmpty(targetPath)) + { + targetPath = AssemblyName; + } } else {