Skip to content

Formatter requires two passes in the presence of directives inside lambda block bodies, inside argument lists #57465

@davidwengier

Description

@davidwengier

Given the following code:

namespace N
{
    public class C
    {
        protected void Render()
        {
            if (true)
            {
                    M(() =>
                        {
#nullable enable
                               if (true)
                                    {
                                    }
                                }
                        );
                }
        }
    }
}

Formatting the document once results in:

namespace N
{
    public class C
    {
        protected void Render()
        {
            if (true)
            {
                M(() =>
                    {
#nullable enable
                            if (true)
                        {
                        }
                    }
                    );
            }
        }
    }
}

Note the position of the if (true). It has been moved by the formatter, but not by enough.

Foratting the document again results in:

namespace N
{
    public class C
    {
        protected void Render()
        {
            if (true)
            {
                M(() =>
                    {
#nullable enable
                        if (true)
                        {
                        }
                    }
                    );
            }
        }
    }
}

This is now properly formatted, and subsequent formatting does nothing, as you would expect.

Removing the #nullable enable from the file fixes the issue, and the formatter does the right thing on the first pass. Alternatively, changing the code so the lambda is assigned to a discard rather than passing it as an argument, also fixes the issue, even if the #nullable enable is left in place.

This bug is the cause of dotnet/razor#5676 in Razor, which generates code in a similar form to the example here.

Metadata

Metadata

Assignees

Labels

Area-IDEBugIDE-FormatterCode formatter and/or smart indentResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions