Metalama 1.0 / / Metalama Documentation / Conceptual Documentation / Creating Aspects / Debugging Aspects

Debugging Aspects

Debugging the compile-time logic of an aspect is currently difficult because the compiler does not execute your source code, but the transformed code produced from your source code and stored under an unpredictable path.

Debugging compile-time logic

To debug compile-time logic:

  1. Inject breakpoints directly in your source code:

    • In a build-time method such as BuildAspect, call System.Diagnostics.Debugger.Break().
    • In a template method, call meta.DebugBreak.

      Warning

      Normal debugger breakpoints will not work. You must have a breakpoint directly in your source code.

  2. Attach the debugger to the process

    • In an aspect test, run the test with the debugger.
    • To debug the compiler, set the MetalamaDebugCompiler property to True:

      dotnet build -p:MetalamaDebugCompiler=True
      

Debugging design-time logic

To attach a debugger to the design-time compiler process:

  1. Install Metalama Command Line Tools as described in Installing Metalama Command Line Tools.
  2. Execute the following commands:

    metalama-config diag edit
    
  3. In the diagnostics.json file, edit the debugger/processes section and enable debugging for the proper process. If you are using Visual Studio, this process is named RoslynCodeAnalysisService.

    {
        "logging": {
            "processes": {
                "Compiler": false,
                "Rider": false,
                "DevEnv": false,
                "RoslynCodeAnalysisService": false
            },
            "categories": {
            "*": true,
            "Licensing": false
            }
        },
        "debugger": {
            "processes": {
                "Compiler": false,
                "Rider": false,
                "DevEnv": false,
                "RoslynCodeAnalysisService": true
            }
        }
    }
    
  4. Restart your IDE.