postsharp / Metalama

Metalama is a Roslyn-based meta-programming framework. Use this repo to report bugs or ask questions.
164 stars 4 forks source link

Bug: Metalama ran into an unexpected problem: This service is not available in the current execution context #193

Closed WhitWaldo closed 10 months ago

WhitWaldo commented 11 months ago

I have an aspect. The BuildAspect isn't relevant here as I've confirmed that without the following line from BuildEligibility the aspect works fine. Adding it back in breaks Metalama.

public sealed class MyAttribute : TypeAspect
{
  private Type _entityType {get; init;}
  private string _entityKeyPropertyName { get; init; }

  public MyAttribute(Type entityType, string entityKeyPropertyName) 
  {
    _entityKeyPropertyName = entityKeyPropertyName;
    _entityType = entityType;
  }

  public override void BuildEligibility(IEligibilityBuilder<INamedType> builder)
  {
     //Commenting the following out causes the aspect to apply without issue
      builder.MustSatisfy(_ =>
        {
            var namedEntityType = (INamedType)TypeFactory.GetType(_entityType);
            return namedEntityType.Properties.Any(m => m.Name == _entityKeyIdPropertyName);
        }, _ => $"The entity type '{_entityType.Name}' must contain a property called '{_entityKeyIdPropertyName}'");
  }
}

The message shown in VS in the gold bar is: "Metalama ran into an unexpected problem. This service is not available in the current execution context." There's an Open Details button and it reads as follows:

StreamJsonRpc.RemoteInvocationException: This service is not available in the current execution context.
   at StreamJsonRpc.JsonRpc.<InvokeCoreAsync>d__151`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Metalama.Framework.DesignTime.VisualStudio.Preview.UserProcessTransformationPreviewService.<PreviewTransformationAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Metalama.Framework.DesignTime.Contracts.Preview.TransformationPreviewServiceExtensions.<PreviewTransformationAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Metalama.Vsx.Preview.PreviewCommand.<ExecuteCoreAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Metalama.Vsx.Preview.PreviewCommand.<Execute_Source>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Metalama.Vsx.Preview.PreviewCommand.<Execute>d__5.MoveNext()
RPC server exception:
System.InvalidOperationException: This service is not available in the current execution context.
      at Metalama.Framework.Code.SyntaxBuilders.SyntaxBuilder.get_CurrentImplementation()
      at Metalama.Framework.Code.TypeFactory.get_Implementation()
      at Metalama.Framework.Code.TypeFactory.GetType(Type type)
      at XYZ.MyAttribute.<BuildEligibility>b__13_0(INamedType t) in C:\Users\*\AppData\Local\Temp\Metalama\CompileTime\*\.netcoreapp7.0\716806e52bf7a6f1\2023.2.2-rc\MyA_0a5d3105.cs:line 47
      at Metalama.Framework.Eligibility.Implementation.EligibilityRule`1.GetEligibility(T obj)
      at Metalama.Framework.Engine.Aspects.AspectClass.<>c__DisplayClass63_0.<GetEligibility>g__GetEligibilityCore|0()
      at Metalama.Framework.Engine.Utilities.UserCode.UserCodeInvoker.Invoke[TResult,TPayload](UserCodeFunc`2 func, TPayload& payload, UserCodeExecutionContext context)
      at Metalama.Framework.Engine.Aspects.AspectClass.GetEligibility(IDeclaration obj, Boolean isInheritable)
      at Metalama.Framework.Engine.Aspects.AspectInstance.ComputeEligibility(IDeclaration declaration)
      at Metalama.Framework.Engine.Aspects.CompilationAspectSource.<>c__DisplayClass7_0.<GetAspectInstances>b__0(IAttribute attribute)
      at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
      at System.Linq.Enumerable.SelectManySingleSelectorIterator`2.MoveNext()
      at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
      at System.Linq.Enumerable.WhereEnumerableIterator`1.ToList()
      at Metalama.Framework.Engine.Pipeline.PipelineStepsState.ExecuteAspectSource(CompilationModel compilation, IAspectClass aspectClass, IEnumerable`1 aspectSources, CancellationToken cancellationToken)
      at Metalama.Framework.Engine.Pipeline.EvaluateAspectSourcesPipelineStep.ExecuteAsync(CompilationModel compilation, Int32 stepIndex, CancellationToken cancellationToken)
      at Metalama.Framework.Engine.Pipeline.PipelineStepsState.ExecuteAsync(CancellationToken cancellationToken)
      at Metalama.Framework.Engine.Pipeline.HighLevelPipelineStage.ExecuteAsync(AspectPipelineConfiguration pipelineConfiguration, AspectPipelineResult input, IDiagnosticAdder diagnostics, TestableCancellationToken cancellationToken)
      at Metalama.Framework.Engine.Pipeline.AspectPipeline.ExecuteAsync(PartialCompilation compilation, CompilationModel compilationModel, IDiagnosticAdder diagnosticAdder, AspectPipelineConfiguration pipelineConfiguration, TestableCancellationToken cancellationToken)
      at Metalama.Framework.Engine.Pipeline.DesignTime.PreviewAspectPipeline.ExecutePreviewAsync(DiagnosticBag diagnostics, PartialCompilation compilation, AspectPipelineConfiguration configuration, TestableCancellationToken cancellationToken)
      at Metalama.Framework.DesignTime.Preview.TransformationPreviewServiceImpl.PreviewTransformationAsync(ProjectKey projectKey, String syntaxTreeName, TestableCancellationToken cancellationToken)
PostSharpBot commented 11 months ago

Hello @WhitWaldo, thank you for submitting this issue. We will try to get back to you as soon as possible. Note to the PostSharp team, this ticket is being tracked in our dashboard under ID TP-33592.

WhitWaldo commented 11 months ago

Forgot to mention - running 2023.2.2-rc

svick commented 11 months ago

Thanks for reporting. I am going take a look at this.

WhitWaldo commented 11 months ago

This is also an issue in 2023.3.1-preview

gfraiteur commented 10 months ago

Solved in 2023.3.3-preview.