From f3daba510bd772cbc878212ae12ef123d4db134e Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 24 Apr 2024 11:57:34 +0200 Subject: [PATCH 1/2] C#: Fix `global.json` and `packages.config` lookup --- .../Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs | 2 +- .../Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index c573c5ff4e6..74c1a37154d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -143,7 +143,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching // See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json var versions = new List(); - foreach (var path in files.Where(p => p.EndsWith("global.json", StringComparison.Ordinal))) + foreach (var path in files.Where(p => p.EndsWith(Path.DirectorySeparatorChar + "global.json", StringComparison.Ordinal))) { try { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs index 44cff0a8085..467289c7e0d 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs @@ -184,7 +184,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching { try { - var isPackagesConfig = file.EndsWith("packages.config", StringComparison.OrdinalIgnoreCase); + var isPackagesConfig = file.EndsWith(Path.DirectorySeparatorChar + "packages.config", StringComparison.OrdinalIgnoreCase); foreach (ReadOnlySpan line in unsafeFileReader.ReadLines(file)) { From 4a97f9589017937fd4f40f9a9a2d7c76e1b01452 Mon Sep 17 00:00:00 2001 From: Tamas Vajk Date: Wed, 24 Apr 2024 13:47:25 +0200 Subject: [PATCH 2/2] Improve code quality --- .../DotNet.cs | 2 +- .../FileContent.cs | 2 +- .../DotnetSourceGeneratorBase.cs | 26 +------------ csharp/extractor/Semmle.Util/FileUtils.cs | 37 +++++++++++++++++++ 4 files changed, 40 insertions(+), 27 deletions(-) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index 74c1a37154d..95e8fe0675f 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -143,7 +143,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching // See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json var versions = new List(); - foreach (var path in files.Where(p => p.EndsWith(Path.DirectorySeparatorChar + "global.json", StringComparison.Ordinal))) + foreach (var path in files.Where(p => string.Equals(FileUtils.SafeGetFileName(p, logger), "global.json", StringComparison.OrdinalIgnoreCase))) { try { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs index 467289c7e0d..f33329046cf 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs @@ -184,7 +184,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching { try { - var isPackagesConfig = file.EndsWith(Path.DirectorySeparatorChar + "packages.config", StringComparison.OrdinalIgnoreCase); + var isPackagesConfig = string.Equals(FileUtils.SafeGetFileName(file, logger), "packages.config", StringComparison.OrdinalIgnoreCase); foreach (ReadOnlySpan line in unsafeFileReader.ReadLines(file)) { diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs index 4d353ffbeea..461590348df 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs @@ -55,7 +55,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching // group additional files by closes project file: var projects = fileProvider.Projects - .Select(p => (File: p, Directory: SafeGetDirectoryName(p))) + .Select(p => (File: p, Directory: FileUtils.SafeGetDirectoryName(p, logger))) .Where(p => p.Directory.Length > 0); var groupedFiles = new Dictionary>(); @@ -93,30 +93,6 @@ namespace Semmle.Extraction.CSharp.DependencyFetching } } - private string SafeGetDirectoryName(string fileName) - { - try - { - var dir = Path.GetDirectoryName(fileName); - if (dir is null) - { - return ""; - } - - if (!dir.EndsWith(Path.DirectorySeparatorChar)) - { - dir += Path.DirectorySeparatorChar; - } - - return dir; - } - catch (Exception ex) - { - logger.LogDebug($"Failed to get directory name for {fileName}: {ex.Message}"); - return ""; - } - } - protected abstract ICollection AdditionalFiles { get; } protected abstract string FileType { get; } diff --git a/csharp/extractor/Semmle.Util/FileUtils.cs b/csharp/extractor/Semmle.Util/FileUtils.cs index 4a22877e3c1..4d9052bcc4e 100644 --- a/csharp/extractor/Semmle.Util/FileUtils.cs +++ b/csharp/extractor/Semmle.Util/FileUtils.cs @@ -185,5 +185,42 @@ namespace Semmle.Util return new FileInfo(outputPath); } + + public static string SafeGetDirectoryName(string path, ILogger logger) + { + try + { + var dir = Path.GetDirectoryName(path); + if (dir is null) + { + return ""; + } + + if (!dir.EndsWith(Path.DirectorySeparatorChar)) + { + dir += Path.DirectorySeparatorChar; + } + + return dir; + } + catch (Exception ex) + { + logger.LogDebug($"Failed to get directory name for {path}: {ex.Message}"); + return ""; + } + } + + public static string? SafeGetFileName(string path, ILogger logger) + { + try + { + return Path.GetFileName(path); + } + catch (Exception ex) + { + logger.LogDebug($"Failed to get file name for {path}: {ex.Message}"); + return null; + } + } } }