Skip to content

Commit

Permalink
Merge pull request #16312 from tamasvajk/fix/buildless/file-lookup
Browse files Browse the repository at this point in the history
C#: Fix `global.json` and `packages.config` lookup
  • Loading branch information
tamasvajk authored Apr 24, 2024
2 parents 3b44b13 + 4a97f95 commit f29d2c2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ private static BuildScript DownloadDotNet(IBuildActions actions, ILogger logger,
// See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
var versions = new List<string>();

foreach (var path in files.Where(p => p.EndsWith("global.json", StringComparison.Ordinal)))
foreach (var path in files.Where(p => string.Equals(FileUtils.SafeGetFileName(p, logger), "global.json", StringComparison.OrdinalIgnoreCase)))
{
try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private void DoInitialize()
{
try
{
var isPackagesConfig = file.EndsWith("packages.config", StringComparison.OrdinalIgnoreCase);
var isPackagesConfig = string.Equals(FileUtils.SafeGetFileName(file, logger), "packages.config", StringComparison.OrdinalIgnoreCase);

foreach (ReadOnlySpan<char> line in unsafeFileReader.ReadLines(file))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected override IEnumerable<string> Run()

// 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<string, List<string>>();
Expand Down Expand Up @@ -93,30 +93,6 @@ protected override IEnumerable<string> Run()
}
}

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<string> AdditionalFiles { get; }

protected abstract string FileType { get; }
Expand Down
37 changes: 37 additions & 0 deletions csharp/extractor/Semmle.Util/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,42 @@ public static FileInfo CreateTemporaryFile(string extension, out bool shouldClea

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;
}
}
}
}

0 comments on commit f29d2c2

Please sign in to comment.