Skip to content
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
wants to merge 11 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions analyzers/src/SonarAnalyzer.CSharp.Styling/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@
"resolved": "2.14.1",
"contentHash": "lQKvtaTDOXnoVJ20ibTuSIOf2i0uO0MPbDhd1jm238I+U/2ZnRENj0cktKZhtchBMtCUSRQ5v4xBCUbKNmyVMw=="
},
"Microsoft.AspNetCore.Razor.Language": {
"type": "Transitive",
"resolved": "6.0.29",
"contentHash": "GqbAfFQEv9XKi+QRlhzoYJvbi9EkFo0rkzvaP1xGvn2Hjn1DrhGK93UuJ8zLw8FQ9jmW1GOMLXLSPJoID4lLbg=="
},
"Microsoft.Bcl.AsyncInterfaces": {
"type": "Transitive",
"resolved": "8.0.0",
Expand Down Expand Up @@ -282,6 +287,7 @@
"sonaranalyzer.csharp": {
"type": "Project",
"dependencies": {
"Microsoft.AspNetCore.Razor.Language": "[6.0.29, )",
"Microsoft.CodeAnalysis.CSharp.Workspaces": "[1.3.2, )",
"SonarAnalyzer": "[1.0.0, )",
"System.Collections.Immutable": "[1.1.37, )"
Expand Down
107 changes: 107 additions & 0 deletions analyzers/src/SonarAnalyzer.CSharp/Rules/FrameworkViewCompiler.cs
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
Copy link
Contributor Author

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.

{
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(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to optionally register.

  • Opt-in only for .NET FM projects that contain cshtml files.
  • Opt-out on SonarLint.

...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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this thread.

The main problem is that:

  • On .NET Core, the Views are part of the compilation...but we do not care, as they are compiled anyways and we get analysis (almost) for free.
  • On .NET Framework the Views are NOT part of the compilation, so if we propagate the diagnostic with ReportIssue, roslyn throws an AD0001.

}
});

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 analyzers/src/SonarAnalyzer.CSharp/Rules/RazorCompiler.cs
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) { }
}
""");
}
}
Loading