-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fbp live directive diagnostics #80575
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
base: main
Are you sure you want to change the base?
Changes from all commits
e774d7a
eadcdb1
7c91f63
956bd15
92b7303
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis.Diagnostics; | ||
| using Microsoft.DotNet.FileBasedPrograms; | ||
|
|
||
| namespace Microsoft.CodeAnalysis.FileBasedPrograms; | ||
|
|
||
| [DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
| internal sealed class AppDirectiveDiagnosticAnalyzer : DiagnosticAnalyzer | ||
| { | ||
| public const string DiagnosticId = "FileBasedPrograms"; | ||
|
|
||
| private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm getting this warning when I open this in VSCode (and didn't see it reported by CI, so just FYI):
|
||
| DiagnosticId, | ||
| title: DiagnosticId, | ||
| // TODO: we probably want to have a different diagnostic for each kind that the SDK package can produce | ||
| messageFormat: "{0}", | ||
| category: "Usage", | ||
| defaultSeverity: DiagnosticSeverity.Error, | ||
| isEnabledByDefault: true); | ||
|
|
||
| public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => [Rule]; | ||
|
|
||
| public override void Initialize(AnalysisContext context) | ||
| { | ||
| context.EnableConcurrentExecution(); | ||
| context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
|
|
||
| context.RegisterCompilationStartAction(context => | ||
| { | ||
| context.RegisterSyntaxTreeAction(visitSyntaxTree); | ||
| }); | ||
|
|
||
| void visitSyntaxTree(SyntaxTreeAnalysisContext context) | ||
| { | ||
| var tree = context.Tree; | ||
| if (!tree.Options.Features.ContainsKey("FileBasedProgram")) | ||
| return; | ||
|
|
||
| var root = tree.GetRoot(context.CancellationToken); | ||
| if (!root.ContainsDirectives) | ||
| return; | ||
|
|
||
| // App directives are only valid when they appear before the first C# token | ||
| var rootLeadingTrivia = root.GetLeadingTrivia(); | ||
| var diagnosticBag = DiagnosticBag.Collect(out var diagnosticsBuilder); | ||
| AppDirectiveHelpers.FindLeadingDirectives( | ||
| new SourceFile(tree.FilePath, tree.GetText(context.CancellationToken)), | ||
| root.GetLeadingTrivia(), | ||
| diagnosticBag, | ||
| builder: null); | ||
|
|
||
| foreach (var diag in diagnosticsBuilder) | ||
| { | ||
| context.ReportDiagnostic(createDiagnostic(tree, diag)); | ||
| } | ||
|
|
||
| // The compiler already reports an error on all the directives past the first token in the file. | ||
| // Console.WriteLine("Hello World!"); | ||
| // #:property foo=bar // error CS9297: '#:' directives cannot be after first token in file | ||
| } | ||
|
|
||
| // TODO: should SimpleDiagnostics have IDs? message args? TextSpan? | ||
| // It feels unreasonable for users to suppress these. | ||
| // When these are present, the user cannot build/run, period. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, currently we only have errors. We might want to add warnings in the future, but don't know about any candidates right now. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Future candidate is |
||
| Diagnostic createDiagnostic(SyntaxTree syntaxTree, SimpleDiagnostic simpleDiagnostic) | ||
| { | ||
| return Diagnostic.Create( | ||
| Rule, | ||
| location: Location.Create(syntaxTree, simpleDiagnostic.Location.TextSpan), | ||
| simpleDiagnostic.Message); | ||
| } | ||
| } | ||
| } | ||
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.
I guess source packages can contain any sources, including resx and xlf, and if these strings need to be used inside SDK too, I don't see other option than having these in the package.
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.
I'll try creating a new resource file next to AppDirectiveHelpers for short term, and hopefully it is something we can just move to the source package when ready.
There is no real overhead to creating an additional resource file vs reusing existing one, I think?