commandlineparser / commandlineparser/commandline

InvalidCastException in GetUsageData when using nullable reference types

未关闭
#714 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
C#
星标
4.8k
派生
478
PR 合并指标
30 天内没有已合并 PR

描述

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.

```console
$ 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.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.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.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.

贡献指南

这个仓库没有索引到贡献指南

调研方向

从 src/CommandLine/Core/ReflectionExtensions.cs 中 GetUsageData 附近开始,参考链接的第 39-48 行以及会到达 GitNStats.Program.Main 的 --help 调用。复现 nullable reference types 失败,并确认帮助渲染能够在不发生 InvalidCastException 的情况下完成;payload 未指定回归测试。

由索引模型根据 Issue 内容生成。

评估

技术栈
csharp
领域
cli
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
38/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。