Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ Rule ID | Category | Severity | Notes
--------|----------|----------|--------------------
DAPR1303 | Usage | Warning | The provided input type does not match the target workflow or activity input type
DAPR1304 | Usage | Warning | The requested output type does not match the target workflow or activity output type
DAPR1305 | Usage | Warning | Workflow implementations do not support dependency injection via constructors
18 changes: 18 additions & 0 deletions src/Dapr.Workflow.Analyzers/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/Dapr.Workflow.Analyzers/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
<data name="DAPR1304MessageFormat" xml:space="preserve">
<value>The requested output type '{0}' does not match the declared output type '{1}' for {2} '{3}'</value>
</data>
<data name="DAPR1305Title" xml:space="preserve">
<value>Workflow implementations do not support dependency injection via constructors</value>
</data>
<data name="DAPR1305MessageFormat" xml:space="preserve">
<value>Workflow '{0}' has constructor parameter '{1}' of type '{2}', but dependency injection is not supported in workflow implementations. Move dependencies to a WorkflowActivity instead.</value>
</data>
</root>
125 changes: 125 additions & 0 deletions src/Dapr.Workflow.Analyzers/WorkflowDependencyInjectionAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// ------------------------------------------------------------------------
// Copyright 2025 The Dapr Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------

using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Dapr.Workflow.Analyzers;

/// <summary>
/// Analyzes whether a Workflow implementation attempts to use constructor-based dependency
/// injection, which is not supported by the Dapr workflow runtime because workflow code must
/// be deterministic and is replayed multiple times.
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class WorkflowDependencyInjectionAnalyzer : DiagnosticAnalyzer
{
internal static readonly DiagnosticDescriptor WorkflowDependencyInjectionDescriptor = new(
id: "DAPR1305",
title: new LocalizableResourceString(nameof(Resources.DAPR1305Title), Resources.ResourceManager, typeof(Resources)),
messageFormat: new LocalizableResourceString(nameof(Resources.DAPR1305MessageFormat), Resources.ResourceManager, typeof(Resources)),
category: "Usage",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);

/// <summary>
/// Gets the diagnostics supported by this analyzer.
/// </summary>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
[
WorkflowDependencyInjectionDescriptor
];

/// <summary>
/// Initializes analyzer actions.
/// </summary>
/// <param name="context">The analysis context.</param>
public override void Initialize(AnalysisContext context)
{
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.EnableConcurrentExecution();

context.RegisterCompilationStartAction(static compilationStartContext =>
{
var workflowBaseType = compilationStartContext.Compilation.GetWorkflowBaseType();
Comment thread
WhitWaldo marked this conversation as resolved.
if (workflowBaseType is null)
{
return;
}

compilationStartContext.RegisterSyntaxNodeAction(
nodeContext => AnalyzeClassDeclaration(nodeContext, workflowBaseType),
SyntaxKind.ClassDeclaration);
});
}

private static void AnalyzeClassDeclaration(
SyntaxNodeAnalysisContext context,
INamedTypeSymbol workflowBaseType)
{
context.CancellationToken.ThrowIfCancellationRequested();

var classDeclaration = (ClassDeclarationSyntax)context.Node;

if (context.SemanticModel.GetDeclaredSymbol(classDeclaration, context.CancellationToken) is not INamedTypeSymbol classSymbol)
{
return;
}

if (!DerivesFromWorkflow(classSymbol, workflowBaseType))
{
return;
}

foreach (var constructor in classDeclaration.Members.OfType<ConstructorDeclarationSyntax>())
{
context.CancellationToken.ThrowIfCancellationRequested();

foreach (var parameter in constructor.ParameterList.Parameters)
{
var parameterSymbol = context.SemanticModel
.GetDeclaredSymbol(parameter, context.CancellationToken);

var typeName = parameterSymbol?.Type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)
?? parameter.Type?.ToString()
?? "unknown";

var paramName = parameter.Identifier.Text;

context.ReportDiagnostic(Diagnostic.Create(
WorkflowDependencyInjectionDescriptor,
parameter.GetLocation(),
classSymbol.Name,
paramName,
typeName));
}
}
}

private static bool DerivesFromWorkflow(INamedTypeSymbol classSymbol, INamedTypeSymbol workflowBaseType)
{
for (var current = classSymbol.BaseType; current is not null; current = current.BaseType)
{
if (current.IsGenericType &&
SymbolEqualityComparer.Default.Equals(current.OriginalDefinition, workflowBaseType))
{
return true;
}
}

return false;
}
}
Loading
Loading