0

I'm trying to use the dotnet format command to convert a large C# solution to leverage file-scoped namespaces.

I've tried annotating my preference for these in the solution's .editorconfig with both

# from https://www.meziantou.net/updating-your-project-to-use-file-scoped-namespaces.htm
csharp_style_namespace_declarations=file_scoped:suggestion

and

# from https://stackoverflow.com/questions/71652548/rider-editorconfig-file-scoped-namespaces-not-respected
csharp_namespace_body = file_scoped

However, when I run dotnet format My.sln it makes some whitespace formatting fixes (e. g. removing trailing whitespace) but does not convert to file-scoped namespaces. Is this supposed to work? What am I missing?

I'm using dotnet 6.0.201 and I've also tried downloading an updated build of dotnet-format (7.0.321101+ec7537dd0645b35fc9057006639ca41e37d16348).

ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152

1 Answers1

3

One way to solve this is by increasing that suggestion to error or warning.

csharp_style_namespace_declarations = file_scoped:error

Then it will be picked up by dotnet format, as you were already doing.

dotnet format My.sln

Alternatively you can leave it to suggestion

csharp_style_namespace_declarations=file_scoped:suggestion

and pass --severity info to dotnet format

dotnet format My.sln --severity info

By default dotnet format runs with --severity warn, which does apply the .editorconfig settings configured with the warning and error severity but not those with suggestion.

From the documentation about --severity

The minimum severity of diagnostics to fix. Allowed values are info, warn and error. The default value is warn.

pfx
  • 20,323
  • 43
  • 37
  • 57