Skip to content

Commit

Permalink
Merge pull request #15454 from tamasvajk/standalone/cshtml-default
Browse files Browse the repository at this point in the history
C#: Change asp.net core view generation to be opt out
  • Loading branch information
tamasvajk authored Jan 29, 2024
2 parents 79d9109 + 4dffb3b commit 39b32a9
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
}

var webViewExtractionOption = Environment.GetEnvironmentVariable("CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS");
if (bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
if (webViewExtractionOption == null ||
bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
shouldExtractWebViews)
{
logger.LogInfo("Generating source files from cshtml and razor files...");
GenerateSourceFilesFromWebViews(allNonBinaryFiles);
}

Expand Down Expand Up @@ -334,9 +334,14 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllP
}
}

private bool IsAspNetCoreDetected()
{
return fileContent.IsNewProjectStructureUsed && fileContent.UseAspNetCoreDlls;
}

private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLocations)
{
if (!fileContent.IsNewProjectStructureUsed || !fileContent.UseAspNetCoreDlls)
if (!IsAspNetCoreDetected())
{
return;
}
Expand Down Expand Up @@ -445,6 +450,14 @@ private void GenerateSourceFilesFromWebViews(List<FileInfo> allFiles)

logger.LogInfo($"Found {views.Length} cshtml and razor files.");

if (!IsAspNetCoreDetected())
{
logger.LogInfo("Generating source files from cshtml files is only supported for new (SDK-style) project files");
return;
}

logger.LogInfo("Generating source files from cshtml and razor files...");

var sdk = new Sdk(dotnet).GetNewestSdk();
if (sdk != null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
from create_database_utils import *


os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'true'
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| Program.cs |
| test-db/working/implicitUsings/GlobalUsings.g.cs |
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import csharp

private string getPath(File f) {
result = f.getRelativePath() and
not exists(
result
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")
)
or
exists(int index |
index =
f.getRelativePath()
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
result = f.getRelativePath().substring(index, f.getRelativePath().length())
)
}

from File f
where f.fromSource() or f.getExtension() = "cshtml"
select getPath(f)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var dummy = "dummy";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
<RemoveDir Directories=".\bin" />
<RemoveDir Directories=".\obj" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "8.0.100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import os
from create_database_utils import *

os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'false'
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
from create_database_utils import *


os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'true'
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Program.cs |
| Views/Home/Index.cshtml |
| _semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_net6_Views_Home_Index_cshtml.g.cs |
| test-db/working/implicitUsings/GlobalUsings.g.cs |
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import csharp

private string getPath(File f) {
result = f.getRelativePath() and
not exists(
result
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")
)
or
exists(int index |
index =
f.getRelativePath()
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
result = f.getRelativePath().substring(index, f.getRelativePath().length())
)
}

from File f
where f.fromSource() or f.getExtension() = "cshtml"
select getPath(f)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var dummy = "dummy";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@{
ViewData["Title"] = "Home Page";
}

<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
<RemoveDir Directories=".\bin" />
<RemoveDir Directories=".\obj" />
</Target>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"sdk": {
"version": "6.0.418"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
from create_database_utils import *

run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])

0 comments on commit 39b32a9

Please sign in to comment.