Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#pragma with long line produces unformattable file #865

Closed
Denton-L opened this issue Apr 5, 2023 · 1 comment
Closed

#pragma with long line produces unformattable file #865

Denton-L opened this issue Apr 5, 2023 · 1 comment
Milestone

Comments

@Denton-L
Copy link

Denton-L commented Apr 5, 2023

The following file fails the csharpier check:

using System;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace Test;

public static class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("test");
        }
        catch (Exception e)
        {
            if (
                e is
#pragma warning disable CS0618
                BadHttpRequestException
#pragma warning restore CS0618
                {
                    Message: "Thisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlongline"
                }
            )
            {
                Console.WriteLine("test");
            }
        }
    }
}

with the following:

Warning ./Program.cs - Was not formatted.
  ----------------------------- Expected: Around Line 21 -----------------------------
  #pragma·warning·restore·CS0618
  
  ················{
  ----------------------------- Actual: Around Line 21 -----------------------------
  #pragma·warning·restore·CS0618
  ················{
  ····················Message:·"Thisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlongline"
  
Total time:                                                                          141ms
Total files:                                                                           1  
Files that were not formatted:                                                         1  

However, it seems like running dotnet csharpier --no-cache ./Program.cs over and over again keeps producing extra newlines. This is the file after running the command five times:

using System;
using Microsoft.AspNetCore.Server.Kestrel.Core;

namespace Test;

public static class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("test");
        }
        catch (Exception e)
        {
            if (
                e is
#pragma warning disable CS0618
                BadHttpRequestException
#pragma warning restore CS0618





                {
                    Message: "Thisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlongline"
                }
            )
            {
                Console.WriteLine("test");
            }
        }
    }
}

This seems to indicate that there is no way to make csharpier happy.

A workaround is moving the block inside the #pragma but I would like to avoid that if possible.

This is the .csharpierrc.json:

{
  "printWidth": 120
}

I'm running csharpier 0.23.0 and dotnet 7.0.201.

Thanks!

@belav
Copy link
Owner

belav commented Apr 5, 2023

Another potential workaround - I don't know if this is a valid pattern, but the playground is happy with this and the extra line is not produced.

const string longMessage = "Thisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlonglinethisisasuperlongline";

if (e is
#pragma warning disable CS0618
    BadHttpRequestException
#pragma warning restore CS0618
    {
        Message: longMessage
    }
)

@belav belav added this to the 0.24.0 milestone Apr 10, 2023
belav added a commit that referenced this issue Apr 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants