dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Error message for invalid csharp_using_directive_placement is not clear #49407

Open vsfeedback opened 3 years ago

vsfeedback commented 3 years ago

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work] Hi, when I run a CodeAnalysis on my . Net5 solution I see 72 warnings in the Error List (I think one for every file). All the warnings are AD0001:

Severity    Code    Description Project File    Line    Suppression State
Warning AD0001  Analyzer 'Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives.MisplacedUsingDirectivesDiagnosticAnalyzer' threw an exception of type 'System.NotSupportedException' with message 'Specified method is not supported.'.
Exception occurred with following context:
Compilation: Translator.Core
SyntaxTree: C:\Users\wvl\source\repos\KasparInternal.Tools.FlutterTranslationHelper\Translator.Core\TypeBuilder\EntryTypeBuilder.LanguagePropertyBuilder.cs
SyntaxNode: using System; using System.Linq ... [CompilationUnitSyntax]@[20..6103) (2,0)-(139,0)

System.NotSupportedException: Specified method is not supported.
   at Microsoft.CodeAnalysis.CSharp.CodeStyle.CSharpCodeStyleOptions.ParseUsingDirectivesPlacement(String optionString, CodeStyleOption2`1 default)
   at Microsoft.CodeAnalysis.CSharp.CodeStyle.CSharpCodeStyleOptions.<>c.<.cctor>b__46_9(String s)
   at Microsoft.CodeAnalysis.Options.EditorConfigStorageLocation`1.TryGetOption(String value, Type type, T& result)
   at Microsoft.CodeAnalysis.AnalyzerConfigOptionsExtensions.TryGetEditorConfigOption[T](AnalyzerConfigOptions analyzerConfigOptions, IOption option, Boolean useDefaultIfMissing, T& value)
   at Microsoft.CodeAnalysis.AnalyzerConfigOptionsExtensions.TryGetEditorConfigOption[T](AnalyzerConfigOptions analyzerConfigOptions, IOption option, T& value)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerHelper. <GetOptionAsync>d__24`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerHelper.GetOption[T](AnalyzerOptions analyzerOptions, ILanguageSpecificOption`1 option, SyntaxTree syntaxTree, CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.CSharp.MisplacedUsingDirectives.MisplacedUsingDirectivesDiagnosticAnalyzer.AnalyzeCompilationUnitNode(SyntaxNodeAnalysisContext context)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.<>c__62`1. <ExecuteSyntaxNodeAction>b__62_0(ValueTuple`2 data)
   at Microsoft.CodeAnalysis.Diagnostics.AnalyzerExecutor.ExecuteAndCatchIfThrows_NoLock[TArg](DiagnosticAnalyzer analyzer, Action`1 analyze, TArg argument, Nullable`1 info)
-----

Suppress the following diagnostics to disable this analyzer: IDE0065    Translator.Core     1   Active

Original Comments

(no comments)


Original Solutions

(no solutions)

CyrusNajmabadi commented 3 years ago

You have an editorconfig value for csharp_using_directive_placement that is not inside_namespace or outside_namespace.

CyrusNajmabadi commented 3 years ago

@sharwell @jinujoseph do we have a tracking bug about making these errors easier to diagnose? I would like to dupe this against that. It's def a probem that we just throw new NotSupportedException() without any sort of clear message to the user as to what the issue is.

jinujoseph commented 3 years ago

@JoeRobich thoughts ?

JoeRobich commented 3 years ago

We should remove throwing NotSupported when parsing the users editorconfig value string. Other parsing methods return the @default value when the string contains an unexpected value.

sharwell commented 3 years ago

@CyrusNajmabadi We have #19055 and #37347

CyrusNajmabadi commented 1 year ago

Closing out as covered by other analyzers.

sharwell commented 1 year ago

From #67299:

In the above case, the user has the following in their .editorconfig file:

# usings outside namespace
csharp_using_directive_placement = true

This is producing an exception in the following parsing code:

https://github.com/dotnet/roslyn/blob/f9946775cde0bf9034bfe29e8cf3e01b5e099795/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/CodeStyle/CSharpCodeStyleOptions_Parsing.cs#L55-L60

The parsing code should be updated to provide a more detailed (actionable) error message for users.