InvalidCastException in GetUsageData when using nullable reference types #714
Comments
rubberduck203
added a commit
to rubberduck203/commandline
that referenced
this issue
Nov 21, 2020
|
Minimal reproduction can be found here: rubberduck203@90ccab8 In the test project file, enable nullable reference types <nullable>enable</nullable>The options class must have a nullable property, then we need to add custom usage examples in order to trigger the bug. public class ReflectionExtensionsTests
{
[Fact]
public void GetUsageDataDoesNotExplodeWhenUsedWithNullableReferenceTypesUsingTheUsageAttribute()
{
typeof(NullableReferenceTypeOptions).GetUsageData();
}
}
public class NullableReferenceTypeOptions
{
[Option(HelpText = "Define a string value here.")]
public string? StringValue { get; set; }
[Usage]
public static IEnumerable<Example> Examples
{
get
{
yield return new Example("Run on current directory", new NullableReferenceTypeOptions());
}
}
} |
|
I submitted PR #715 to fix this. |
rubberduck203
added a commit
to rubberduck203/GitNStats
that referenced
this issue
Nov 23, 2020
There's a bug in the commandline parser library that triggers when an option class has a custom usage function and that function has more than one attribute. When a class has nullable reference type properties, it's members are all tagged with the System.Runtime.CompilerServices.NullableAttribute attribute. Disabling NRTs in just the Options.cs file fixes the bug while still allowing us to use NRTs elsewhere. A bug has been submitted upstream commandlineparser/commandline#714 As well as a pull request that fixes the issue commandlineparser/commandline#715 The compiler directive can be removed once it's accepted and released.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rubberduck203/GitNStats#27
I recently enabled nullable reference types for my application.
Since then, calling my app with the
--helpflag has started failing with an unhandled exception.The invalid cast is occurring in this block of code.
commandline/src/CommandLine/Core/ReflectionExtensions.cs
Lines 39 to 48 in d443a51
I haven't figured out if it's occurring on line 44 or 45 yet, but
GetUsageDataseems to be assuming that it will only find a single attribute.I'll do my best to update this with a minimal reproduction.
The text was updated successfully, but these errors were encountered: