From 69d89cd20b66564b1e74e4739ffd501cdbc394a5 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Fri, 21 Jun 2024 10:11:45 +0200 Subject: [PATCH] Compute unique identifier (folder path) for each compilation --- .../Extractor/Extractor.cs | 16 ++++++++++++++-- csharp/extractor/Semmle.Util/FileUtils.cs | 19 +++++++++++++------ .../all-platforms/binlog/Files.expected | 3 ++- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs b/csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs index 94cb59e9ceffd..d4c9f07f7447e 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp/Extractor/Extractor.cs @@ -155,8 +155,20 @@ static bool filter(CompilerCall compilerCall) var compilerCall = compilationData.CompilerCall; var diagnosticName = compilerCall.GetDiagnosticName(); - logger.LogInfo($" Processing compilation {diagnosticName}"); + logger.LogInfo($" Processing compilation {diagnosticName} at {compilerCall.ProjectDirectory}"); var compilerArgs = compilerCall.GetArguments(); + + var compilationIdentifierPath = string.Empty; + try + { + compilationIdentifierPath = FileUtils.ConvertPathToSafeRelativePath( + Path.GetRelativePath(Directory.GetCurrentDirectory(), compilerCall.ProjectDirectory)); + } + catch (ArgumentException exc) + { + logger.LogWarning($" Failed to get relative path for {compilerCall.ProjectDirectory} from current working directory {Directory.GetCurrentDirectory()}: {exc.Message}"); + } + var args = reader.ReadCommandLineArguments(compilerCall); // Generated syntax trees are always added to the end of the list of syntax trees. @@ -174,7 +186,7 @@ static bool filter(CompilerCall compilerCall) TracingAnalyser.GetOutputName(compilation, args), compilation, generatedSyntaxTrees, - diagnosticName, + Path.Combine(compilationIdentifierPath, diagnosticName), options), () => { }); diff --git a/csharp/extractor/Semmle.Util/FileUtils.cs b/csharp/extractor/Semmle.Util/FileUtils.cs index 4d9052bcc4eaa..92087645fd1c4 100644 --- a/csharp/extractor/Semmle.Util/FileUtils.cs +++ b/csharp/extractor/Semmle.Util/FileUtils.cs @@ -113,17 +113,24 @@ private static async Task DownloadFileAsync(string address, string filename) public static void DownloadFile(string address, string fileName) => DownloadFileAsync(address, fileName).GetAwaiter().GetResult(); + public static string ConvertPathToSafeRelativePath(string path) + { + // Remove all leading path separators / or \ + // For example, UNC paths have two leading \\ + path = path.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + + if (path.Length > 1 && path[1] == ':') + path = path[0] + "_" + path.Substring(2); + + return path; + } + public static string NestPaths(ILogger logger, string? outerpath, string innerpath) { var nested = innerpath; if (!string.IsNullOrEmpty(outerpath)) { - // Remove all leading path separators / or \ - // For example, UNC paths have two leading \\ - innerpath = innerpath.TrimStart(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); - - if (innerpath.Length > 1 && innerpath[1] == ':') - innerpath = innerpath[0] + "_" + innerpath.Substring(2); + innerpath = ConvertPathToSafeRelativePath(innerpath); nested = Path.Combine(outerpath, innerpath); } diff --git a/csharp/ql/integration-tests/all-platforms/binlog/Files.expected b/csharp/ql/integration-tests/all-platforms/binlog/Files.expected index 8b3aae7617b73..bf3694ff9caa5 100644 --- a/csharp/ql/integration-tests/all-platforms/binlog/Files.expected +++ b/csharp/ql/integration-tests/all-platforms/binlog/Files.expected @@ -6,4 +6,5 @@ | b/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs:0:0:0:0 | b/obj/Debug/net8.0/.NETCoreApp,Version=v8.0.AssemblyAttributes.cs | | b/obj/Debug/net8.0/test.AssemblyInfo.cs:0:0:0:0 | b/obj/Debug/net8.0/test.AssemblyInfo.cs | | b/obj/Debug/net8.0/test.GlobalUsings.g.cs:0:0:0:0 | b/obj/Debug/net8.0/test.GlobalUsings.g.cs | -| generated/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs:0:0:0:0 | generated/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs | +| generated/a/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs:0:0:0:0 | generated/a/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs | +| generated/b/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs:0:0:0:0 | generated/b/test.csproj (net8.0)/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs |