-
Notifications
You must be signed in to change notification settings - Fork 485
Support banning namespaces #6521
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
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6521 +/- ##
==========================================
- Coverage 96.43% 96.42% -0.01%
==========================================
Files 1373 1373
Lines 320260 320477 +217
Branches 10296 10302 +6
==========================================
+ Hits 308837 309029 +192
- Misses 8969 8985 +16
- Partials 2454 2463 +9 |
This might be due to the fact that it might be a merged namespace symbol. See https://github.com/dotnet/roslyn/blob/98a00a37e1a66501a1a7495b89853b466668c6b8/src/Compilers/Core/Portable/Symbols/INamespaceSymbol.cs#L49-L60 I think before yield return, you may want to check if namespace's |
Thank you for the suggestion. I haven't went down the route of comparing |
Will this produce warnings on the generated code when using implicit global usings? For example, if I ban <ItemGroup>
<Using Include="Foo"/>
</ItemGroup> Then in a file like // <auto-generated/>
global using global::Foo; After which I can use symbols from the banned namespace in any source file. If we provide the warning in the generated file, that's fine. But if not, then there is the chance of accidentally bits from banned namespaces. |
@drewnoakes The detection of banned APIs doesn't change with this PR. Banning a namespace effectively means the same thing as banning all types within that namespace and its descendants. See also this example on what it means to ban a type. Merely referencing the namespace or a type within that namespace doesn't produce the warning. Only consuming a banned API produces the warning, e.g. invoking a method on a banned type. |
I've rebased my PR with the lazy defer work changes from @CyrusNajmabadi. Is there anything else I can do to get this in a shape to be merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for picking this up. Looks good to me.
src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/SymbolIsBannedAnalyzerBase.cs
Show resolved
Hide resolved
src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/SymbolIsBannedAnalyzerBase.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/SymbolIsBannedAnalyzerBase.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.CodeAnalysis.BannedApiAnalyzers/Core/DocumentationCommentIdParser.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I can merge once you address/respond to the suggestions
@mavasani thank you for the review. There's one more thing to clear up and then the PR should be good to go. |
Thank you @Bouke! |
@mavasani I don't think this is part of the latest 3.3.4 NuGet package? Any chance we can ship an update? |
@jlaanstra That is correct, we'll work on shipping an updated package. Meanwhile, you can use the pre-release version from this feed: https://dev.azure.com/dnceng/public/_artifacts/feed/dotnet8/NuGet/Microsoft.CodeAnalysis.BannedApiAnalyzers/overview/3.11.0-beta1.23407.1 |
The BannedApiAnalyzer refers to "ID string format" for reference on what symbols to ban. One of those symbols is Namespaces, but this is not supported. As this has been requested before and is also something I ran into earlier today, it seems like a good fit to add to the project.
This will ban everything under namespace
A
, including child namespaces likeA.B
andA.B.C
. Having an invocation likenew A.B.C.D()
will yield the error "The symbol 'A' is banned in this project":I've chosen to report on the namespace of the symbol instead of the symbol itself for clarity. Reporting the symbol would yield the message "The symbol 'D' is banned in this project" which might be confusing as it is actually any parent namespace that is banned.
This PR should be further tested other than the provided unit tests.
I've tried banningUpdate: this also works now, thanks to @mavasani.System.Threading
in the tests, but that didn't work. Could it be that theINamespaceSymbol
implements equality other than just a name comparison?