WithExpectedExitCode ignored when the tool exit code is zero
- Dominant language
- C#
- Stars
- 4.2k
- Forks
- 778
- Avg merge
- 1h 15m
- Merged PRs (30d)
- 19
Description
### Prerequisites
- [X] I have written a descriptive issue title
- [X] I have searched [issues](https://github.com/cake-build/cake/issues) to ensure it has not already been reported
### Cake runner
Cake .NET Tool
### Cake version
2.3.0
### Operating system
Windows
### Operating system architecture
64-Bit
### CI Server
_No response_
### What are you seeing?
This bug is related to the [new feature](https://github.com/cake-build/cake/issues/3947) added in Cake 2.3.0.
Firstly, let's say I have an executable built with C# with the following signature `static async Task Main(string[] args)` (notice `Task` rather than `Task`). In other words, my executable does not return a numerical exit code and therefore the exit code is assumed to be zero. I haven't tested it, but I am assuming you could also reproduce this exact same problem with an executable with the following signature: `static async Task Main(string[] args)` coupled with this line in the body of the "Main": `return 0;`.
What's important in order to reproduce the problem I am describing is that the exit code of the executable must be zero. I fully understand that zero **typically** signifies that the tool completed successfully but that's not always the case.
Secondly, let's say that my Cake script contains the following:
```csharp
Command(
new CommandSettings
{
ToolName = appName,
ToolExecutableNames = new[] { appName, $"{appName}.exe" },
ToolPath = $"{publishDir}{appName}.exe",
}.WithExpectedExitCode(1234),
new ProcessArgumentBuilder()
.Append("nopause")
};
```
Notice that I specified "1234" as the expected exit code. In this scenario, my executable returns zero and I have specified that the expected exit code should be 1234. I expect an exception to be thrown to let me know that the exit code does not match the expected value but that's not what I observe. I observe that my Cake script completes successfully, and no exception is thrown.
I investigated this situation and I believe I discovered what's causing this problem in `Cake.Core/Tooling/Tool.cs`. Line 101 invokes the custom exit code handler and correctly identifies that zero is not the expected exit code and line 103 invokes `ProcessExitCode`. However, the logic in the `ProcessExitCode` method on line 115 throws an exception **IF AND ONLY IF** the exit code is different than zero which, in the scenario I presented is not the desired behavior.
https://github.com/cake-build/cake/blob/2e23e6f647e00e8ab0e8dc5a9ec2382f99659b45/src/Cake.Core/Tooling/Tool.cs#L101..L120
Again, let me repeat that I completely understand that a "zero" exit code typically signifies success, but I don't think Cake should hard code this logic which has the side effect of overriding the custom exit code handler as I have demonstrated in my scenario.
### What is expected?
I expect an exception to be thrown which would let me know that the exit code does not match the expected value.
### Steps to Reproduce
As I stated previously, there are two conditions that must be met to reproduce this problem:
1. The executable must return zero (either because `Task Main` returns zero or `Task Main` does not return a numerical exit code which consequently is assumed to be zero)
2. The CommandSettings when invoking `Command` must define a non-zero expected exit code
When these two conditions are met, I expect an exception but instead my Cake script completes successfully.
### Output log
_No response_
Contributor guide
Research direction
Start in src/Cake.Core/Tooling/Tool.cs around lines 101-120, where the custom exit code handler and ProcessExitCode are invoked. Reproduce the case with exit code 0 and a non-zero WithExpectedExitCode value, then verify that the expected exception is raised; an open linked pull request indicates that work is already underway.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- build-system, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100