Prevent Jetbrains Rider from "optimizing" certain `using` directives
For backward compatibility reasons, when I need to modify the models I use to deserialize JSON/YAML data in a breaking way, I copy & paste the whole structure into a new "versioned namespace" and then make changes to the new copy. Example:
In `MyProject/V1/YamlObject.cs`:
```cs
namespace MyProject.V1;
public record YamlObject;
```
In `MyProject/V2/YamlObject.cs`:
```cs
namespace MyProject.V2;
public record YamlObject;
```
And separately, in `MyProject/YamlObjectLatest.cs` I have:
```cs
global using YamlObjectLatest = MyProject.V2.YamlObject;
```
When I execute Code Cleanup in Jetbrains Rider, it tends to favor doing two things:
1. If I use `V2.YamlObject` and/or `V1.YamlObject` explicitly in code that wants to use either of these versioned types, it prioritizes taking away the partially-qualified namespace and placing it in a `using` directive. So as an example, it wants to make `V1.YamlObject` into `YamlObject`.
2. If I'm using `V2.YamlObject`, it wants to convert it into the global using: `YamlObjectLatest`. This is great everywhere *except* where I am explicitly mapping V1 -> V2 and won't necessarily want to use the latest version.
How can I configure my `.editorconfig`, Rider, and/or the code so that I am not constantly fighting the above refactoring it wants to do?
Regarding code that uses these objects, I have files where I always want to use the latest version, and others where I'm doing some compatibility/conversion work that target specific versions. I'm hoping there's some comments, pragmas, or some kind of annotations in code I can use to communicate these scenarios to Rider so it doesn't fight my changes.
Please sign in to leave a comment.
Hello, thank you for your question.
I'm afraid, Rider doesn't have such a functionality that allows to do what you're asking using comments/annotations/editorconfig. There is the following issue in our bug tracker which you can comment or vote on to be notified of status changes: Allow disabling Code Cleanup for a block of code with comments.
You can also try enabling the following option in Rider settings: File | Settings | Editor | Code Style | C# | Syntax Style | Reference Qualification and 'using' Directives | Prefer fully qualified references. Or you can create a custom Code Cleanup profile in File | Settings | Editor | Code Cleanup, disable the following option Optimize Imports | C# | Shorten qualified references and use this profile when cleaning up code.
Please let me know if you have any questions. Have a nice day!