Skip to content

Commit 39b32a9

Browse files
authored
Merge pull request #15454 from tamasvajk/standalone/cshtml-default
C#: Change asp.net core view generation to be opt out
2 parents 79d9109 + 4dffb3b commit 39b32a9

File tree

17 files changed

+127
-7
lines changed

17 files changed

+127
-7
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DependencyManager.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public DependencyManager(string srcDir, IDependencyOptions options, ILogger logg
112112
}
113113

114114
var webViewExtractionOption = Environment.GetEnvironmentVariable("CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS");
115-
if (bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
115+
if (webViewExtractionOption == null ||
116+
bool.TryParse(webViewExtractionOption, out var shouldExtractWebViews) &&
116117
shouldExtractWebViews)
117118
{
118-
logger.LogInfo("Generating source files from cshtml and razor files...");
119119
GenerateSourceFilesFromWebViews(allNonBinaryFiles);
120120
}
121121

@@ -334,9 +334,14 @@ private void RemoveNugetPackageReference(string packagePrefix, ISet<string> dllP
334334
}
335335
}
336336

337+
private bool IsAspNetCoreDetected()
338+
{
339+
return fileContent.IsNewProjectStructureUsed && fileContent.UseAspNetCoreDlls;
340+
}
341+
337342
private void AddAspNetCoreFrameworkDlls(ISet<string> dllPaths, ISet<string> frameworkLocations)
338343
{
339-
if (!fileContent.IsNewProjectStructureUsed || !fileContent.UseAspNetCoreDlls)
344+
if (!IsAspNetCoreDetected())
340345
{
341346
return;
342347
}
@@ -445,6 +450,14 @@ private void GenerateSourceFilesFromWebViews(List<FileInfo> allFiles)
445450

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

453+
if (!IsAspNetCoreDetected())
454+
{
455+
logger.LogInfo("Generating source files from cshtml files is only supported for new (SDK-style) project files");
456+
return;
457+
}
458+
459+
logger.LogInfo("Generating source files from cshtml and razor files...");
460+
448461
var sdk = new Sdk(dotnet).GetNewestSdk();
449462
if (sdk != null)
450463
{
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import os
22
from create_database_utils import *
33

4-
5-
os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'true'
64
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Program.cs |
2+
| test-db/working/implicitUsings/GlobalUsings.g.cs |
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import csharp
2+
3+
private string getPath(File f) {
4+
result = f.getRelativePath() and
5+
not exists(
6+
result
7+
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")
8+
)
9+
or
10+
exists(int index |
11+
index =
12+
f.getRelativePath()
13+
.indexOf("_semmle_code_target_codeql_csharp_integration_tests_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
14+
result = f.getRelativePath().substring(index, f.getRelativePath().length())
15+
)
16+
}
17+
18+
from File f
19+
where f.fromSource() or f.getExtension() = "cshtml"
20+
select getPath(f)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var dummy = "dummy";
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div class="text-center">
6+
<h1 class="display-4">Welcome</h1>
7+
<p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
8+
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
11+
<RemoveDir Directories=".\bin" />
12+
<RemoveDir Directories=".\obj" />
13+
</Target>
14+
</Project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.100"
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
from create_database_utils import *
3+
4+
os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'false'
5+
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import os
22
from create_database_utils import *
33

4-
5-
os.environ['CODEQL_EXTRACTOR_CSHARP_STANDALONE_EXTRACT_WEB_VIEWS'] = 'true'
64
run_codeql_database_create(lang="csharp", extra_args=["--extractor-option=buildless=true", "--extractor-option=cil=false"])

0 commit comments

Comments
 (0)