diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Razor.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Razor.cs index d8bf815a5806..24423e6a129f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Razor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorWrapper/Razor.cs @@ -34,10 +34,25 @@ protected override void GenerateAnalyzerConfig(IEnumerable cshtmls, stri using var sw = new StreamWriter(analyzerConfigPath); sw.WriteLine("is_global = true"); - foreach (var f in cshtmls.Select(f => f.Replace('\\', '/'))) + foreach (var cshtml in cshtmls) { - sw.WriteLine($"\n[{f}]"); - var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(f)); // TODO: this should be the relative path of the file. + var adjustedPath = cshtml.Replace('\\', '/'); + string? relativePath; + + try + { + var csprojFolder = Path.GetDirectoryName(csprojFile); + relativePath = csprojFolder is not null ? Path.GetRelativePath(csprojFolder, cshtml) : cshtml; + relativePath = relativePath.Replace('\\', '/'); + } + catch (Exception e) + { + logger.LogWarning($"Failed to get relative path for {cshtml}: {e.Message}"); + relativePath = adjustedPath; + } + + sw.WriteLine($"\n[{adjustedPath}]"); + var base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(relativePath)); sw.WriteLine($"build_metadata.AdditionalFiles.TargetPath = {base64}"); } } diff --git a/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.expected b/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.expected index f64a66be22f2..2b6fea50a1bf 100644 --- a/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.expected +++ b/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.expected @@ -1,4 +1,4 @@ | Program.cs | | Views/Home/Index.cshtml | | test-db/working/implicitUsings/GlobalUsings.g.cs | -| 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 | +| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/Microsoft.CodeAnalysis.Razor.Compiler/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs | diff --git a/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.ql b/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.ql index 2ab3af8e13e7..c0891b2e6574 100644 --- a/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.ql +++ b/csharp/ql/integration-tests/all-platforms/cshtml_standalone/Files.ql @@ -1,21 +1,5 @@ import csharp -private string getPath(File f) { - result = f.getRelativePath() and - not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")) - or - exists(int index1, int index2, string pattern | - pattern = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and - index1 = f.getRelativePath().indexOf(pattern) and - index2 = - f.getRelativePath() - .indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and - result = - f.getRelativePath().substring(0, index1 + pattern.length()) + "/[...]" + - f.getRelativePath().substring(index2, f.getRelativePath().length()) - ) -} - from File f where f.fromSource() or f.getExtension() = "cshtml" -select getPath(f) +select f.getRelativePath() diff --git a/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.expected b/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.expected index 65357c4e3600..4059eb703336 100644 --- a/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.expected +++ b/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.expected @@ -1,4 +1,4 @@ | Program.cs | | Views/Home/Index.cshtml | | test-db/working/implicitUsings/GlobalUsings.g.cs | -| 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 | +| test-db/working/razor/EC52D77FE9BF67AD10C5C3F248392316/[...]/Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator/Views_Home_Index_cshtml.g.cs | diff --git a/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql b/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql index f59129efe945..2dfcb116736e 100644 --- a/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql +++ b/csharp/ql/integration-tests/all-platforms/cshtml_standalone_net6/Files.ql @@ -2,21 +2,17 @@ import csharp private string getPath(File f) { result = f.getRelativePath() and - not exists(result.indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_")) + not exists(result.indexOf("EC52D77FE9BF67AD10C5C3F248392316")) or - exists(int index0, int index1, int index2, string pattern0, string pattern1 | + exists(int index0, int index1, string pattern0, string pattern1 | // TODO: Remove index0 and pattern0. Currently there's some instability in the path depending on which dotnet SDK is being used. (See issue #448) pattern0 = "EC52D77FE9BF67AD10C5C3F248392316" and index0 = f.getRelativePath().indexOf(pattern0) and pattern1 = "Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator" and index1 = f.getRelativePath().indexOf(pattern1) and - index2 = - f.getRelativePath() - .indexOf("_ql_csharp_ql_integration_tests_all_platforms_cshtml_standalone_") and result = f.getRelativePath().substring(0, index0 + pattern0.length()) + "/[...]/" + - f.getRelativePath().substring(index1, index1 + pattern1.length()) + "/[...]" + - f.getRelativePath().substring(index2, f.getRelativePath().length()) + f.getRelativePath().substring(index1, f.getRelativePath().length()) ) } diff --git a/csharp/ql/lib/change-notes/2025-01-06-razor-relative-path.md b/csharp/ql/lib/change-notes/2025-01-06-razor-relative-path.md new file mode 100644 index 000000000000..dcb481739459 --- /dev/null +++ b/csharp/ql/lib/change-notes/2025-01-06-razor-relative-path.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* The Razor source generator invocation in `build-mode:none` extraction has been changed to use relative file paths instead of absolute ones.