Open sandboxFocusImprove this doc

Debugging aspects

Adding breakpoints to templates and compile-time code

Debugging the compile-time logic of an aspect or fabric can be challenging because the compiler doesn't execute your source code, but a heavily transformed version where T# templates have been compiled into plain C#. This transformed code is stored under the obj/<Configuration>/<TargetFramework>/metalama directory.

Warning

You cannot set compile-time breakpoints in your source code files if the project references the Metalama.Framework package and the MetalamaEnabled MSBuild property is not set to False. Visual Studio breakpoints placed in source files will not be hit because the debugger sees only the transformed code, not the original source.

You must add break statements directly in your source code and remember to remove them after the debugging session. Once the debugger stops at a break statement and you step through the code, you'll be viewing the transformed code. At that point, you can set breakpoints in that transformed code file for the remainder of your debugging session.

To add break statements:

Debugging aspect tests

The most convenient way to debug an aspect is to create an aspect test as described in Snapshot testing of aspects. This allows you to isolate the scenario you want to debug.

To debug an aspect test:

  1. Insert breakpoints directly into your source code as described above.
  2. Use the Debug command of the unit test runner.

Debugging the compiler process

To debug compile-time logic:

  1. Insert breakpoints directly into your source code as described above.

  2. Execute the compiler with these options:

    • -p:MetalamaDebugCompiler=True to cause the compiler to display the JIT debugger dialog, allowing you to attach a debugger to the compiler process.
    • -p:MetalamaConcurrentBuildEnabled=False to force Metalama to run in a single thread, saving you from the chaos of multi-threaded debugging.
    • Optionally, --disable-build-servers to disable the use of reusable server MSBuild and Metalama.Compiler processes.
    • Optionally, --no-dependencies to avoid rebuilding referenced projects.

Example:

dotnet build MyProject.csproj -p:MetalamaDebugCompiler=True -p:MetalamaConcurrentBuildEnabled=False

Debugging the IDE process

To attach a debugger to the design-time compiler process, follow these steps:

  1. Install the Metalama Command Line Tool as instructed in Installing the Metalama command line tool.

  2. Execute the command below:

    metalama config edit diagnostics
    
  3. In the diagnostics.json file, modify the debugging/processes section and enable debugging for the appropriate process. If you're using Visual Studio, this process is named RoslynCodeAnalysisService.

    {
    
        // ...
    
        "debugger": {
            "processes": {
                 "Rider": false,
                  "RoslynCodeAnalysisService": true
            }
        }
    
        // ...
    
    }
    
  4. Insert breakpoints directly into your source code as described above.

  5. Restart your IDE.