| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | |
| | | 7 | | using Microsoft.CodeAnalysis; |
| | | 8 | | |
| | | 9 | | using NexusLabs.Needlr.Generators.Models; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.Generators; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Detects disposable captive dependencies and reports diagnostics. |
| | | 15 | | /// A captive dependency occurs when a longer-lived service depends on a shorter-lived disposable. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class CaptiveDependencyAnalyzer |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Detects disposable captive dependencies using inferred lifetimes from DiscoveryResult. |
| | | 21 | | /// Reports NDLRGEN022 when a longer-lived service depends on a shorter-lived disposable. |
| | | 22 | | /// </summary> |
| | | 23 | | internal static void ReportDisposableCaptiveDependencies(SourceProductionContext spc, DiscoveryResult discoveryResul |
| | | 24 | | { |
| | | 25 | | // Build lookup from type name to DiscoveredType for O(1) lifetime lookups |
| | 455 | 26 | | var typeLookup = new Dictionary<string, DiscoveredType>(); |
| | 467986 | 27 | | foreach (var type in discoveryResult.InjectableTypes) |
| | | 28 | | { |
| | 233538 | 29 | | typeLookup[type.TypeName] = type; |
| | | 30 | | // Also map by interfaces so we can look up dependencies by interface |
| | 467582 | 31 | | foreach (var iface in type.InterfaceNames) |
| | | 32 | | { |
| | | 33 | | // Only add if not already present (first registration wins for interface resolution) |
| | 253 | 34 | | if (!typeLookup.ContainsKey(iface)) |
| | | 35 | | { |
| | 248 | 36 | | typeLookup[iface] = type; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Check each injectable type for captive dependencies |
| | 467986 | 42 | | foreach (var type in discoveryResult.InjectableTypes) |
| | | 43 | | { |
| | 233538 | 44 | | CheckForCaptiveDependencies(spc, type, typeLookup); |
| | | 45 | | } |
| | 455 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Checks a single type for captive dependency issues. |
| | | 50 | | /// </summary> |
| | | 51 | | private static void CheckForCaptiveDependencies( |
| | | 52 | | SourceProductionContext spc, |
| | | 53 | | DiscoveredType type, |
| | | 54 | | Dictionary<string, DiscoveredType> typeLookup) |
| | | 55 | | { |
| | | 56 | | // Skip types with transient lifetime - they can't capture shorter-lived dependencies |
| | 233538 | 57 | | if (type.Lifetime == GeneratorLifetime.Transient) |
| | 5 | 58 | | return; |
| | | 59 | | |
| | 658646 | 60 | | foreach (var param in type.ConstructorParameters) |
| | | 61 | | { |
| | | 62 | | // Skip factory patterns that create new instances on demand |
| | 95790 | 63 | | if (IsFactoryPattern(param.TypeName)) |
| | | 64 | | continue; |
| | | 65 | | |
| | | 66 | | // Try to find the dependency in our discovered types |
| | 95790 | 67 | | if (!typeLookup.TryGetValue(param.TypeName, out var dependency)) |
| | | 68 | | continue; |
| | | 69 | | |
| | | 70 | | // Check if the dependency is shorter-lived |
| | 31320 | 71 | | if (!IsShorterLifetime(type.Lifetime, dependency.Lifetime)) |
| | | 72 | | continue; |
| | | 73 | | |
| | | 74 | | // Check if the dependency is disposable |
| | 7 | 75 | | if (!dependency.IsDisposable) |
| | | 76 | | continue; |
| | | 77 | | |
| | | 78 | | // Report the captive dependency |
| | 5 | 79 | | spc.ReportDiagnostic(Diagnostic.Create( |
| | 5 | 80 | | DiagnosticDescriptors.DisposableCaptiveDependency, |
| | 5 | 81 | | Location.None, |
| | 5 | 82 | | type.TypeName, |
| | 5 | 83 | | GetLifetimeName(type.Lifetime), |
| | 5 | 84 | | dependency.TypeName, |
| | 5 | 85 | | GetLifetimeName(dependency.Lifetime))); |
| | | 86 | | } |
| | 233533 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Checks if a type name represents a factory pattern that creates new instances on demand. |
| | | 91 | | /// </summary> |
| | | 92 | | private static bool IsFactoryPattern(string typeName) |
| | | 93 | | { |
| | | 94 | | // Func<T> - factory delegate |
| | 95790 | 95 | | if (typeName.StartsWith("System.Func<", StringComparison.Ordinal)) |
| | 0 | 96 | | return true; |
| | | 97 | | |
| | | 98 | | // Lazy<T> - deferred creation |
| | 95790 | 99 | | if (typeName.StartsWith("System.Lazy<", StringComparison.Ordinal)) |
| | 0 | 100 | | return true; |
| | | 101 | | |
| | | 102 | | // IServiceScopeFactory - creates new scopes |
| | 95790 | 103 | | if (typeName == "Microsoft.Extensions.DependencyInjection.IServiceScopeFactory") |
| | 0 | 104 | | return true; |
| | | 105 | | |
| | | 106 | | // IServiceProvider - resolves services dynamically |
| | 95790 | 107 | | if (typeName == "System.IServiceProvider") |
| | 0 | 108 | | return true; |
| | | 109 | | |
| | 95790 | 110 | | return false; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Checks if dependency lifetime is shorter than consumer lifetime. |
| | | 115 | | /// </summary> |
| | | 116 | | private static bool IsShorterLifetime(GeneratorLifetime consumer, GeneratorLifetime dependency) |
| | | 117 | | { |
| | | 118 | | // Singleton > Scoped > Transient (in terms of lifetime duration) |
| | | 119 | | // A shorter lifetime means the dependency will be disposed sooner |
| | 31320 | 120 | | return (consumer, dependency) switch |
| | 31320 | 121 | | { |
| | 4 | 122 | | (GeneratorLifetime.Singleton, GeneratorLifetime.Scoped) => true, |
| | 1 | 123 | | (GeneratorLifetime.Singleton, GeneratorLifetime.Transient) => true, |
| | 2 | 124 | | (GeneratorLifetime.Scoped, GeneratorLifetime.Transient) => true, |
| | 31313 | 125 | | _ => false |
| | 31320 | 126 | | }; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Gets the human-readable name for a lifetime. |
| | | 131 | | /// </summary> |
| | 10 | 132 | | internal static string GetLifetimeName(GeneratorLifetime lifetime) => lifetime switch |
| | 10 | 133 | | { |
| | 4 | 134 | | GeneratorLifetime.Singleton => "Singleton", |
| | 4 | 135 | | GeneratorLifetime.Scoped => "Scoped", |
| | 2 | 136 | | GeneratorLifetime.Transient => "Transient", |
| | 0 | 137 | | _ => lifetime.ToString() |
| | 10 | 138 | | }; |
| | | 139 | | } |