question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidCastException in GetUsageData when using nullable reference types

See original GitHub issue

https://github.com/rubberduck203/GitNStats/issues/27

I recently enabled nullable reference types for my application. Since then, calling my app with the --help flag has started failing with an unhandled exception.

$ dotnet run --project src/gitnstats/gitnstats.csproj -- --help
Unhandled exception. System.InvalidCastException: Unable to cast object of type 'System.Runtime.CompilerServices.NullableAttribute' to type 'CommandLine.Text.UsageAttribute'.
   at CommandLine.Core.ReflectionExtensions.<>c.<GetUsageData>b__2_3(<>f__AnonymousType1`2 <>h__TransparentIdentifier0)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at CommandLine.Core.ReflectionExtensions.GetUsageData(Type type)
   at CommandLine.Text.HelpText.GetUsageFromType(Type type)
   at CommandLine.Text.HelpText.RenderUsageTextAsLines[T](ParserResult`1 parserResult, Func`2 mapperFunc)+MoveNext()
   at CSharpx.EnumerableExtensions.ToMaybe[T](IEnumerable`1 source)
   at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Func`2 onError, Func`2 onExample, Boolean verbsIndex, Int32 maxDisplayWidth)
   at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Func`2 onError, Int32 maxDisplayWidth)
   at CommandLine.Text.HelpText.AutoBuild[T](ParserResult`1 parserResult, Int32 maxDisplayWidth)
   at CommandLine.Parser.<>c__DisplayClass17_0`1.<DisplayHelp>b__1(IEnumerable`1 _, TextWriter writer)
   at CSharpx.MaybeExtensions.Do[T1,T2](Maybe`1 maybe, Action`2 action)
   at CommandLine.Parser.<>c__DisplayClass17_0`1.<DisplayHelp>b__0(IEnumerable`1 errors)
   at CommandLine.ParserResultExtensions.WithNotParsed[T](ParserResult`1 result, Action`1 action)
   at CommandLine.Parser.DisplayHelp[T](ParserResult`1 parserResult, TextWriter helpWriter, Int32 maxDisplayWidth)
   at CommandLine.Parser.MakeParserResult[T](ParserResult`1 parserResult, ParserSettings settings)
   at CommandLine.Parser.ParseArguments[T](IEnumerable`1 args)
   at GitNStats.Program.Main(String[] args) in /Users/rubberduck/src/GitNStats/src/gitnstats/Program.cs:line 11

The invalid cast is occurring in this block of code.

https://github.com/commandlineparser/commandline/blob/d443a51aeb3a418425e970542b3b96e9da5f62e2/src/CommandLine/Core/ReflectionExtensions.cs#L39-L48

I haven’t figured out if it’s occurring on line 44 or 45 yet, but GetUsageData seems to be assuming that it will only find a single attribute.

I’ll do my best to update this with a minimal reproduction.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
rubberduck203commented, Mar 7, 2022

The last commit to this repo was several months before I submitted the bug report & PR in 2020.

1reaction
rubberduck203commented, Nov 21, 2020

I submitted PR #715 to fix this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Nullable reference types
Nullable reference types refers to a group of features enabled in a nullable aware context that minimize the likelihood that your code causes ......
Read more >
C# 8.0 nullable references: prepare today by not misusing ' ...
The cast will throw an InvalidCastException to let us know that the cast failed, and the exception's Message will contain the following text...
Read more >
c# 9 Nullable reference types [closed]
In c# 9 with the new feature of Nullable reference types i find myself putting "?" on every class properties types to turn...
Read more >
Nullable reference types and nullability in EF Core
Exercise caution when enabling nullable reference types on an existing project: reference type properties which were previously configured as ...
Read more >
Digging Into Nullable Reference Types in C#
As C# developers, we spend a lot of time worrying about whether an object is null (since anyone can pass a null for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found