dotnet / roslyn-analyzers

MIT License
1.58k stars 464 forks source link

Can't ban namespaces #7340

Open gilfrenkel89 opened 3 months ago

gilfrenkel89 commented 3 months ago

I am trying to ban the usage of Newtonsoft.Json package in my codebasewith the BannedApiAnalyzers Nuget package (latest stable version, 3.3.4).

My problem is that while I am able to ban specific classes, I am unable to ban the entire namespace.

Consider the following code for example:

var x = Newtonsoft.Json.JsonConvert.SerializeObject(new object());

When my BannedSymbols.txt file looks like this: T:Newtonsoft.Json.JsonConvert;Use System.Text.Json instead ,I get a compilation error, as expected.

But when my BannedSymbols.txt file looks like this: N:Newtonsoft.Json;Use System.Text.Json instead, the build passes. The build passes whether the namespace is used in a fully-qualified statement (as above) or in a using statement.

Banning individual classes is less maintainable and more prone to errors - I want the entire namespace banned.

What am I missing? How can I ban the entire Newtonsoft.Json namespace (and its sub-namespaces, like Newtonsoft.Json.Linq)?

AliveDevil commented 2 months ago

Was stumped by this as well, solution is an upgrade to at least 3.11.0-beta1.23407.1^1.

gilfrenkel89 commented 2 months ago

Thanks @AliveDevil ! Is there any estimate on when this will be released (on a non-beta version)?