Skip to content

Fix CS0121 ambiguity in generated Program.g.cs for no-input Task handlers#2384

Open
GarrettBeatty wants to merge 2 commits into
devfrom
fix/annotations-cs0121-no-input-task-ambiguity
Open

Fix CS0121 ambiguity in generated Program.g.cs for no-input Task handlers#2384
GarrettBeatty wants to merge 2 commits into
devfrom
fix/annotations-cs0121-no-input-task-ambiguity

Conversation

@GarrettBeatty
Copy link
Copy Markdown
Contributor

Summary

Fixes #1907

When a Lambda handler has no input parameters and returns Task (e.g., public async Task FunctionHandler()), the Annotations source generator produced ambiguous code in Program.g.cs:

Func<Stream, Task> handler = new ...().FunctionHandler;
await LambdaBootstrapBuilder.Create(handler, new Serializer()).Build().RunAsync();

Func<Stream, Task> matches two overloads, causing CS0121:

  • Create<TInput>(Func<TInput, Task>, ILambdaSerializer)TInput = Stream
  • Create<TOutput>(Func<Stream, TOutput>, ILambdaSerializer)TOutput = Task

Fix

In ExecutableAssembly.tt/.cs, detect this specific case (no typed input + Task return) and use the non-generic, non-serializer overload Create(Func<Stream, Task>), which is unambiguous and correct since no deserialization is needed when there is no typed input:

Func<Stream, System.Threading.Tasks.Task> handler = new ...().FunctionHandler;
await LambdaBootstrapBuilder.Create(handler).Build().RunAsync();

Testing

Added VerifyExecutableAssemblyWithParameterlessTaskReturnConstructor snapshot test covering the exact reproduction case from the issue.

GarrettBeatty and others added 2 commits May 17, 2026 21:46
…lers

When a Lambda handler has no input parameters and returns Task (e.g.,
'public async Task FunctionHandler()'), the source generator was producing:

  Func<Stream, Task> handler = ...;
  LambdaBootstrapBuilder.Create(handler, serializer)...

Func<Stream, Task> is ambiguous between two overloads:
  - Create<TInput>(Func<TInput, Task>, ILambdaSerializer)  (TInput=Stream)
  - Create<TOutput>(Func<Stream, TOutput>, ILambdaSerializer) (TOutput=Task)

Fix: detect this specific case in ExecutableAssembly.tt/.cs and use the
non-generic, non-serializer overload Create(Func<Stream, Task>) which is
unambiguous and correct (no deserialization needed with no typed input).

Fixes #1907

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@GarrettBeatty GarrettBeatty marked this pull request as ready for review May 18, 2026 02:35
@GarrettBeatty GarrettBeatty requested review from a team as code owners May 18, 2026 02:36
@GarrettBeatty GarrettBeatty requested review from normj and philasmar May 18, 2026 02:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the Annotations source generator so executable Lambda projects with parameterless Task handlers generate an unambiguous LambdaBootstrapBuilder.Create call, resolving CS0121 for the issue reproduction.

Changes:

  • Adds a template branch for parameterless Task handlers to use Create(Func<Stream, Task>) without a serializer.
  • Updates the generated T4 backing class accordingly.
  • Adds a regression source file, snapshots, and a verifier test for the parameterless async handler case.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/ExecutableAssembly.tt Adds the unambiguous bootstrap generation path.
Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Templates/ExecutableAssembly.cs Updates generated T4 implementation for the template change.
Libraries/test/TestExecutableServerlessApp/ParameterlessTaskMethods.cs Adds a parameterless Task handler test input.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/SourceGeneratorTests.cs Adds the regression verification test.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ProgramParameterlessTask.g.cs Captures expected generated executable program output.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ParameterlessTaskMethods_NoParameterTask_Generated.g.cs Captures expected generated handler wrapper output.
Libraries/test/Amazon.Lambda.Annotations.SourceGenerators.Tests/Snapshots/ServerlessTemplates/parameterlesstask.template Captures expected serverless template output.
.autover/changes/23b26fb3-e30b-47a7-8a8e-3d8504420311.json Adds patch changelog metadata.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants