Skip to content

Commit

Permalink
Updating coretools to detect unsupported inproc6 sdk scenario (#3825)
Browse files Browse the repository at this point in the history
  • Loading branch information
surgupta-msft authored and fabiocav committed Nov 6, 2024
1 parent b774dc0 commit 1b49034
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
8 changes: 8 additions & 0 deletions build/BuildSteps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public static void DotnetPublishForZips()
if (isMinVersion)
{
RemoveLanguageWorkers(outputPath);
CreateMinConfigurationFile(outputPath);
}

// Publish net8 version of the artifact as well.
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions host/src/CoreToolsHost/CoreToolsHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<PublishAot>true</PublishAot>
<!--Starting with .NET 9, CET is enabled by default (https://github.com/dotnet/docs/issues/42600).
However, debugger support for CET was introduced in .NET 7, so we disable CET here to ensure
compatibility with the .NET 6 runtime. Otherwise, crashes may occur when debugging .NET 6 apps
in environments where CET is supported/enabled. -->
<CETCompat>false</CETCompat>
<OptimizationPreference>Speed</OptimizationPreference>
<AssemblyName>func</AssemblyName>
<IlcExportUnmanagedEntrypoints>true</IlcExportUnmanagedEntrypoints>
Expand Down
15 changes: 15 additions & 0 deletions src/Azure.Functions.Cli/Actions/HostActions/StartHostAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,14 @@ private async Task<bool> 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;
}

Expand All @@ -488,6 +494,11 @@ private async Task<bool> TryHandleInProcDotNetLaunchAsync()

PrintVerboseForHostSelection(selectedRuntime);

if (Utilities.IsMinifiedVersion())
{
ThrowForInProc();
}

StartHostAsChildProcess(isDotNet8Project);

return true;
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 2 additions & 0 deletions src/Azure.Functions.Cli/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions src/Azure.Functions.Cli/Common/DotnetConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
20 changes: 20 additions & 0 deletions src/Azure.Functions.Cli/Common/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
16 changes: 16 additions & 0 deletions test/Azure.Functions.Cli.Tests/UtilitiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 1b49034

Please sign in to comment.