Skip to content

Commit c9fab0b

Browse files
committed
C#: Change source generated razor file paths to be relative to csproj
1 parent b0062fc commit c9fab0b

File tree

5 files changed

+24
-29
lines changed

5 files changed

+24
-29
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Razor.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,25 @@ protected override void GenerateAnalyzerConfig(IEnumerable<string> cshtmls, stri
3434
using var sw = new StreamWriter(analyzerConfigPath);
3535
sw.WriteLine("is_global = true");
3636

37-
foreach (var f in cshtmls.Select(f => f.Replace('\\', '/')))
37+
foreach (var cshtml in cshtmls)
3838
{
39-
sw.WriteLine($"\n[{f}]");
40-
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(f)); // TODO: this should be the relative path of the file.
39+
var adjustedPath = cshtml.Replace('\\', '/');
40+
string? relativePath;
41+
42+
try
43+
{
44+
var csprojFolder = Path.GetDirectoryName(csprojFile);
45+
relativePath = csprojFolder is not null ? Path.GetRelativePath(csprojFolder, cshtml) : cshtml;
46+
relativePath = relativePath.Replace('\\', '/');
47+
}
48+
catch (Exception e)
49+
{
50+
logger.LogWarning($"Failed to get relative path for {cshtml}: {e.Message}");
51+
relativePath = adjustedPath;
52+
}
53+
54+
sw.WriteLine($"\n[{adjustedPath}]");
55+
var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(relativePath));
4156
sw.WriteLine($"build_metadata.AdditionalFiles.TargetPath = {base64}");
4257
}
4358
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| Program.cs |
22
| Views/Home/Index.cshtml |
33
| test-db/working/implicitUsings/GlobalUsings.g.cs |
4-
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/[...]_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_test_test_Views_Home_Index_cshtml.g.cs |
4+
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs |
Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
import csharp
22

3-
private string getPath(File f) {
4-
result = f.getRelativePath() and
5-
not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_"))
6-
or
7-
exists(int index1, int index2, string pattern |
8-
pattern = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and
9-
index1 = f.getRelativePath().indexOf(pattern) and
10-
index2 =
11-
f.getRelativePath()
12-
.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
13-
result =
14-
f.getRelativePath().substring(0, index1 + pattern.length()) + "/[...]" +
15-
f.getRelativePath().substring(index2, f.getRelativePath().length())
16-
)
17-
}
18-
193
from File f
204
where f.fromSource() or f.getExtension() = "cshtml"
21-
select getPath(f)
5+
select f.getRelativePath()
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
| Program.cs |
22
| Views/Home/Index.cshtml |
33
| test-db/working/implicitUsings/GlobalUsings.g.cs |
4-
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/[...]/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/[...]_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_net6_test_test_Views_Home_Index_cshtml.g.cs |
4+
| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/[...]/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs |

csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,17 @@ import csharp
22

33
private string getPath(File f) {
44
result = f.getRelativePath() and
5-
not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_"))
5+
not exists(result.indexOf("EC52D77FE9BF67AD10C5C3F248392316"))
66
or
7-
exists(int index0, int index1, int index2, string pattern0, string pattern1 |
7+
exists(int index0, int index1, string pattern0, string pattern1 |
88
// TODO: Remove index0 and pattern0. Currently there's some instability in the path depending on which dotnet SDK is being used. (See issue #448)
99
pattern0 = "EC52D77FE9BF67AD10C5C3F248392316" and
1010
index0 = f.getRelativePath().indexOf(pattern0) and
1111
pattern1 = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and
1212
index1 = f.getRelativePath().indexOf(pattern1) and
13-
index2 =
14-
f.getRelativePath()
15-
.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and
1613
result =
1714
f.getRelativePath().substring(0, index0 + pattern0.length()) + "/[...]/" +
18-
f.getRelativePath().substring(index1, index1 + pattern1.length()) + "/[...]" +
19-
f.getRelativePath().substring(index2, f.getRelativePath().length())
15+
f.getRelativePath().substring(index1, f.getRelativePath().length())
2016
)
2117
}
2218

0 commit comments

Comments
 (0)