Open haacked opened 3 years ago
Hey @haacked sorry for the delay in reviewing this issue. I am not really familiar with the use-case you are describing, I am just supposed to copy-paste the code in a new project?
Also, would you mind giving a shot with the latest version of the analyzer?
Hi @Evangelink, it's admittedly a slightly unusual use case.
I want to run the CSharpSymbolIsBannedAnalyzer
against code that's created using CSharpScript.Create
and then compiled script.CompileCodeAsync()
.
I couldn't find a way to programmatically reference CSharpSymbolIsBannedAnalyzer
so I just cloned the dotnet/roslyn-analyzers
repo and added my own Program.cs
(which I included in the issue).
In the BannedSymbolsAdditionalText
I'm attempting to ban System.Console
and System.Environment
.
And as you can see from my code, it fails to report a diagnostic for the following code:
Console.WriteLine("This should be banned!");
But it works fine and reports a diagnostic for:
var env = Environment.CommandLine;
I expected that a diagnostic would be reported for Console.WriteLine(...)
.
Hi guys. Maybe someone can advice? How to use CSharpSymbolIsBannedAnalyzer in own project? I've put the package ref on https://www.nuget.org/packages/Microsoft.CodeAnalysis.BannedApiAnalyzers but the code (simple console app in where dynamic code compilation implemented). I've tried code like that
var analyzers = ImmutableArray.Create<DiagnosticAnalyzer>(
new CSharpSymbolIsBannedAnalyzer());
var compilationWithAnalyzers = compilation.WithAnalyzers(
analyzers,
new AnalyzerOptions(ImmutableArray.Create<AdditionalText>(new BannedSymbolsAdditionalText(@"T:System.Diagnostics.Process;Don't use Process"))),
cancellationToken);
But my code cannot see CSharpSymbolIsBannedAnalyzer. The package looks like contains some other structure (not as nuget regular reference). Maybe I should use other way?
@junsan1 or @haacked did you work out how to reference the CSharpSymbolIsBannedAnalyzer for use on a C# script, like in your snipped? Im facing exactly the same issue, unsure what to do except perhaps to replicate the analyzer code in my script host.
Analyzer
Diagnostic ID: RS0030: The symbol '{0}' is banned in this project
Analyzer source
NuGet Package: Microsoft.CodeAnalysis.BannedApiAnalyzers
Version: v3.3.1 (Latest)
Describe the bug
Attempting to ban a type doesn't catch usage of the type for CSharpScript compilations when calling a void static method of the type.
Steps To Reproduce
Console.WriteLine("test");
.T:System.Console;Don't use System.Console
Expected behavior
There should be a RS0030 diagnostic warning against using
System.Console
.Actual behavior
No diagnostic.
Additional context
It might be easier if I just include the code I used to demonstrate this potential bug.
And the output is:
As you can see, there is no warning for the code
Console.WriteLine("This should be banned!");
but there is a warning forvar env = Environment.CommandLine
. Not only that, I was able to successfully run theConsole.WriteLine
script even though it should be banned.