-
Notifications
You must be signed in to change notification settings - Fork 231
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
[DO NOT MERGE] Make a POC around in-memory compilation of .NET Framework views #9244
Draft
gregory-paidis-sonarsource
wants to merge
11
commits into
master
Choose a base branch
from
greg/framework-view-compiler
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
699bb5c
Make a POC around in-memory compilation of .NET Framework views
gregory-paidis-sonarsource 0e6416a
Update the code to load csproj files from the project directory
costin-zaharia-sonarsource 4633a25
Add exporter
costin-zaharia-sonarsource d6849e4
Remove SarifExporter, use ReportIssue, fix AD0001s by using file mapp…
gregory-paidis-sonarsource e1dd8c6
Add isRazorAnalysisEnabled back, fix UTs
gregory-paidis-sonarsource a09169c
Enable parameterized rules
gregory-paidis-sonarsource f846743
Add Costin.cshtml and enable support for multiple views
gregory-paidis-sonarsource e0b9bb9
Add excluded rules, run the rule only in ASP.NET framework context, c…
gregory-paidis-sonarsource bb86f03
Split the implementation in multiple files and use assembly metadata …
costin-zaharia-sonarsource 416e76c
Remove redundant analyzer options
costin-zaharia-sonarsource 106e77e
Dispose metadata
costin-zaharia-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
analyzers/src/SonarAnalyzer.CSharp/Rules/FrameworkViewCompiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2024 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.IO; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
|
||
namespace SonarAnalyzer.Rules.CSharp; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class FrameworkViewCompiler : SonarDiagnosticAnalyzer | ||
{ | ||
private readonly HashSet<string> BadBoys = new() | ||
{ | ||
"S3904", // AssemblyVersion attribute | ||
"S3990", // CLSCompliant attribute | ||
"S3992", // ComVisible attribute | ||
"S1451", // License header | ||
"S103", // Lines too long | ||
"S104", // Files too many lines | ||
"S109", // Magic number | ||
"S113", // Files without newline | ||
"S1147", // Exit methods | ||
"S1192", // String literals duplicated | ||
"S1944", // Invalid cast | ||
"S1905", // Redundant cast | ||
"S1116", // Empty statements | ||
}; | ||
|
||
private ImmutableArray<DiagnosticAnalyzer> Rules; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } | ||
|
||
protected override bool EnableConcurrentExecution => false; | ||
|
||
public FrameworkViewCompiler() | ||
{ | ||
Rules = RuleFinder2.CreateAnalyzers(AnalyzerLanguage.CSharp, false) | ||
.Where(x => x.SupportedDiagnostics.All(d => !BadBoys.Contains(d.Id))) | ||
.ToImmutableArray(); | ||
|
||
SupportedDiagnostics = Rules.SelectMany(x => x.SupportedDiagnostics).ToImmutableArray(); | ||
} | ||
|
||
protected override void Initialize(SonarAnalysisContext context) => | ||
|
||
context.RegisterCompilationAction( | ||
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. We need to optionally register.
...probably more filtering required. |
||
c => | ||
{ | ||
// TODO: Maybe there is a better check, maybe use References(KnownAssembly for System.Web.Mvc) | ||
if (c.Compilation.GetTypeByMetadataName(KnownType.System_Web_Mvc_Controller) is null) | ||
{ | ||
return; | ||
} | ||
|
||
var projectConfiguration = c.ProjectConfiguration(); | ||
var root = Path.GetDirectoryName(projectConfiguration.ProjectPath); | ||
var dummy = CompileViews(c.Compilation, root).WithAnalyzers(Rules, c.Options); | ||
var diagnostics = dummy.GetAnalyzerDiagnosticsAsync().Result; | ||
foreach (var diagnostic in diagnostics) | ||
{ | ||
c.ReportIssue(diagnostic); | ||
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. See this thread. The main problem is that:
|
||
} | ||
}); | ||
|
||
Compilation CompileViews(Compilation compilation, string rootDir) | ||
{ | ||
FilesToAnalyzeProvider filesProvider = new(Directory.GetFiles(rootDir, "*.*", SearchOption.AllDirectories)); | ||
var razorCompiler = new RazorCompiler(rootDir, filesProvider); | ||
var dummyCompilation = compilation; | ||
|
||
var documents = razorCompiler.CompileAll(); | ||
var razorTrees = new List<SyntaxTree>(); | ||
|
||
var i = 0; | ||
|
||
foreach (var razorDocument in documents) | ||
{ | ||
if (razorDocument.GetCSharpDocument()?.GeneratedCode is { } csharpCode) | ||
{ | ||
var razorTree = CSharpSyntaxTree.ParseText( | ||
csharpCode, | ||
new CSharpParseOptions(compilation.GetLanguageVersion()), | ||
path: $"x_{i++}.cshtml.g.cs"); | ||
razorTrees.Add(razorTree); | ||
} | ||
} | ||
dummyCompilation = dummyCompilation.AddSyntaxTrees(razorTrees); | ||
return dummyCompilation; | ||
} | ||
} |
195 changes: 195 additions & 0 deletions
195
analyzers/src/SonarAnalyzer.CSharp/Rules/RazorCompiler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
using System.IO; | ||
using System.Text.RegularExpressions; | ||
using System.Xml.Linq; | ||
using System.Xml.XPath; | ||
using Microsoft.AspNetCore.Razor.Language; | ||
using Microsoft.AspNetCore.Razor.Language.CodeGeneration; | ||
using Microsoft.AspNetCore.Razor.Language.Intermediate; | ||
|
||
namespace SonarAnalyzer.Rules; | ||
|
||
// Copied from sonar-security. | ||
|
||
/// <summary> | ||
/// Compiler for manual Razor Views cross-compilation to C# code for .NET Framework projects. | ||
/// We don't need to manually compile .NET Core Razor projects. They are compiled during build time and analyzed directly with Roslyn analyzers. | ||
/// </summary> | ||
internal sealed class RazorCompiler | ||
{ | ||
public const string SonarRazorCompiledItemAttribute = nameof(SonarRazorCompiledItemAttribute); | ||
|
||
private static readonly Regex CshtmlFileRegex = new(@"\.cshtml$", RegexOptions.IgnoreCase, TimeSpan.FromSeconds(2000)); | ||
|
||
private readonly string rootDirectory; | ||
private readonly string[] viewPaths; | ||
private readonly Dictionary<string, Engine> engines = new(); // Cache for Razor engines based on the View and web.config directory. | ||
|
||
public RazorCompiler(string rootDirectory, FilesToAnalyzeProvider filesToAnalyzeProvider) | ||
{ | ||
this.rootDirectory = rootDirectory; | ||
viewPaths = filesToAnalyzeProvider.FindFiles(CshtmlFileRegex).ToArray(); | ||
if (viewPaths.Length != 0) | ||
{ | ||
BuildEngines(filesToAnalyzeProvider.FindFiles("web.config")); | ||
} | ||
} | ||
|
||
public IEnumerable<RazorCodeDocument> CompileAll() | ||
{ | ||
foreach (var viewPath in viewPaths) | ||
{ | ||
if (FindEngine(Path.GetDirectoryName(viewPath)) is { } engine) | ||
{ | ||
yield return engine.Compile(viewPath); | ||
} | ||
} | ||
} | ||
|
||
private void BuildEngines(IEnumerable<string> webConfigs) | ||
{ | ||
// Each web.config can change the list of imported namespace and we need a separate engine for it | ||
var razorFileSystem = RazorProjectFileSystem.Create(rootDirectory); | ||
foreach (var webConfigPath in webConfigs.OrderBy(x => x.Length)) | ||
{ | ||
var webConfig = File.ReadAllText(webConfigPath); | ||
if (webConfig.Contains("<system.web.webPages.razor") | ||
&& ParseXDocument(webConfig)?.XPathSelectElement("configuration/system.web.webPages.razor/pages/namespaces")?.Elements() is { } xmlElements | ||
&& xmlElements.Any()) | ||
{ | ||
var webConfigDir = Path.GetDirectoryName(webConfigPath); | ||
var namespaces = ReadNamespaces(webConfigDir, xmlElements); | ||
engines.Add(webConfigDir, new Engine(razorFileSystem, namespaces)); | ||
} | ||
} | ||
if (!engines.TryGetValue(rootDirectory, out var rootEngine)) | ||
{ | ||
rootEngine = new Engine(razorFileSystem, Array.Empty<string>()); | ||
} | ||
// Fill cache with values for each View directory | ||
foreach (var directory in viewPaths.Select(Path.GetDirectoryName).Distinct().OrderBy(x => x.Length)) | ||
{ | ||
if (!engines.ContainsKey(directory)) | ||
{ | ||
engines.Add(directory, FindEngine(directory) ?? rootEngine); | ||
} | ||
} | ||
} | ||
|
||
private string[] ReadNamespaces(string webConfigDir, IEnumerable<XElement> xmlElements) | ||
{ | ||
// web.config structure is hierarchical. Every level inherits parent's configuration and modifies it with Add/Clear operations. | ||
var ret = FindEngine(webConfigDir)?.Namespaces.ToList() ?? new List<string>(); | ||
foreach (var element in xmlElements) | ||
{ | ||
switch (element.Name.LocalName) | ||
{ | ||
case "add": | ||
var ns = element.Attribute("namespace")?.Value; | ||
if (!string.IsNullOrEmpty(ns)) | ||
{ | ||
ret.Add(ns); | ||
} | ||
break; | ||
|
||
case "clear": | ||
ret.Clear(); | ||
break; | ||
} | ||
} | ||
return ret.Distinct().ToArray(); | ||
} | ||
|
||
private Engine FindEngine(string directory) | ||
{ | ||
while (directory.StartsWith(rootDirectory, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
if (engines.ContainsKey(directory)) | ||
{ | ||
return engines[directory]; | ||
} | ||
directory = Path.GetDirectoryName(directory); | ||
} | ||
return null; | ||
} | ||
|
||
private static XDocument ParseXDocument(string text) | ||
{ | ||
try | ||
{ | ||
return XDocument.Parse(text); | ||
} | ||
catch | ||
{ | ||
return null; | ||
} | ||
} | ||
|
||
private sealed class Engine | ||
{ | ||
public readonly string[] Namespaces; | ||
private readonly RazorProjectEngine razorProjectEngine; | ||
|
||
public Engine(RazorProjectFileSystem razorFileSystem, string[] namespaces) | ||
{ | ||
Namespaces = namespaces; | ||
razorProjectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, razorFileSystem, builder => | ||
{ | ||
builder.AddDefaultImports(Namespaces.Select(x => $"@using {x}").ToArray()); | ||
builder.AddDirective(ModificationPass.Directive); | ||
builder.Features.Add(new ModificationPass()); | ||
var targetExtension = builder.Features.OfType<IRazorTargetExtensionFeature>().Single(); | ||
UpdateCompiledItemAttributeName(targetExtension.TargetExtensions.Single(x => x.GetType().Name == "MetadataAttributeTargetExtension")); | ||
}); | ||
} | ||
|
||
public RazorCodeDocument Compile(string viewPath) => | ||
razorProjectEngine.Process(razorProjectEngine.FileSystem.GetItem(viewPath, FileKinds.Legacy)); | ||
|
||
private static void UpdateCompiledItemAttributeName(ICodeTargetExtension metadataAttributeTargetExtension) => | ||
metadataAttributeTargetExtension.GetType().GetProperty("CompiledItemAttributeName").SetValue(metadataAttributeTargetExtension, "global::" + SonarRazorCompiledItemAttribute); | ||
} | ||
|
||
private sealed class ModificationPass : IRazorOptimizationPass | ||
{ | ||
// This represents the '@model' directive in Razor syntax tree. AddTypeToken() ensures that there will be exactly one (space separated) type token following the directive. | ||
// i.e.: @model ThisIs.Single.TypeToken | ||
public static readonly DirectiveDescriptor Directive = DirectiveDescriptor.CreateDirective("model", DirectiveKind.SingleLine, builder => builder.AddTypeToken()); | ||
|
||
public int Order => 1; // Must be higher than MetadataAttributePass.Order == 0, that generates the attribute nodes. | ||
public RazorEngine Engine { get; set; } | ||
|
||
public void Execute(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode) | ||
{ | ||
var namespaceNode = FindNode<NamespaceDeclarationIntermediateNode>(documentNode); | ||
var classNode = FindNode<ClassDeclarationIntermediateNode>(namespaceNode); | ||
var methodNode = FindNode<MethodDeclarationIntermediateNode>(classNode); | ||
var modelNode = FindNode<DirectiveIntermediateNode>(methodNode, x => x.Directive == Directive); | ||
documentNode.Children.Add(new SonarAttributeIntermediateNode()); | ||
namespaceNode.Children.Remove(namespaceNode.Children.Single(x => x.GetType().Name == "RazorSourceChecksumAttributeIntermediateNode")); | ||
classNode.BaseType = $"global::System.Web.Mvc.WebViewPage<{modelNode?.Tokens.Single().Content ?? "dynamic"}>"; | ||
methodNode.Modifiers.Remove("async"); | ||
methodNode.ReturnType = "void"; | ||
methodNode.MethodName = "Execute"; | ||
} | ||
|
||
private static TNode FindNode<TNode>(IntermediateNode parent, Func<TNode, bool> predicate = null) where TNode : IntermediateNode => | ||
parent.Children.OfType<TNode>().FirstOrDefault(x => predicate is null || predicate(x)); | ||
} | ||
|
||
private sealed class SonarAttributeIntermediateNode : ExtensionIntermediateNode | ||
{ | ||
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly; | ||
|
||
public override void Accept(IntermediateNodeVisitor visitor) => | ||
AcceptExtensionNode(this, visitor); | ||
|
||
public override void WriteNode(CodeTarget target, CodeRenderingContext context) => | ||
// The constructor signature must be same as the original Microsoft.AspNetCore.Razor.Hosting.RazorCompiledItemAttribute | ||
context.CodeWriter.WriteLine($$""" | ||
public sealed class {{SonarRazorCompiledItemAttribute}} : System.Attribute | ||
{ | ||
public SonarRazorCompiledItemAttribute(System.Type type, string kind, string identifier) { } | ||
} | ||
"""); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is the entrypoint.
The goal of this analyzer is to make one dummy compilation (or one dummy compilation per view file) and slap our analyzers on top of it. Then we can ask for diagnostics and filter accordingly.