Optional sorting of import directives
I can't find a way to completely disable sorting import directives. There are the following two options but neither of them disables the sorting action
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
Would be useful to have an option:
dotnet_sort_import_directives = true|false
@tomekpaszek, the absense of both of those settings from the .editorconfig should disable import sorting. Please log a bug if it does not. We use the presense of either setting as, "I want import sorting and it should be performed like this".
Oh, ok 🤦♂️ Yes, it works as expected. Thank you.
No worries. This needs to be documented better. I am going to reopen this issue to track updating the documentation.
I have a similar problem.
I would like to have import sorting but as a suggestion, not as warning, error. Is that currently possible?
I tried with dotnet_sort_system_directives_first = true:suggestion and even with dotnet_sort_system_directives_first = true:silent and it seems it doesn't work. It always logs: Fix imports ordering. for a file with incorrect import order and make a dotnet format to terminate with a non-zero exit code. Is there any way to log message Fix imports ordering. and return zero exit code?
And another confusing thing is dotnet_sort_system_directives_first . It says only about sorting system directives. But I see that I need to have all imports sorted alphabetically, not only System imports. So I reorder some other imports and then dotnet format shows a message Fix imports ordering. So is there any other setting for sorting imports or only this dotnet_sort_system_directives_first and just a name of it is confusing?
I'm also seeing the above issue.
When both settings are set to false in .editorconfig, dotnet format sorts the namespace imports:
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
We use the presense of either setting as, "I want import sorting and it should be performed like this".
To help you get started, here's an example .editorconfig file with the default options.
where the example .editorconfig supplied explicitly lists these options with false as the default. @JoeRobich Your comments seems to suggest otherwise? This is confusing to me and inconsistent with the docs.
So, neither of these settings controls whether you want to sort imports. dotnet_sort_system_directives_first controls whether you want System imports first as opposed to being strictly alphabetical. dotnet_separate_import_directive_groups controls whether you want an empty line or not between your sorted import groups.
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = false
The above configuration means that you want your imports sorted alphabetically with no empty line between groups.
Since there is not a setting which says, "I want my imports organized", we take the inclusion of either of these settings in your .editorconfig to mean that you are interested in having your imports organized.
I have a similar problem. I would like to have import sorting but as a suggestion, not as warning, error. Is that currently possible?
That is not currently possible in Roslyn. The settings are for how imports should be organized not a diagnostic for whether they should be organized. It is included in dotnet-format as a "pseudo" diagnostic because it is highly requested that imports organization be enforced.
What we need is a new Code Style analyzer be added to Roslyn with a dotnet_prefer_organized_imports .editorconfig setting.
Since there is not a setting which says, "I want my imports organized", we take the inclusion of either of these settings in your .editorconfig to mean that you are interested in having your imports organized.
In other words, excluding these settings should leave your imports as they are, do I understand you correctly?
If excluding means 'entirely absent from your .editorconfig', then yes. dotnet-format should not check whether your imports are organized.
Okay, so "false" is not in fact the default for these settings as documented, but "absence" is the default, which has the effect of not organizing imports. This is confusing imho, and it is also not possible to induce this behavior by overriding inherited .editorconfig settings (from a parent folder).
What we need is a new Code Style analyzer be added to Roslyn with a dotnet_prefer_organized_imports .editorconfig setting.
I agree with this. It would also resolve the behavior I mentioned