Skip to content

Commit

Permalink
Improve error handling when dotnet.exe is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillOsenkov committed Aug 8, 2024
1 parent 42cb688 commit ccc5142
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 29 deletions.
12 changes: 11 additions & 1 deletion ILRepack/ProcessRunner.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;

Expand Down Expand Up @@ -29,7 +31,15 @@ public static Task<ProcessRunResult> RunAsync(string executableName, string argu

public static async Task<ProcessRunResult> RunAsync(ProcessStartInfo processStartInfo)
{
Process process = Process.Start(processStartInfo);
Process process;
try
{
process = Process.Start(processStartInfo);
}
catch (Win32Exception ex)
{
throw new FileNotFoundException($"{ex.Message}: {processStartInfo.FileName}");
}

var outputTask = process.StandardOutput.ReadToEndAsync();
var errorTask = process.StandardError.ReadToEndAsync();
Expand Down
68 changes: 40 additions & 28 deletions ILRepack/RepackAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,46 +200,58 @@ public void InitializeDotnetRuntimeDirectories()

runtimeDirectoriesInitialized = true;

var process = ProcessRunner.Run("dotnet", "--list-runtimes");
if (process.ExitCode != 0)
try
{
throw new Exception(".NET Core SDK list query failed with code " + process.ExitCode);
}
var process = ProcessRunner.Run("dotnet", "--list-runtimes");
if (process == null || process.ExitCode != 0)
{
throw new Exception(".NET Core SDK list query failed with code " + process.ExitCode);
}

var allRuntimes = new List<string>();
var allRuntimes = new List<string>();

var reader = new StringReader(process.Output);
var reader = new StringReader(process.Output);

string line;
while ((line = reader.ReadLine()) != null)
{
var pathStart = line.LastIndexOf('[') + 1;
if (pathStart == 0)
string line;
while ((line = reader.ReadLine()) != null)
{
continue;
}
var pathStart = line.LastIndexOf('[') + 1;
if (pathStart == 0)
{
continue;
}

var pathEnd = line.LastIndexOf(']');
if (pathEnd == -1 || pathEnd <= pathStart)
{
continue;
var pathEnd = line.LastIndexOf(']');
if (pathEnd == -1 || pathEnd <= pathStart)
{
continue;
}

var path = line.Substring(pathStart, pathEnd - pathStart);
var runtimeInfo = line.Substring(0, pathStart - 1);
var parts = runtimeInfo.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
{
continue;
}

var fullPath = Path.Combine(path, parts[1]);
allRuntimes.Add(fullPath);
}

var path = line.Substring(pathStart, pathEnd - pathStart);
var runtimeInfo = line.Substring(0, pathStart - 1);
var parts = runtimeInfo.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length < 2)
allRuntimes.Reverse();

ReadRuntimes(allRuntimes);
}
catch (Exception ex)
{
if (ex is AggregateException aggregate)
{
continue;
ex = aggregate.InnerException;
}

var fullPath = Path.Combine(path, parts[1]);
allRuntimes.Add(fullPath);
Application.Error($"Error when calling 'dotnet --list-runtimes': {ex.Message}");
}

allRuntimes.Reverse();

ReadRuntimes(allRuntimes);
}

private void ReadRuntimes(IEnumerable<string> allRuntimes)
Expand Down

0 comments on commit ccc5142

Please sign in to comment.