Skip to content

Formatting "using" import split on multiple lines requires formatting it twice to get the expected result #1698

@jlocans

Description

@jlocans

Hi,

Encountered an issue where a file formatted like this

using System.Net;
using
    SomeProject.Bar;
using Microsoft.Extensions.Logging;

namespace SomeProject.Foo
{
  public class Foo
  {
  }
}

will require 2 attempts at formatting to get the expected result. So if you did format and then check, the check would fail and not ignore the failure like it normally would.
The issue seems to be this part

using
    SomeProject.Bar;

After 1st formatting the output is this:

using System.Net;
using SomeProject.Bar;
using Microsoft.Extensions.Logging;

namespace SomeProject.Foo
{
    public class Foo { }
}

and an error will be thrown

$ csharpier format .
Error .\Foo.cs - Failed formatting validation./n----------------------------- Original: Around Line 3 -----------------------------
  using
      SomeProject.Bar;
  using Microsoft.Extensions.Logging;
  
  namespace SomeProject.Foo
  {
    public class Foo
    {
  ----------------------------- Formatted: Around Line 1 -----------------------------
  using System.Net;
  using SomeProject.Bar;
  using Microsoft.Extensions.Logging;
  
  namespace SomeProject.Foo
  {
      public class Foo { }
  }

After second formatting the output will be what check would expect. The ordering of imports changed.

using System.Net;
using Microsoft.Extensions.Logging;
using SomeProject.Bar;

namespace SomeProject.Foo
{
    public class Foo { }
}

Originally the import was split on multiple lines by some other tool I assume, because the imported namespace was very long.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:bugSomething isn't working

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions