Skip to content

System.CommandLine silently swallows OperationCanceledException and TaskCanceledException #2808

@lbussell

Description

@lbussell

Repro

#:package System.CommandLine@2.0.8
using System.CommandLine;

// This stack trace won't print to the console
Func<ParseResult, int> throwException = parseResult =>
{
    throw new OperationCanceledException();
    // Also doesn't work:
    // throw new TaskCanceledException();
};

// This works as expected.
// Func<ParseResult, int> throwException = parseResult => { throw new Exception(); };

var root = new RootCommand();
root.SetAction(throwException);
var parseResult = root.Parse(args);
return parseResult.Invoke();
// still returns exit code 1

dotnet run Repro.cs

Expected behavior

I see the stack trace for the OperationCanceledException.

Actual behavior

There is no output.

Additional details

As a developer trying to debug why my application crashed with no output, this behavior is frustrating.
If I wanted to exit gracefully (e.g. if the user pressed Ctrl-C), then I would expect to have to catch an exception at some point, instead of System.CommandLine swallowing it for me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions