mirror of
				https://gitlab.com/Kwoth/nadekobot.git
				synced 2025-11-03 16:24:27 -05:00 
			
		
		
		
	Possible fix for constant source generator crashes
This commit is contained in:
		@@ -64,7 +64,9 @@ public class CmdAttribute : System.Attribute
 | 
			
		||||
    public void Initialize(IncrementalGeneratorInitializationContext context)
 | 
			
		||||
    {
 | 
			
		||||
// #if DEBUG
 | 
			
		||||
//         SpinWait.SpinUntil(() => Debugger.IsAttached);
 | 
			
		||||
//         if (!Debugger.IsAttached)
 | 
			
		||||
//             Debugger.Launch();
 | 
			
		||||
//         // SpinWait.SpinUntil(() => Debugger.IsAttached);
 | 
			
		||||
// #endif
 | 
			
		||||
        context.RegisterPostInitializationOutput(static ctx => ctx.AddSource(
 | 
			
		||||
            "CmdAttribute.g.cs",
 | 
			
		||||
@@ -157,7 +159,8 @@ public class CmdAttribute : System.Attribute
 | 
			
		||||
            .Distinct();
 | 
			
		||||
        
 | 
			
		||||
        var methodModels = methods
 | 
			
		||||
            .Select(x => MethodDeclarationToMethodModel(compilation, x!));
 | 
			
		||||
            .Select(x => MethodDeclarationToMethodModel(compilation, x!))
 | 
			
		||||
            .Where(static x => x is not null);
 | 
			
		||||
 | 
			
		||||
        var groups = methodModels
 | 
			
		||||
            .GroupBy(static x => $"{x.Namespace}.{string.Join(".", x.Classes)}");
 | 
			
		||||
@@ -187,11 +190,21 @@ public class CmdAttribute : System.Attribute
 | 
			
		||||
        return models;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static MethodModel MethodDeclarationToMethodModel(Compilation comp, MethodDeclarationSyntax decl)
 | 
			
		||||
    private static MethodModel? MethodDeclarationToMethodModel(Compilation comp, MethodDeclarationSyntax decl)
 | 
			
		||||
    {
 | 
			
		||||
        // SpinWait.SpinUntil(static () => Debugger.IsAttached);
 | 
			
		||||
 | 
			
		||||
        var semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
 | 
			
		||||
        SemanticModel semanticModel;
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            semanticModel = comp.GetSemanticModel(decl.SyntaxTree);
 | 
			
		||||
        }
 | 
			
		||||
        catch
 | 
			
		||||
        {
 | 
			
		||||
            // for some reason this method can throw "Not part of this compilation" argument exception
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        var methodModel = new MethodModel(
 | 
			
		||||
            @params: decl.ParameterList.Parameters
 | 
			
		||||
                .Where(p => p.Type is not null)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user