diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs index 7bc792384154..c377c2c6deb7 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNet.cs @@ -38,11 +38,18 @@ private DotNet(ILogger logger, string? dotNetPath, TemporaryDirectory tempWorkin private void Info() { - var res = dotnetCliInvoker.RunCommand("--info", silent: false); - if (!res) + var exitCode = dotnetCliInvoker.RunCommandExitCode("--info", silent: false); + switch (exitCode) { - throw new Exception($"{dotnetCliInvoker.Exec} --info failed."); + case 0: + break; + case 143: + logger.LogWarning("Running 'dotnet --info' failed with exit code 143."); + break; + default: + throw new Exception($"{dotnetCliInvoker.Exec} --info failed with exit code {exitCode}."); } + } private string GetRestoreArgs(RestoreSettings restoreSettings) diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs index 45f69a1fdfcd..4c4e789973ca 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/DotNetCliInvoker.cs @@ -57,15 +57,21 @@ private ProcessStartInfo MakeDotnetStartInfo(string args, string? workingDirecto return startInfo; } - private bool RunCommandAux(string args, string? workingDirectory, out IList output, bool silent) + private int RunCommandExitCodeAux(string args, string? workingDirectory, out IList output, out string dirLog, bool silent) { - var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}"; + dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}"; var pi = MakeDotnetStartInfo(args, workingDirectory); var threadId = Environment.CurrentManagedThreadId; void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId); void onError(string s) => logger.LogError(s, threadId); logger.LogInfo($"Running '{Exec} {args}'{dirLog}"); var exitCode = pi.ReadOutput(out output, onOut, onError); + return exitCode; + } + + private bool RunCommandAux(string args, string? workingDirectory, out IList output, bool silent) + { + var exitCode = RunCommandExitCodeAux(args, workingDirectory, out output, out var dirLog, silent); if (exitCode != 0) { logger.LogError($"Command '{Exec} {args}'{dirLog} failed with exit code {exitCode}"); @@ -77,6 +83,9 @@ private bool RunCommandAux(string args, string? workingDirectory, out IList RunCommandAux(args, null, out _, silent); + public int RunCommandExitCode(string args, bool silent = true) => + RunCommandExitCodeAux(args, null, out _, out _, silent); + public bool RunCommand(string args, out IList output, bool silent = true) => RunCommandAux(args, null, out output, silent); diff --git a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs index 3a599afe96d7..61d0ea4260db 100644 --- a/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs +++ b/csharp/extractor/Semmle.Extraction.CSharp.DependencyFetching/IDotNetCliInvoker.cs @@ -30,6 +30,12 @@ internal interface IDotNetCliInvoker /// bool RunCommand(string args, bool silent = true); + /// + /// Execute `dotnet ` and return the exit code. + /// If `silent` is true the output of the command is logged as `debug` otherwise as `info`. + /// + int RunCommandExitCode(string args, bool silent = true); + /// /// Execute `dotnet ` and return true if the command succeeded, otherwise false. /// The output of the command is returned in `output`. diff --git a/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs b/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs index 904ad04ce82f..be6745dd0725 100644 --- a/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs +++ b/csharp/extractor/Semmle.Extraction.Tests/DotNet.cs @@ -12,6 +12,7 @@ internal class DotNetCliInvokerStub : IDotNetCliInvoker private string lastArgs = ""; public string WorkingDirectory { get; private set; } = ""; public bool Success { get; set; } = true; + public int ExitCode { get; set; } = 0; public DotNetCliInvokerStub(IList output) { @@ -26,6 +27,12 @@ public bool RunCommand(string args, bool silent) return Success; } + public int RunCommandExitCode(string args, bool silent) + { + lastArgs = args; + return ExitCode; + } + public bool RunCommand(string args, out IList output, bool silent) { lastArgs = args; @@ -83,7 +90,7 @@ public void TestDotnetInfo() public void TestDotnetInfoFailure() { // Setup - var dotnetCliInvoker = new DotNetCliInvokerStub(new List()) { Success = false }; + var dotnetCliInvoker = new DotNetCliInvokerStub(new List()) { ExitCode = 1 }; // Execute try @@ -94,12 +101,26 @@ public void TestDotnetInfoFailure() // Verify catch (Exception e) { - Assert.Equal("dotnet --info failed.", e.Message); + Assert.Equal("dotnet --info failed with exit code 1.", e.Message); return; } Assert.Fail("Expected exception"); } + [Fact] + public void TestDotnetInfoExitCode143() + { + // Setup + var dotnetCliInvoker = new DotNetCliInvokerStub(new List()) { ExitCode = 143 }; + + // Execute + _ = MakeDotnet(dotnetCliInvoker); + + // Verify + var lastArgs = dotnetCliInvoker.GetLastArgs(); + Assert.Equal("--info", lastArgs); + } + [Fact] public void TestDotnetRestoreProjectToDirectory1() {