Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 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
3 changes: 3 additions & 0 deletions src/Generators/AnalyzerReleases.Shipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
; Shipped analyzer releases
; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

9 changes: 9 additions & 0 deletions src/Generators/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn/blob/main/src/RoslynAnalyzers/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
DURABLE3001 | DurableTask.Design | Error | **DurableTaskSourceGenerator**: Reports when a task name in [DurableTask] attribute is not a valid C# identifier. Task names must start with a letter or underscore and contain only letters, digits, and underscores.
DURABLE3002 | DurableTask.Design | Error | **DurableTaskSourceGenerator**: Reports when an event name in [DurableEvent] attribute is not a valid C# identifier. Event names must start with a letter or underscore and contain only letters, digits, and underscores.
173 changes: 154 additions & 19 deletions src/Generators/DurableTaskSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -39,6 +40,32 @@ public class DurableTaskSourceGenerator : IIncrementalGenerator
* }
*/

/// <summary>
/// Diagnostic ID for invalid task names.
/// </summary>
const string InvalidTaskNameDiagnosticId = "DURABLE3001";

/// <summary>
/// Diagnostic ID for invalid event names.
/// </summary>
const string InvalidEventNameDiagnosticId = "DURABLE3002";

static readonly DiagnosticDescriptor InvalidTaskNameRule = new(
InvalidTaskNameDiagnosticId,
title: "Invalid task name",
messageFormat: "The task name '{0}' is not a valid C# identifier. Task names must start with a letter or underscore and contain only letters, digits, and underscores.",
category: "DurableTask.Design",
DiagnosticSeverity.Error,
isEnabledByDefault: true);

static readonly DiagnosticDescriptor InvalidEventNameRule = new(
InvalidEventNameDiagnosticId,
title: "Invalid event name",
messageFormat: "The event name '{0}' is not a valid C# identifier. Event names must start with a letter or underscore and contain only letters, digits, and underscores.",
category: "DurableTask.Design",
DiagnosticSeverity.Error,
isEnabledByDefault: true);

/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
Expand All @@ -63,22 +90,32 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
transform: static (ctx, _) => GetDurableFunction(ctx))
.Where(static func => func != null)!;

// Get the project type configuration from MSBuild properties
IncrementalValueProvider<string?> projectTypeProvider = context.AnalyzerConfigOptionsProvider
.Select(static (provider, _) =>
{
provider.GlobalOptions.TryGetValue("build_property.DurableTaskGeneratorProjectType", out string? projectType);
return projectType;
});

// Collect all results and check if Durable Functions is referenced
IncrementalValueProvider<(Compilation, ImmutableArray<DurableTaskTypeInfo>, ImmutableArray<DurableEventTypeInfo>, ImmutableArray<DurableFunction>)> compilationAndTasks =
IncrementalValueProvider<(Compilation, ImmutableArray<DurableTaskTypeInfo>, ImmutableArray<DurableEventTypeInfo>, ImmutableArray<DurableFunction>, string?)> compilationAndTasks =
durableTaskAttributes.Collect()
.Combine(durableEventAttributes.Collect())
.Combine(durableFunctions.Collect())
.Combine(context.CompilationProvider)
.Combine(projectTypeProvider)
// Roslyn's IncrementalValueProvider.Combine creates nested tuple pairs: ((Left, Right), Right)
// After multiple .Combine() calls, we unpack the nested structure:
// x.Right = Compilation
// x.Left.Left.Left = DurableTaskAttributes (orchestrators, activities, entities)
// x.Left.Left.Right = DurableEventAttributes (events)
// x.Left.Right = DurableFunctions (Azure Functions metadata)
.Select((x, _) => (x.Right, x.Left.Left.Left, x.Left.Left.Right, x.Left.Right));
// x.Right = projectType (string?)
// x.Left.Right = Compilation
// x.Left.Left.Left.Left = DurableTaskAttributes (orchestrators, activities, entities)
// x.Left.Left.Left.Right = DurableEventAttributes (events)
// x.Left.Left.Right = DurableFunctions (Azure Functions metadata)
.Select((x, _) => (x.Left.Right, x.Left.Left.Left.Left, x.Left.Left.Left.Right, x.Left.Left.Right, x.Right));

// Generate the source
context.RegisterSourceOutput(compilationAndTasks, static (spc, source) => Execute(spc, source.Item1, source.Item2, source.Item3, source.Item4));
context.RegisterSourceOutput(compilationAndTasks, static (spc, source) => Execute(spc, source.Item1, source.Item2, source.Item3, source.Item4, source.Item5));
}

static DurableTaskTypeInfo? GetDurableTaskTypeInfo(GeneratorSyntaxContext context)
Expand Down Expand Up @@ -166,13 +203,15 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
ITypeSymbol? outputType = kind == DurableTaskKind.Entity ? null : taskType.TypeArguments.Last();

string taskName = classType.Name;
Location? taskNameLocation = null;
if (attribute.ArgumentList?.Arguments.Count > 0)
{
ExpressionSyntax expression = attribute.ArgumentList.Arguments[0].Expression;
taskName = context.SemanticModel.GetConstantValue(expression).ToString();
taskNameLocation = expression.GetLocation();
}

return new DurableTaskTypeInfo(className, taskName, inputType, outputType, kind);
return new DurableTaskTypeInfo(className, taskName, inputType, outputType, kind, taskNameLocation);
}

static DurableEventTypeInfo? GetDurableEventTypeInfo(GeneratorSyntaxContext context)
Expand Down Expand Up @@ -204,6 +243,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
}

string eventName = eventType.Name;
Location? eventNameLocation = null;

if (attribute.ArgumentList?.Arguments.Count > 0)
{
Expand All @@ -212,10 +252,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
if (constantValue.HasValue && constantValue.Value is string value)
{
eventName = value;
eventNameLocation = expression.GetLocation();
}
}

return new DurableEventTypeInfo(eventName, eventType);
return new DurableEventTypeInfo(eventName, eventType, eventNameLocation);
}

static DurableFunction? GetDurableFunction(GeneratorSyntaxContext context)
Expand All @@ -230,29 +271,70 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
return null;
}

/// <summary>
/// Checks if a name is a valid C# identifier.
/// </summary>
/// <param name="name">The name to validate.</param>
/// <returns>True if the name is a valid C# identifier, false otherwise.</returns>
static bool IsValidCSharpIdentifier(string name)
{
if (string.IsNullOrEmpty(name))
{
return false;
}

// Use Roslyn's built-in identifier validation
return SyntaxFacts.IsValidIdentifier(name);
}

static void Execute(
SourceProductionContext context,
Compilation compilation,
ImmutableArray<DurableTaskTypeInfo> allTasks,
ImmutableArray<DurableEventTypeInfo> allEvents,
ImmutableArray<DurableFunction> allFunctions)
ImmutableArray<DurableFunction> allFunctions,
string? projectType)
{
if (allTasks.IsDefaultOrEmpty && allEvents.IsDefaultOrEmpty && allFunctions.IsDefaultOrEmpty)
{
return;
}

// This generator also supports Durable Functions for .NET isolated, but we only generate Functions-specific
// code if we find the Durable Functions extension listed in the set of referenced assembly names.
bool isDurableFunctions = compilation.ReferencedAssemblyNames.Any(
assembly => assembly.Name.Equals("Microsoft.Azure.Functions.Worker.Extensions.DurableTask", StringComparison.OrdinalIgnoreCase));
// Validate task names and report diagnostics for invalid identifiers
foreach (DurableTaskTypeInfo task in allTasks)
{
if (!IsValidCSharpIdentifier(task.TaskName))
{
Location location = task.TaskNameLocation ?? Location.None;
Diagnostic diagnostic = Diagnostic.Create(InvalidTaskNameRule, location, task.TaskName);
context.ReportDiagnostic(diagnostic);
}
}

// Validate event names and report diagnostics for invalid identifiers
foreach (DurableEventTypeInfo eventInfo in allEvents)
{
if (!IsValidCSharpIdentifier(eventInfo.EventName))
{
Location location = eventInfo.EventNameLocation ?? Location.None;
Diagnostic diagnostic = Diagnostic.Create(InvalidEventNameRule, location, eventInfo.EventName);
context.ReportDiagnostic(diagnostic);
}
}

// Determine if we should generate Durable Functions specific code
bool isDurableFunctions = DetermineIsDurableFunctions(compilation, allFunctions, projectType);

// Separate tasks into orchestrators, activities, and entities
// Skip tasks with invalid names to avoid generating invalid code
List<DurableTaskTypeInfo> orchestrators = new();
List<DurableTaskTypeInfo> activities = new();
List<DurableTaskTypeInfo> entities = new();

foreach (DurableTaskTypeInfo task in allTasks)
IEnumerable<DurableTaskTypeInfo> validTasks = allTasks
.Where(task => IsValidCSharpIdentifier(task.TaskName));

foreach (DurableTaskTypeInfo task in validTasks)
{
if (task.IsActivity)
{
Expand All @@ -268,7 +350,12 @@ static void Execute(
}
}

int found = activities.Count + orchestrators.Count + entities.Count + allEvents.Length + allFunctions.Length;
// Filter out events with invalid names
List<DurableEventTypeInfo> validEvents = allEvents
.Where(eventInfo => IsValidCSharpIdentifier(eventInfo.EventName))
.ToList();

int found = activities.Count + orchestrators.Count + entities.Count + validEvents.Count + allFunctions.Length;
if (found == 0)
{
return;
Expand Down Expand Up @@ -347,7 +434,7 @@ public static class GeneratedDurableTaskExtensions
}

// Generate WaitFor{EventName}Async methods for each event type
foreach (DurableEventTypeInfo eventInfo in allEvents)
foreach (DurableEventTypeInfo eventInfo in validEvents)
{
AddEventWaitMethod(sourceBuilder, eventInfo);
AddEventSendMethod(sourceBuilder, eventInfo);
Expand Down Expand Up @@ -381,6 +468,49 @@ public static class GeneratedDurableTaskExtensions
context.AddSource("GeneratedDurableTaskExtensions.cs", SourceText.From(sourceBuilder.ToString(), Encoding.UTF8, SourceHashAlgorithm.Sha256));
}

/// <summary>
/// Determines whether the current project should be treated as an Azure Functions-based Durable Functions project.
/// </summary>
/// <param name="compilation">The Roslyn compilation for the project, used to inspect referenced assemblies.</param>
/// <param name="allFunctions">The collection of discovered Durable Functions triggers in the project.</param>
/// <param name="projectType">
/// An optional project type hint. When set to <c>"Functions"</c> or <c>"Standalone"</c>, this value takes precedence
/// over automatic detection. Any other value (including <c>"Auto"</c>) falls back to auto-detection.
/// </param>
/// <returns>
/// <c>true</c> if the project is determined to be a Durable Functions (Azure Functions) project; otherwise, <c>false</c>.
/// </returns>
static bool DetermineIsDurableFunctions(Compilation compilation, ImmutableArray<DurableFunction> allFunctions, string? projectType)
{
// Check if the user has explicitly configured the project type
if (!string.IsNullOrWhiteSpace(projectType))
{
// Explicit configuration takes precedence
if (projectType!.Equals("Functions", StringComparison.OrdinalIgnoreCase))
{
return true;
}
else if (projectType.Equals("Standalone", StringComparison.OrdinalIgnoreCase))
{
return false;
}
// If "Auto" or unrecognized value, fall through to auto-detection
}

// Auto-detect based on the presence of Azure Functions trigger attributes
// If we found any methods with OrchestrationTrigger, ActivityTrigger, or EntityTrigger attributes,
// then this is a Durable Functions project
if (!allFunctions.IsDefaultOrEmpty)
{
return true;
}

// Fallback: check if Durable Functions assembly is referenced
// This handles edge cases where the project references the assembly but hasn't defined triggers yet
return compilation.ReferencedAssemblyNames.Any(
assembly => assembly.Name.Equals("Microsoft.Azure.Functions.Worker.Extensions.DurableTask", StringComparison.OrdinalIgnoreCase));
}

static void AddOrchestratorFunctionDeclaration(StringBuilder sourceBuilder, DurableTaskTypeInfo orchestrator)
{
sourceBuilder.AppendLine($@"
Expand Down Expand Up @@ -588,11 +718,13 @@ public DurableTaskTypeInfo(
string taskName,
ITypeSymbol? inputType,
ITypeSymbol? outputType,
DurableTaskKind kind)
DurableTaskKind kind,
Location? taskNameLocation = null)
{
this.TypeName = taskType;
this.TaskName = taskName;
this.Kind = kind;
this.TaskNameLocation = taskNameLocation;

// Entities only have a state type parameter, not input/output
if (kind == DurableTaskKind.Entity)
Expand Down Expand Up @@ -620,6 +752,7 @@ public DurableTaskTypeInfo(
public string InputParameter { get; }
public string OutputType { get; }
public DurableTaskKind Kind { get; }
public Location? TaskNameLocation { get; }

public bool IsActivity => this.Kind == DurableTaskKind.Activity;

Expand Down Expand Up @@ -647,14 +780,16 @@ static string GetRenderedTypeExpression(ITypeSymbol? symbol)

class DurableEventTypeInfo
{
public DurableEventTypeInfo(string eventName, ITypeSymbol eventType)
public DurableEventTypeInfo(string eventName, ITypeSymbol eventType, Location? eventNameLocation = null)
{
this.TypeName = GetRenderedTypeExpression(eventType);
this.EventName = eventName;
this.EventNameLocation = eventNameLocation;
}

public string TypeName { get; }
public string EventName { get; }
public Location? EventNameLocation { get; }

static string GetRenderedTypeExpression(ITypeSymbol? symbol)
{
Expand Down
60 changes: 60 additions & 0 deletions src/Generators/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
Source generators for `Microsoft.DurableTask`

## Overview

The `Microsoft.DurableTask.Generators` package provides source generators that automatically generate type-safe extension methods for orchestrators and activities. The generator automatically detects whether you're using Azure Functions or the Durable Task Scheduler and generates appropriate code for your environment.

## Configuration

### Project Type Detection

The generator uses intelligent automatic detection to determine the project type:

1. **Primary Detection**: Checks for Azure Functions trigger attributes (`OrchestrationTrigger`, `ActivityTrigger`, `EntityTrigger`) in your code
- If any methods use these trigger attributes, it generates Azure Functions-specific code
- Otherwise, it generates standalone Durable Task Worker code

2. **Fallback Detection**: If no trigger attributes are found, checks if `Microsoft.Azure.Functions.Worker.Extensions.DurableTask` is referenced
- This handles projects that reference the Functions package but haven't defined triggers yet

This automatic detection solves the common issue where transitive dependencies on Functions packages would incorrectly trigger Functions mode even when not using Azure Functions.

### Explicit Project Type Configuration (Optional)

In rare scenarios where you need to override the automatic detection, you can explicitly configure the project type using the `DurableTaskGeneratorProjectType` MSBuild property in your `.csproj` file:

```xml
<PropertyGroup>
<DurableTaskGeneratorProjectType>Standalone</DurableTaskGeneratorProjectType>
</PropertyGroup>
```

#### Supported Values

- `Auto` (default): Automatically detects project type using the intelligent detection described above
- `Functions`: Forces generation of Azure Functions-specific code
- `Standalone`: Forces generation of standalone Durable Task Worker code (includes `AddAllGeneratedTasks` method)

#### Example: Force Standalone Mode

```xml
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DurableTaskGeneratorProjectType>Standalone</DurableTaskGeneratorProjectType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.DurableTask.Generators" OutputItemType="Analyzer" />
<!-- Your other package references -->
</ItemGroup>
</Project>
```

With standalone mode, the generator produces the `AddAllGeneratedTasks` extension method for worker registration:

```csharp
builder.Services.AddDurableTaskWorker(builder =>
{
builder.AddTasks(r => r.AddAllGeneratedTasks());
});
```

For more information, see https://github.com/microsoft/durabletask-dotnet
Loading
Loading