Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Azure.Functions.Sdk/Azure.Functions.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
<DevelopmentDependency>true</DevelopmentDependency>
<SuppressDependenciesWhenPacking>true</SuppressDependenciesWhenPacking>
<VersionPrefix>0.1.0</VersionPrefix>
<PolySharpExcludeGeneratedTypes>System.Runtime.CompilerServices.ModuleInitializerAttribute</PolySharpExcludeGeneratedTypes>
<PolySharpExcludeGeneratedTypes>System.Runtime.CompilerServices.ModuleInitializerAttribute;System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute</PolySharpExcludeGeneratedTypes>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" Version="22.0.15" />
</ItemGroup>

<ItemGroup>
<!-- These packages are provided by MSBuild / dotnet sdk at runtime, we want to reference but NOT include them. -->
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.11.48" ExcludeAssets="Runtime" />
<PackageReference Include="NuGet.ProjectModel" Version="6.14.0" ExcludeAssets="Runtime" />
</ItemGroup>
Expand Down
9 changes: 9 additions & 0 deletions src/Azure.Functions.Sdk/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

namespace Azure.Functions.Sdk;

internal static class Constants
{
public const string ExtensionsOutputFolder = ".azurefunctions";
}
48 changes: 48 additions & 0 deletions src/Azure.Functions.Sdk/ExceptionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Runtime.InteropServices;

namespace System;

internal static class ExceptionExtensions
{
/// <summary>
/// Determines whether the exception is considered fatal and should not be caught.
/// </summary>
/// <param name="exception">The exception to check.</param>
/// <returns></returns>
public static bool IsFatal(this Exception? exception)
{
while (exception is not null)
{
if (exception
is (OutOfMemoryException and not InsufficientMemoryException)
or AppDomainUnloadedException
or BadImageFormatException
or CannotUnloadAppDomainException
or InvalidProgramException
or AccessViolationException)
{
return true;
}

if (exception is AggregateException aggregate)
{
foreach (Exception inner in aggregate.InnerExceptions)
{
if (inner.IsFatal())
{
return true;
}
}
}
else
{
exception = exception.InnerException;
}
}

return false;
}
}
72 changes: 72 additions & 0 deletions src/Azure.Functions.Sdk/ExtensionReference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Diagnostics.CodeAnalysis;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Mono.Cecil;

namespace Azure.Functions.Sdk;

/// <summary>
/// Represents an Azure Functions extension reference.
/// </summary>
public static class ExtensionReference
{
private const string ExtensionsInformationType = "Microsoft.Azure.Functions.Worker.Extensions.Abstractions.ExtensionInformationAttribute";

/// <summary>
/// Gets the root path of the Azure Functions SDK module.
/// </summary>
/// <param name="path">The path to the assembly.</param>
/// <param name="sourcePackageId">The source package ID.</param>
/// <param name="extensionReference">The resulting extension reference, if found.</param>
public static bool TryGetFromModule(
string path,
string sourcePackageId,
[NotNullWhen(true)] out ITaskItem? extensionReference)
{
// Read the assembly from the specified path
AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(path);

// Find the extension attribute
foreach (CustomAttribute customAttribute in assembly.CustomAttributes)
{
if (string.Equals(
customAttribute.AttributeType.FullName,
ExtensionsInformationType,
StringComparison.Ordinal))
{
customAttribute.GetArguments(out string name, out string version);
extensionReference = new TaskItem(name);
extensionReference.SetVersion(version);
extensionReference.SetIsImplicitlyDefined(true);
extensionReference.SetSourcePackageId(sourcePackageId);

// We have two 'IsImplicitlyDefined' attributes, one for the package and one for the extension.
// IsImplicitlyDefined on the ITaskItem indicates the reference is from an extension package.
// IsImplicitlyDefined on the attribute indicates if the package can be trimmed or not.
if (!IsImplicitlyDefined(customAttribute))
{
// We can trim this reference if it is NOT implicitly defined.
// Trimming means we will exclude it if it is not used.
extensionReference.SetCanTrim(true);
}

return true;
}
}

extensionReference = default;
return false;
}

private static bool IsImplicitlyDefined(CustomAttribute customAttribute)
{
return customAttribute.ConstructorArguments.Count >= 3
&& bool.TryParse(
customAttribute.ConstructorArguments[2].Value?.ToString(),
out bool isImplicitlyDefined)
&& isImplicitlyDefined;
}
}
28 changes: 28 additions & 0 deletions src/Azure.Functions.Sdk/FunctionsAssemblyResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Immutable;
using Mono.Cecil;

namespace Azure.Functions.Sdk;

public class FunctionsAssemblyResolver : DefaultAssemblyResolver
{
private static readonly ImmutableHashSet<string> TrustedPlatformAssemblies
= GetTrustedPlatformAssemblies();

public override AssemblyDefinition? Resolve(AssemblyNameReference name)
{
// As soon as we get to a TPA we can stop. This helps prevent the odd circular reference
// with type forwarders as well.
AssemblyDefinition assemblyDef = base.Resolve(name);
return TrustedPlatformAssemblies.Contains(assemblyDef.MainModule.FileName) ? null : assemblyDef;
}

private static ImmutableHashSet<string> GetTrustedPlatformAssemblies()
{
object data = AppContext.GetData("TRUSTED_PLATFORM_ASSEMBLIES");
return data is null ? [] : data.ToString().Split(Path.PathSeparator)
.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);
}
}
69 changes: 69 additions & 0 deletions src/Azure.Functions.Sdk/FunctionsAssemblyScanner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.IO.Abstractions;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Mono.Cecil;
using ILogger = NuGet.Common.ILogger;

namespace Azure.Functions.Sdk;

public sealed partial class FunctionsAssemblyScanner
{
private static readonly Regex ExcludedPackagesRegex = new(
@"^(System|Azure\.Core|Azure\.Identity|Microsoft\.Bcl|Microsoft\.Extensions|Microsoft\.Identity|Microsoft\.NETCore|Microsoft\.NETStandard|Microsoft\.Win32|Grpc)\..*",
RegexOptions.Compiled);

private readonly IFileSystem _fileSystem;
private readonly ReaderParameters _readerParameters;
private readonly FunctionsAssemblyResolver _resolver;

public FunctionsAssemblyScanner(
IFileSystem? fileSystem = null,
IEnumerable<string>? searchDirectories = null)
{
_resolver = new();
_readerParameters = new ReaderParameters
{
AssemblyResolver = _resolver,
};

_fileSystem = fileSystem ?? new FileSystem();
if (searchDirectories != null)
{
foreach (string directory in searchDirectories)
{
_resolver.AddSearchDirectory(directory);
}
}
}

public static FunctionsAssemblyScanner FromTaskItems(
IEnumerable<ITaskItem> items, IFileSystem? fileSystem = null)
{
IEnumerable<string> searchDirectories = items
.Select(item => Path.GetDirectoryName(item.ItemSpec))
.Where(dir => !string.IsNullOrEmpty(dir))
.Distinct();

return new(fileSystem, searchDirectories);
}

public static bool IsExcludedPackage(string name)
{
return string.IsNullOrEmpty(name) || ExcludedPackagesRegex.IsMatch(name);
}

public IEnumerable<WebJobsReference> GetWebJobsReferences(string assembly, ILogger? logger = null)
{
AssemblyDefinition definition = ReadAssembly(assembly);
return WebJobsReference.FromModule(definition, logger);
}

private AssemblyDefinition ReadAssembly(string assemblyPath)
{
Throw.IfNullOrEmpty(assemblyPath);
return AssemblyDefinition.ReadAssembly(assemblyPath, _readerParameters);
}
}
17 changes: 17 additions & 0 deletions src/Azure.Functions.Sdk/LockFileExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using NuGet.Packaging;
using NuGet.ProjectModel;

namespace Azure.Functions.Sdk;

public static class LockFileExtensions
{
public static FallbackPackagePathResolver GetPathResolver(this LockFile lockFile)
{
Throw.IfNull(lockFile);
string? userPackageFolder = lockFile.PackageFolders.FirstOrDefault()?.Path;
return new(userPackageFolder, lockFile.PackageFolders.Skip(1).Select(folder => folder.Path));
}
}
Loading