diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md index 17d4678ce..2bf896fda 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/AnalyzerReleases.Unshipped.md @@ -1,2 +1,8 @@ ; Unshipped analyzer release ; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md + +### New Rules + +Rule ID | Category | Severity | Notes +--------|----------|----------|------- +AWSLambda0104 | AWSLambdaCSharpGenerator | Error | Missing reference to a required dependency \ No newline at end of file diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs index f4e03856f..7455e4e0b 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs @@ -1,6 +1,4 @@ using System; -using System.Reflection.Metadata; -using System.Runtime.InteropServices.ComTypes; using Microsoft.CodeAnalysis; namespace Amazon.Lambda.Annotations.SourceGenerator.Diagnostics @@ -10,7 +8,7 @@ public static class DiagnosticDescriptors /// Generic errors public static readonly DiagnosticDescriptor UnhandledException = new DiagnosticDescriptor(id: "AWSLambda0001", title: "Unhandled exception", - messageFormat: $"This is a bug. Please run the build with detailed verbosity (dotnet build --verbosity detailed) and file a bug at https://github.com/aws/aws-lambda-dotnet with the build output and stack trace {{0}}.", + messageFormat: "This is a bug. Please run the build with detailed verbosity (dotnet build --verbosity detailed) and file a bug at https://github.com/aws/aws-lambda-dotnet with the build output and stack trace {0}.", category: "AWSLambda", DiagnosticSeverity.Error, isEnabledByDefault: true, @@ -38,5 +36,13 @@ public static class DiagnosticDescriptors category: "AWSLambdaCSharpGenerator", DiagnosticSeverity.Info, isEnabledByDefault: true); + + public static readonly DiagnosticDescriptor MissingDependencies = new DiagnosticDescriptor(id: "AWSLambda0104", + title: "Missing reference to a required dependency", + messageFormat: "Your project has a missing required package dependency. Please add a reference to the following package: {0}", + category: "AWSLambdaCSharpGenerator", + DiagnosticSeverity.Error, + isEnabledByDefault: true); + } } \ No newline at end of file diff --git a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs index b8d7d62b9..f421ead98 100644 --- a/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs +++ b/Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Generator.cs @@ -74,6 +74,20 @@ public void Execute(GeneratorExecutionContext context) foreach (var lambdaMethod in receiver.LambdaMethods) { var lambdaMethodModel = semanticModelProvider.GetMethodSemanticModel(lambdaMethod); + + // Check for necessary references + if (lambdaMethodModel.HasAttribute(context, TypeFullNames.RestApiAttribute) + || lambdaMethodModel.HasAttribute(context, TypeFullNames.HttpApiAttribute)) + { + // Check for arbitrary type from "Amazon.Lambda.APIGatewayEvents" + if (context.Compilation.ReferencedAssemblyNames.FirstOrDefault(x => x.Name == "Amazon.Lambda.APIGatewayEvents") == null) + { + diagnosticReporter.Report(Diagnostic.Create(DiagnosticDescriptors.MissingDependencies, + lambdaMethod.GetLocation(), + "Amazon.Lambda.APIGatewayEvents")); + } + } + var model = LambdaFunctionModelBuilder.Build(lambdaMethodModel, configureMethodModel, context); // If there are more than one event, report them as errors