Skip to content

Commit 23f3e44

Browse files
committed
C#: Use nuget.config file for dotnet restore fallback logic
1 parent 139585f commit 23f3e44

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

csharp/extractor/Semmle.Extraction.CSharp.Standalone/BuildAnalysis.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ public BuildAnalysis(Options options, ProgressMonitor progressMonitor)
133133
DateTime.Now - startTime);
134134
}
135135

136-
private IEnumerable<string> GetFiles(string pattern)
136+
private IEnumerable<string> GetFiles(string pattern, bool recurseSubdirectories = true)
137137
{
138-
return sourceDir.GetFiles(pattern, SearchOption.AllDirectories)
138+
return sourceDir.GetFiles(pattern, new EnumerationOptions { RecurseSubdirectories = recurseSubdirectories, MatchCasing = MatchCasing.CaseInsensitive })
139139
.Select(d => d.FullName)
140140
.Where(d => !options.ExcludesFile(d));
141141
}
@@ -318,16 +318,16 @@ private void AnalyseProject(FileInfo project)
318318

319319
}
320320

321-
private bool Restore(string target)
321+
private bool Restore(string target, string? pathToNugetConfig = null)
322322
{
323-
return dotnet.RestoreToDirectory(target, packageDirectory.DirInfo.FullName);
323+
return dotnet.RestoreToDirectory(target, packageDirectory.DirInfo.FullName, pathToNugetConfig);
324324
}
325325

326-
private void Restore(IEnumerable<string> targets)
326+
private void Restore(IEnumerable<string> targets, string? pathToNugetConfig = null)
327327
{
328328
foreach (var target in targets)
329329
{
330-
Restore(target);
330+
Restore(target, pathToNugetConfig);
331331
}
332332
}
333333

@@ -336,7 +336,23 @@ private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
336336
var alreadyDownloadedPackages = Directory.GetDirectories(packageDirectory.DirInfo.FullName).Select(d => Path.GetFileName(d).ToLowerInvariant()).ToHashSet();
337337
var notYetDownloadedPackages = new HashSet<string>();
338338

339-
var allFiles = GetFiles("*.*").ToArray();
339+
var nugetConfigs = GetFiles("nuget.config", recurseSubdirectories: true).ToArray();
340+
string? nugetConfig = null;
341+
if (nugetConfigs.Length > 1)
342+
{
343+
progressMonitor.MultipleNugetConfig(nugetConfigs);
344+
nugetConfig = GetFiles("nuget.config", recurseSubdirectories: false).FirstOrDefault();
345+
if (nugetConfig == null)
346+
{
347+
progressMonitor.NoTopLevelNugetConfig();
348+
}
349+
}
350+
else
351+
{
352+
nugetConfig = nugetConfigs.FirstOrDefault();
353+
}
354+
355+
var allFiles = GetFiles("*.*");
340356
foreach (var file in allFiles)
341357
{
342358
try
@@ -390,7 +406,7 @@ private void DownloadMissingPackages(IEnumerable<string> restoreTargets)
390406
continue;
391407
}
392408

393-
success = Restore(tempDir.DirInfo.FullName);
409+
success = Restore(tempDir.DirInfo.FullName, nugetConfig);
394410

395411
// TODO: the restore might fail, we could retry with a prerelease (*-* instead of *) version of the package.
396412

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,11 @@ private bool RunCommand(string args)
4545
return true;
4646
}
4747

48-
public bool RestoreToDirectory(string projectOrSolutionFile, string packageDirectory)
48+
public bool RestoreToDirectory(string projectOrSolutionFile, string packageDirectory, string? pathToNugetConfig = null)
4949
{
5050
var args = $"restore --no-dependencies \"{projectOrSolutionFile}\" --packages \"{packageDirectory}\" /p:DisableImplicitNuGetFallbackFolder=true";
51+
if (pathToNugetConfig != null)
52+
args += $" --configfile \"{pathToNugetConfig}\"";
5153
return RunCommand(args);
5254
}
5355

csharp/extractor/Semmle.Extraction.CSharp.Standalone/ProgressMonitor.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,5 +118,15 @@ public void FailedToReadFile(string file, Exception ex)
118118
logger.Log(Severity.Info, $"Failed to read file {file}");
119119
logger.Log(Severity.Debug, $"Failed to read file {file}, exception: {ex}");
120120
}
121+
122+
public void MultipleNugetConfig(string[] nugetConfigs)
123+
{
124+
logger.Log(Severity.Info, $"Found multiple nuget.config files: {string.Join(", ", nugetConfigs)}.");
125+
}
126+
127+
internal void NoTopLevelNugetConfig()
128+
{
129+
logger.Log(Severity.Info, $"Could not find a top-level nuget.config file.");
130+
}
121131
}
122132
}

0 commit comments

Comments
 (0)