From 631f2e829cf8df777cfa81c9b062c57c730706fc Mon Sep 17 00:00:00 2001 From: Surbhi Gupta Date: Wed, 2 Oct 2024 22:06:57 -0700 Subject: [PATCH] Updating coretools to detect unsupported inproc6 sdk scenario (#3825) --- build/BuildSteps.cs | 8 ++++++++ host/src/CoreToolsHost/CoreToolsHost.csproj | 5 +++++ .../Actions/HostActions/StartHostAction.cs | 15 ++++++++++++++ src/Azure.Functions.Cli/Common/Constants.cs | 2 ++ .../Common/DotnetConstants.cs | 4 ++++ src/Azure.Functions.Cli/Common/Utilities.cs | 20 +++++++++++++++++++ .../UtilitiesTests.cs | 16 +++++++++++++++ 7 files changed, 70 insertions(+) diff --git a/build/BuildSteps.cs b/build/BuildSteps.cs index 5c4d13fe1..480498b59 100644 --- a/build/BuildSteps.cs +++ b/build/BuildSteps.cs @@ -120,6 +120,7 @@ public static void DotnetPublishForZips() if (isMinVersion) { RemoveLanguageWorkers(outputPath); + CreateMinConfigurationFile(outputPath); } // Publish net8 version of the artifact as well. @@ -792,6 +793,13 @@ private static void RemoveLanguageWorkers(string outputPath) } } + private static void CreateMinConfigurationFile(string outputPath) + { + var filePath = Path.Combine(outputPath, "artifactsconfig.json"); + string artifactsJsonContent = "{\"minifiedVersion\": true}"; + File.WriteAllTextAsync(filePath, artifactsJsonContent).GetAwaiter().GetResult(); + } + private static PackageInfo GetLatestPackageInfo(string name, string majorVersion, string source) { string includeAllVersion = !string.IsNullOrWhiteSpace(majorVersion) ? "-AllVersions" : string.Empty; diff --git a/host/src/CoreToolsHost/CoreToolsHost.csproj b/host/src/CoreToolsHost/CoreToolsHost.csproj index 357b5b5c1..6550771fd 100644 --- a/host/src/CoreToolsHost/CoreToolsHost.csproj +++ b/host/src/CoreToolsHost/CoreToolsHost.csproj @@ -7,6 +7,11 @@ enable True true + + false Speed func true diff --git a/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs b/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs index ce0de6f28..dfd09ea4d 100644 --- a/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs +++ b/src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs @@ -470,8 +470,14 @@ private async Task TryHandleInProcDotNetLaunchAsync() if (!string.Equals(HostRuntime, "default", StringComparison.OrdinalIgnoreCase)) { + if (Utilities.IsMinifiedVersion()) + { + ThrowForInProc(); + } + var isNet8InProcSpecified = string.Equals(HostRuntime, DotnetConstants.InProc8HostRuntime, StringComparison.OrdinalIgnoreCase); StartHostAsChildProcess(isNet8InProcSpecified); + return true; } @@ -488,6 +494,11 @@ private async Task TryHandleInProcDotNetLaunchAsync() PrintVerboseForHostSelection(selectedRuntime); + if (Utilities.IsMinifiedVersion()) + { + ThrowForInProc(); + } + StartHostAsChildProcess(isDotNet8Project); return true; @@ -541,6 +552,10 @@ void ThrowCliException(string suffix) PrintVerboseForHostSelection(HostRuntime); } + private void ThrowForInProc() + { + throw new CliException($"This version of the Azure Functions Core Tools requires your project to reference version {DotnetConstants.InProcFunctionsMinSdkVersion} or later of {DotnetConstants.InProcFunctionsSdk}. Please update to the latest version. For more information, see: {DotnetConstants.InProcFunctionsDocsLink}"); + } private void PrintVerboseForHostSelection(string hostRuntime) { if (VerboseLogging.GetValueOrDefault()) diff --git a/src/Azure.Functions.Cli/Common/Constants.cs b/src/Azure.Functions.Cli/Common/Constants.cs index 0de6021f8..92122a94e 100644 --- a/src/Azure.Functions.Cli/Common/Constants.cs +++ b/src/Azure.Functions.Cli/Common/Constants.cs @@ -26,6 +26,8 @@ internal static class Constants public const string HostJsonFileName = "host.json"; public const string PackageJsonFileName = "package.json"; public const string ProxiesJsonFileName = "proxies.json"; + public const string ArtifactsConfigFileName = "artifactsconfig.json"; + public const string MinifiedVersionConfigSectionName = "minifiedVersion"; public const string ExtensionsCsProjFile = "extensions.csproj"; public const string DefaultVEnvName = "worker_env"; public const string ExternalPythonPackages = ".python_packages"; diff --git a/src/Azure.Functions.Cli/Common/DotnetConstants.cs b/src/Azure.Functions.Cli/Common/DotnetConstants.cs index d46022bdb..8c4b58bd0 100644 --- a/src/Azure.Functions.Cli/Common/DotnetConstants.cs +++ b/src/Azure.Functions.Cli/Common/DotnetConstants.cs @@ -16,5 +16,9 @@ internal static class DotnetConstants public const string InProc6HostRuntime = "inproc6"; public static readonly string[] ValidRuntimeValues = [InProc8HostRuntime, InProc6HostRuntime, "default"]; + + public const string InProcFunctionsSdk = "Microsoft.NET.Sdk.Functions"; + public const string InProcFunctionsMinSdkVersion = "4.5.0"; + public const string InProcFunctionsDocsLink = "https://aka.ms/functions-core-tools-in-proc-sdk-requirement"; } } diff --git a/src/Azure.Functions.Cli/Common/Utilities.cs b/src/Azure.Functions.Cli/Common/Utilities.cs index 8dfe174be..d9670e698 100644 --- a/src/Azure.Functions.Cli/Common/Utilities.cs +++ b/src/Azure.Functions.Cli/Common/Utilities.cs @@ -263,5 +263,25 @@ internal static IConfigurationRoot BuildHostJsonConfigutation(ScriptApplicationH var configuration = builder.Build(); return configuration; } + + internal static bool IsMinifiedVersion() + { + IConfigurationBuilder builder = new ConfigurationBuilder(); + builder.AddJsonFile(Constants.ArtifactsConfigFileName, optional: true); + var config = builder.Build(); + + try + { + var section = config.GetSection(Constants.MinifiedVersionConfigSectionName); + + if (section.Exists()) + { + string value = section.Value; + return bool.TryParse(value, out bool isValue) && isValue; + } + } + catch { } + return false; + } } } diff --git a/test/Azure.Functions.Cli.Tests/UtilitiesTests.cs b/test/Azure.Functions.Cli.Tests/UtilitiesTests.cs index 098cdfd09..d64fdbb41 100644 --- a/test/Azure.Functions.Cli.Tests/UtilitiesTests.cs +++ b/test/Azure.Functions.Cli.Tests/UtilitiesTests.cs @@ -2,6 +2,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using System.Collections.Generic; +using System.IO; using Xunit; namespace Azure.Functions.Cli.Tests @@ -60,5 +61,20 @@ public void IsSystemLogCategory_Test(string inputCategory, bool expected) { Assert.Equal(expected, Utilities.IsSystemLogCategory(inputCategory)); } + + [Theory] + [InlineData(true)] + [InlineData(false)] + public void Test_IsMinifiedVersion(bool expected) + { + var filePath = Path.Combine("artifactsconfig.json"); + string artifactsJsonContent = "{\"minifiedVersion\": " + expected.ToString().ToLower() + "}"; + File.WriteAllTextAsync(filePath, artifactsJsonContent).GetAwaiter().GetResult(); + + bool output = Utilities.IsMinifiedVersion(); + + File.Delete(filePath); + Assert.Equal(expected, output); + } } }