-
Notifications
You must be signed in to change notification settings - Fork 507
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
SA1200 is firing for global usings #3404
Comments
Note that global using directives are only expected to appear in auto-generated files, since they are created by the SDK according to directives listed in the project file. When this pattern is followed, SA1200 will not be reported because SA1200 is not active in generated files. |
Why would you expect that? It is totally valid to use them in normal code, to reduce the boilerplate across your code base. |
Consistent declaration of global usings in the project file reduces the burden of keeping track of which directives are declared in which location(s) throughout the project. This best practice is already supported by SA1200. |
Some people use older .NET SDKs but reference a version of the compiler that supports global usings via nuget packages just so they can use newer C# language versions I think with it (when they set their C# language versions to Now I know you are thinking: Why are they wanting to do that? The answer is that sometimes their CI pipelines does not allow them to update the .NET SDK they want to use and so they get stuck with an older one installed with no way to install an newer one without admin/root access (some lock that down to avoid them from installing malicious programs in their CI). Sometimes however they have to reference the C# compiler package due to the one in their SDK bugging on their code and the referenced one fixes it due to it being newer. Both cases are valid reasons why the analyzer should acknowledge global usings and not expect them to only happen on autogenerated files mainly because it is not always possible. Likewise perhaps the user prefers to type the global usings themselves? |
@AraHaan You can still add the following line to the top of a file manually if it's not possible to use the project system to generate the file:
|
Actually it is the new language feature. We can see being used manually on the event of the launch of VS2012. |
This will disable more warnings though, and not just the one this bug causes |
May I please ask that this be reconsidered? Global using statements are intended to be used in normal source code to reduce boilerplate in cases where certain libraries are bound to be imported in all / most files. It's clear that Microsoft has been working hard to reduce boilerplate with file-scoped namespaces, implied Main method, record types and global namespaces. This allows the language to be more succinct (sometimes to its detriment) but in many cases this is justified to reduce repeated code. Would you at least consider an option to avoid firing SA1200 when global usings are used in regular source code please? |
The current behavior does not impede the use of global using directives via the project system support which is used by the SDK. The file containing the resulting generated using directives is marked with
It would be fine to update all the using directive rules to account for global using directives, but we would expect the change to include complete tests for all supported options/scenarios related to using directives. This is one of the more complicated analyzer/fix combinations, so I'm not sure when this will be completed considering the SDK approach for this feature naturally avoids problems. |
Thanks so much for the reply. For the moment I'm simply getting around it by disabling Keep up the great work |
As mentioned in #3243, SA1200 is firing for C# 10 global using statements.
cc @AraHaan @sharwell
The text was updated successfully, but these errors were encountered: