C#: Check fallback nuget feeds before trying to use them in the fallback restore process

This commit is contained in:
Tamas Vajk
2024-04-09 15:01:20 +02:00
parent 161f586510
commit 0f7fc90fe0
10 changed files with 180 additions and 31 deletions

View File

@@ -21,7 +21,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
this.Exec = exec;
}
private ProcessStartInfo MakeDotnetStartInfo(string args)
private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirectory)
{
var startInfo = new ProcessStartInfo(Exec, args)
{
@@ -29,6 +29,10 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
RedirectStandardOutput = true,
RedirectStandardError = true
};
if (!string.IsNullOrWhiteSpace(workingDirectory))
{
startInfo.WorkingDirectory = workingDirectory;
}
// Set the .NET CLI language to English to avoid localized output.
startInfo.EnvironmentVariables["DOTNET_CLI_UI_LANGUAGE"] = "en";
startInfo.EnvironmentVariables["MSBUILDDISABLENODEREUSE"] = "1";
@@ -36,26 +40,30 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return startInfo;
}
private bool RunCommandAux(string args, out IList<string> output)
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output)
{
logger.LogInfo($"Running {Exec} {args}");
var pi = MakeDotnetStartInfo(args);
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
logger.LogInfo($"Running {Exec} {args}{dirLog}");
var pi = MakeDotnetStartInfo(args, workingDirectory);
var threadId = Environment.CurrentManagedThreadId;
void onOut(string s) => logger.LogInfo(s, threadId);
void onError(string s) => logger.LogError(s, threadId);
var exitCode = pi.ReadOutput(out output, onOut, onError);
if (exitCode != 0)
{
logger.LogError($"Command {Exec} {args} failed with exit code {exitCode}");
logger.LogError($"Command {Exec} {args}{dirLog} failed with exit code {exitCode}");
return false;
}
return true;
}
public bool RunCommand(string args) =>
RunCommandAux(args, out _);
RunCommandAux(args, null, out _);
public bool RunCommand(string args, out IList<string> output) =>
RunCommandAux(args, out output);
RunCommandAux(args, null, out output);
public bool RunCommand(string args, string? workingDirectory, out IList<string> output) =>
RunCommandAux(args, workingDirectory, out output);
}
}