Skip to content

Commit f29d2c2

Browse files
authored
Merge pull request #16312 from tamasvajk/fix/buildless/file-lookup
C#: Fix `global.json` and `packages.config` lookup
2 parents 3b44b13 + 4a97f95 commit f29d2c2

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ private static BuildScript DownloadDotNet(IBuildActions actions, ILogger logger,
143143
// See https://docs.microsoft.com/en-us/dotnet/core/tools/global-json
144144
var versions = new List<string>();
145145

146-
foreach (var path in files.Where(p => p.EndsWith("global.json", StringComparison.Ordinal)))
146+
foreach (var path in files.Where(p => string.Equals(FileUtils.SafeGetFileName(p, logger), "global.json", StringComparison.OrdinalIgnoreCase)))
147147
{
148148
try
149149
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/FileContent.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ private void DoInitialize()
184184
{
185185
try
186186
{
187-
var isPackagesConfig = file.EndsWith("packages.config", StringComparison.OrdinalIgnoreCase);
187+
var isPackagesConfig = string.Equals(FileUtils.SafeGetFileName(file, logger), "packages.config", StringComparison.OrdinalIgnoreCase);
188188

189189
foreach (ReadOnlySpan<char> line in unsafeFileReader.ReadLines(file))
190190
{

csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/SourceGenerators/DotnetSourceGeneratorBase.cs

+1-25
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected override IEnumerable<string> Run()
5555

5656
// group additional files by closes project file:
5757
var projects = fileProvider.Projects
58-
.Select(p => (File: p, Directory: SafeGetDirectoryName(p)))
58+
.Select(p => (File: p, Directory: FileUtils.SafeGetDirectoryName(p, logger)))
5959
.Where(p => p.Directory.Length > 0);
6060

6161
var groupedFiles = new Dictionary<string, List<string>>();
@@ -93,30 +93,6 @@ protected override IEnumerable<string> Run()
9393
}
9494
}
9595

96-
private string SafeGetDirectoryName(string fileName)
97-
{
98-
try
99-
{
100-
var dir = Path.GetDirectoryName(fileName);
101-
if (dir is null)
102-
{
103-
return "";
104-
}
105-
106-
if (!dir.EndsWith(Path.DirectorySeparatorChar))
107-
{
108-
dir += Path.DirectorySeparatorChar;
109-
}
110-
111-
return dir;
112-
}
113-
catch (Exception ex)
114-
{
115-
logger.LogDebug($"Failed to get directory name for {fileName}: {ex.Message}");
116-
return "";
117-
}
118-
}
119-
12096
protected abstract ICollection<string> AdditionalFiles { get; }
12197

12298
protected abstract string FileType { get; }

csharp/extractor/Semmle.Util/FileUtils.cs

+37
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,42 @@ public static FileInfo CreateTemporaryFile(string extension, out bool shouldClea
185185

186186
return new FileInfo(outputPath);
187187
}
188+
189+
public static string SafeGetDirectoryName(string path, ILogger logger)
190+
{
191+
try
192+
{
193+
var dir = Path.GetDirectoryName(path);
194+
if (dir is null)
195+
{
196+
return "";
197+
}
198+
199+
if (!dir.EndsWith(Path.DirectorySeparatorChar))
200+
{
201+
dir += Path.DirectorySeparatorChar;
202+
}
203+
204+
return dir;
205+
}
206+
catch (Exception ex)
207+
{
208+
logger.LogDebug($"Failed to get directory name for {path}: {ex.Message}");
209+
return "";
210+
}
211+
}
212+
213+
public static string? SafeGetFileName(string path, ILogger logger)
214+
{
215+
try
216+
{
217+
return Path.GetFileName(path);
218+
}
219+
catch (Exception ex)
220+
{
221+
logger.LogDebug($"Failed to get file name for {path}: {ex.Message}");
222+
return null;
223+
}
224+
}
188225
}
189226
}

0 commit comments

Comments
 (0)