Open sandboxFocusImprove this doc

LAMA0073: The transformed code can't be parsed

Severity Error
Reported by Metalama.Framework
Message Failed to parse generated code for file '<FilePath>'. This indicates a code generation issue where the generated syntax tree produces invalid text. Exception: <ExceptionType>: <ExceptionMessage>

Cause

Metalama transforms your code as syntax trees, not as text. A syntax tree can be valid while its text isn't, for instance when two tokens are written without the whitespace that separates them. Such a problem stays hidden during the build, but appears when the transformed code is written to disk, debugged, or parsed again.

When you set the MetalamaVerifyOutputCode MSBuild property to True, Metalama verifies the transformed code at the end of the build: it converts each transformed syntax tree to text and parses this text again with the C# parser. This verification is disabled by default.

Metalama reports LAMA0073 when the C# parser throws an exception during this verification. In the message, <FilePath> is the path of the source file whose transformed code fails, and <ExceptionType> and <ExceptionMessage> are the type and the message of the exception. When the parser reports syntax errors instead of throwing, Metalama reports LAMA0074.

When the verification fails, the build fails too. If MetalamaDebugTransformedCode or MetalamaEmitCompilerTransformedFiles is enabled, Metalama still passes the transformed code to the compiler, so that the failing files are written to disk for inspection.

This diagnostic is caused by a defect in the code generation and only occurs with an opt-in MSBuild property, so it can't be shown in an example.

Resolution

  1. Set MetalamaEmitCompilerTransformedFiles to True and rebuild, then inspect the transformed file named in the message. See Debugging aspect-oriented code to find the transformed files.
  2. If the faulty code comes from Metalama itself, check whether the problem is already fixed by updating the Metalama packages to the latest version of your release line. Otherwise, report the problem as described in How to file an issue, with the message and, if you can, a minimal project that reproduces it.
  3. If the faulty code comes from one of your aspects, for instance from code built as text, correct the template so that it produces valid C#.
  4. As a temporary workaround, remove the MetalamaVerifyOutputCode property. The verification is optional, but the problem it found may still cause other errors.

See also