dotnet / dotnet/format

csharp_style_namespace_declarations causes compilation errors with #region directives

Open
#1,509 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
1.9k
Forks
173
Avg merge
10d 13h
Merged PRs (30d)
1

Description

## Summary

When `csharp_style_namespace_declarations` is fixed in code that contains `#region` blocks, the `#endregion` can be lost, causing compilation errors. It only happens when running `dotnet format`, not when applying the namespace declaration code fix from Visual Studio.

## Reproduction Steps

Given the following `.editorconfig` file:

```ini
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning
```

And the following Program.cs with a `#region`:

```csharp
namespace MyNamespace
{
#region SuperCoolCode
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
#endregion
}
```

Running `dotnet format` will result in the following code that fails to compile (`CS1038 #endregion directive expected`):

```csharp
namespace MyNamespace;
#region SuperCoolCode
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
```

Applying the code fix from within Visual Studio (17.1.0 Preview 4.0) works correctly without an error:

![image](https://user-images.githubusercontent.com/97195/152333642-8be2101a-7636-4a7d-add4-1949e9f632fe.png)

## Version Info

`dotnet format --version` returns `6.0.252703+68bc36719088c86b0ff01334039b0611741b8276`

`dotnet --info` returns:

```
.NET SDK (reflecting any global.json):
Version: 6.0.200-preview.22055.15
Commit: a3c3a2bf3b

Runtime Environment:
OS Name: Windows
OS Version: 10.0.19044
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.200-preview.22055.15\

Host (useful for support):
Version: 6.0.1
Commit: 3a25a7f1cc
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.