Open sandboxFocusImprove this doc

LAMA0056: The selected code fix can't be found again

Severity Error
Reported by Metalama.Extensions.CodeFixes
Message The code fix '<CodeFixTitle>' could no longer be found. The logic that suggests the code fix may be non-deterministic.

Cause

Aspects and validators can attach code fixes to the diagnostics they report, or suggest code fixes without reporting a diagnostic with the Suggest method. The IDE shows these code fixes in the lightbulb menu, identified by their title.

Metalama doesn't keep the implementation of a code fix between the moment the IDE shows it and the moment you select it. When you select a code fix, to preview or apply it, Metalama runs the aspects and validators again on the current file, collects the code fixes they suggest for the same diagnostic ID and location, and looks for the one whose title is the title you selected. In the message, <CodeFixTitle> is this title.

Metalama reports LAMA0056 when this second run doesn't produce the code fix:

  • the second run doesn't suggest any code fix for this diagnostic and location, or
  • it suggests code fixes, but none of them has exactly the same title.

This usually means that the logic that suggests the code fix doesn't give the same result every time it runs, for instance because the title of the code fix includes a random value, a timestamp, or a counter, or because the condition under which the code fix is suggested depends on such values, on state shared between executions, or on the execution scenario. It can also happen when the code was edited between the moment the lightbulb menu was displayed and the moment you selected the code fix.

The diagnostic doesn't appear in the error list. Instead, Metalama inserts the message as a // comment above the target code, in the preview of the code fix, or in the code itself when you apply it.

This diagnostic is only reported by the IDE when you select a code fix, so it can't be shown in an example.

Resolution

If you're applying a code fix suggested by a third-party aspect, remove the comment that Metalama inserted, make a small edit to the file to refresh the lightbulb menu, and select the code fix again. If the error persists, report the issue to the author of the aspect.

If you're the author of the aspect or validator that suggests the code fix:

  • Make the title of the code fix deterministic: compute it only from the code model, for instance from the name of the target declaration, and not from random values, timestamps, counters, or other state that changes between executions.
  • Make the decision to suggest the code fix deterministic as well. Don't base it on static fields or other state that survives from one execution of the aspect to the next.
  • If you make the creation of code fixes conditional on the execution scenario, to improve performance, make sure that the code fix is still created, with the same title, when MetalamaExecutionContext.Current.ExecutionScenario.CapturesCodeFixImplementations is true. This is the scenario in which Metalama runs the aspect to apply the code fix.
  • Don't suggest two code fixes with the same title for the same diagnostic and location: Metalama can't tell them apart and always applies the first one.

See also