Skip to content

sync dev and master #2015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Closed
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## Release 2025-03-13

### Amazon.Lambda.Annotations (1.7.0)
* Add ConfigureHostBuilder function to `Startup` class for allowing the Lambda function to configure an `IHostApplicationBuilder`.
### Amazon.Lambda.RuntimeSupport (1.13.0)
* Add support for parameterized logging method to global logger LambdaLogger in Amazon.Lambda.Core
### Amazon.Lambda.Core (2.5.1)
* Add support for parameterized logging method to global logger LambdaLogger. Method is marked as preview till new version of Amazon.Lambda.RuntimeSupport is deployed to managed runtime.

## Release 2025-03-05

### Amazon.Lambda.TestTool (0.9.1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<IncludeBuildOutput>false</IncludeBuildOutput>

<Version>1.6.3</Version>
<Version>1.7.0</Version>
</PropertyGroup>

<!-- Delay setting the package id till after resolving dependencies to avoid ambigious errors. -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

## Release 1.7.0
### New Rules
Rule ID | Category | Severity | Notes
--------|----------|----------|-------
AWSLambda0119 | AWSLambdaCSharpGenerator | Error | Conflicting Service Configuration Methods Detected

## Release 1.5.1
### New Rules

Expand Down Expand Up @@ -79,4 +85,4 @@ Rule ID | Category | Severity | Notes
AWSLambda0001 | AWSLambda | Error | Unhandled exception
AWSLambda0101 | AWSLambdaCSharpGenerator | Error | Multiple LambdaStartup classes not allowed
AWSLambda0102 | AWSLambdaCSharpGenerator | Error | Multiple events on Lambda function not supported
AWSLambda0103 | AWSLambdaCSharpGenerator | Info | Generated code
AWSLambda0103 | AWSLambdaCSharpGenerator | Info | Generated code
Original file line number Diff line number Diff line change
Expand Up @@ -145,5 +145,13 @@ public static class DiagnosticDescriptors
category: "AWSLambdaCSharpGenerator",
DiagnosticSeverity.Error,
isEnabledByDefault: true);

public static readonly DiagnosticDescriptor MultipleConfigureMethodsNotAllowed = new DiagnosticDescriptor(
id: "AWSLambda0119",
title: "Conflicting Service Configuration Methods Detected",
messageFormat: "Both '{0}' and '{1}' methods are present. Only {1} will be used. Please consolidate your service configuration into {1}.",
category: "AWSLambdaCSharpGenerator",
DiagnosticSeverity.Error,
isEnabledByDefault: true);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Amazon.Lambda.Annotations.SourceGenerator.Diagnostics;
using Amazon.Lambda.Annotations.SourceGenerator.Diagnostics;
using Amazon.Lambda.Annotations.SourceGenerator.Extensions;
using Amazon.Lambda.Annotations.SourceGenerator.FileIO;
using Amazon.Lambda.Annotations.SourceGenerator.Models;
Expand Down Expand Up @@ -93,9 +93,9 @@ public void Execute(GeneratorExecutionContext context)
var isExecutable = false;

bool foundFatalError = false;

var assemblyAttributes = context.Compilation.Assembly.GetAttributes();

var globalPropertiesAttribute = assemblyAttributes
.FirstOrDefault(attr => attr.AttributeClass.Name == nameof(LambdaGlobalPropertiesAttribute));

Expand All @@ -109,7 +109,7 @@ public void Execute(GeneratorExecutionContext context)
defaultRuntime = _targetFrameworksToRuntimes[targetFramework];
}
}

// The runtime specified in the global property has precedence over the one we determined from the TFM (if we did)
if (globalPropertiesAttribute != null)
{
Expand All @@ -134,14 +134,26 @@ public void Execute(GeneratorExecutionContext context)
}
}

var configureMethodSymbol = semanticModelProvider.GetConfigureMethodModel(receiver.StartupClasses.FirstOrDefault());
var configureHostBuilderMethodSymbol = semanticModelProvider.GetConfigureHostBuilderMethodModel(receiver.StartupClasses.FirstOrDefault());
var configureServicesMethodSymbol = semanticModelProvider.GetConfigureServicesMethodModel(receiver.StartupClasses.FirstOrDefault());
var configureMethodSymbol = configureServicesMethodSymbol;

if (configureServicesMethodSymbol != null && configureHostBuilderMethodSymbol != null)
{
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MultipleConfigureMethodsNotAllowed, configureServicesMethodSymbol.Locations.FirstOrDefault(), configureServicesMethodSymbol.Name, configureHostBuilderMethodSymbol.Name));
}

if (configureHostBuilderMethodSymbol != null)
{
configureMethodSymbol = configureHostBuilderMethodSymbol;
}

var annotationReport = new AnnotationReport();

var templateHandler = new CloudFormationTemplateHandler(_fileManager, _directoryManager);

var lambdaModels = new List<LambdaFunctionModel>();

foreach (var lambdaMethodDeclarationSyntax in receiver.LambdaMethods)
{
var lambdaMethodSymbol = semanticModelProvider.GetMethodSemanticModel(lambdaMethodDeclarationSyntax);
Expand All @@ -163,7 +175,7 @@ public void Execute(GeneratorExecutionContext context)
sourceText = template.TransformText().ToEnvironmentLineEndings();
context.AddSource($"{lambdaFunctionModel.GeneratedMethod.ContainingType.Name}.g.cs", SourceText.From(sourceText, Encoding.UTF8, SourceHashAlgorithm.Sha256));
}
catch (Exception e) when (e is NotSupportedException || e is InvalidOperationException)
catch (Exception e) when (e is NotSupportedException || e is InvalidOperationException)
{
diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.CodeGenerationFailed, Location.Create(lambdaMethodDeclarationSyntax.SyntaxTree, lambdaMethodDeclarationSyntax.Span), e.Message));
return;
Expand Down Expand Up @@ -241,7 +253,7 @@ private static ExecutableAssembly GenerateExecutableAssemblySource(
DiagnosticDescriptors.MissingDependencies,
Location.None,
"Amazon.Lambda.RuntimeSupport"));

return null;
}

Expand All @@ -251,14 +263,14 @@ private static ExecutableAssembly GenerateExecutableAssemblySource(
var symbol = model.GetDeclaredSymbol(methodDeclaration) as IMethodSymbol;

// Check to see if a static main method exists in the same namespace that has 0 or 1 parameters
if (symbol.Name != "Main" || !symbol.IsStatic || symbol.ContainingNamespace.Name != lambdaModels[0].LambdaMethod.ContainingAssembly || (symbol.Parameters.Length > 1))
if (symbol.Name != "Main" || !symbol.IsStatic || symbol.ContainingNamespace.Name != lambdaModels[0].LambdaMethod.ContainingAssembly || (symbol.Parameters.Length > 1))
continue;

diagnosticReporter.Report(
Diagnostic.Create(
DiagnosticDescriptors.MainMethodExists,
Location.None));

return null;
}

Expand All @@ -284,4 +296,4 @@ public void Initialize(GeneratorInitializationContext context)
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver(_fileManager, _directoryManager));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static GeneratedMethodModel Build(IMethodSymbol lambdaMethodSymbol,
return model;
}

private static IList<string> BuildUsings(LambdaMethodModel lambdaMethodModel,
private static IList<string> BuildUsings(LambdaMethodModel lambdaMethodModel,
IMethodSymbol lambdaMethodSymbol,
IMethodSymbol configureMethodSymbol,
GeneratorExecutionContext context)
Expand All @@ -45,6 +45,12 @@ private static IList<string> BuildUsings(LambdaMethodModel lambdaMethodModel,
if (configureMethodSymbol != null)
{
namespaces.Add("Microsoft.Extensions.DependencyInjection");

if (lambdaMethodModel.UsingHostBuilderForDependencyInjection)
{
namespaces.Add("Microsoft.Extensions.Hosting");
}

}

namespaces.Add("Amazon.Lambda.Core");
Expand Down Expand Up @@ -73,7 +79,7 @@ private static TypeModel BuildResponseType(IMethodSymbol lambdaMethodSymbol,
return TypeModelBuilder.Build(typeStream, context);
}


if (lambdaMethodSymbol.HasAttribute(context, TypeFullNames.RestApiAttribute))
{
var symbol = lambdaMethodModel.ReturnsVoidOrGenericTask ?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,4 @@ private static LambdaSerializerInfo GetSerializerInfoAttribute(GeneratorExecutio
return new LambdaSerializerInfo(serializerString);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public bool ReturnsIHttpResults
{
return true;
}
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult)
if(ReturnsGenericTask && ReturnType.TypeArguments.Count == 1 && ReturnType.TypeArguments[0].FullName == TypeFullNames.IHttpResult)
{
return true;
}
Expand Down Expand Up @@ -100,6 +100,8 @@ public bool ReturnsVoidTaskOrSqsBatchResponse
/// </summary>
public bool UsingDependencyInjection { get; set; }

public bool UsingHostBuilderForDependencyInjection { get; set; }

/// <summary>
/// Gets or sets the namespace for the nearest enclosing namespace. Returns null if the
/// symbol isn't contained in a namespace.
Expand Down Expand Up @@ -151,4 +153,4 @@ public AttributeModel<LambdaFunctionAttribute> LambdaFunctionAttribute
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public static LambdaMethodModel Build(IMethodSymbol lambdaMethodSymbol,
Events = EventTypeBuilder.Build(lambdaMethodSymbol, context),
ContainingType = TypeModelBuilder.Build(lambdaMethodSymbol.ContainingType, context),
UsingDependencyInjection = configureMethodSymbol != null,
UsingHostBuilderForDependencyInjection = configureMethodSymbol != null && configureMethodSymbol.Name == "ConfigureHostBuilder",
Attributes = lambdaMethodSymbol.GetAttributes().Select(att => AttributeModelBuilder.Build(att, context)).ToList()
};

return model;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public SemanticModelProvider(GeneratorExecutionContext context)
/// If <see cref="startupSyntax"/> is null, returns null.
/// </summary>
/// <param name="startupSyntax">LambdaStartup attributed class syntax</param>
public IMethodSymbol GetConfigureMethodModel(ClassDeclarationSyntax startupSyntax)
public IMethodSymbol GetConfigureServicesMethodModel(ClassDeclarationSyntax startupSyntax)
{
if (startupSyntax == null)
{
return null;
}

IMethodSymbol configureMethodSymbol = null;
IMethodSymbol configureServicesMethodSymbol = null;

var iServiceCollectionSymbol = _context.Compilation.GetTypeByMetadataName("Microsoft.Extensions.DependencyInjection.IServiceCollection");

Expand All @@ -36,7 +36,6 @@ public IMethodSymbol GetConfigureMethodModel(ClassDeclarationSyntax startupSynta
// Filter the methods which can potentially have Configure(IServiceCollection) parameter signature
var members = startupSyntax.Members.Where(member => member.Kind() == SyntaxKind.MethodDeclaration);


foreach (var member in members)
{
var methodSyntax = (MethodDeclarationSyntax) member;
Expand All @@ -47,12 +46,37 @@ public IMethodSymbol GetConfigureMethodModel(ClassDeclarationSyntax startupSynta
&& methodSymbol.Parameters[0].Type
.Equals(iServiceCollectionSymbol, SymbolEqualityComparer.Default))
{
configureMethodSymbol = methodSymbol;
configureServicesMethodSymbol = methodSymbol;
break;
}
}

return configureServicesMethodSymbol;
}

public IMethodSymbol GetConfigureHostBuilderMethodModel(ClassDeclarationSyntax startupSyntax)
{
if (startupSyntax == null)
{
return null;
}

IMethodSymbol configureHostBuilderMethodSymbol = null;

var classModel = _context.Compilation.GetSemanticModel(startupSyntax.SyntaxTree);

foreach (var member in startupSyntax.Members.Where(member => member.Kind() == SyntaxKind.MethodDeclaration))
{
var methodSyntax = (MethodDeclarationSyntax)member;
var methodSymbol = classModel.GetDeclaredSymbol(methodSyntax);
if (methodSymbol != null && methodSymbol.Name == "ConfigureHostBuilder")
{
configureHostBuilderMethodSymbol = methodSymbol;
break;
}
}

return configureMethodSymbol;
return configureHostBuilderMethodSymbol;
}

/// <summary>
Expand All @@ -65,4 +89,4 @@ public IMethodSymbol GetMethodSemanticModel(MethodDeclarationSyntax syntax)
return methodModel.GetDeclaredSymbol(syntax);
}
}
}
}
Loading
Loading