Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issue #2392 #2402

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Fixing issue #2392 #2402

wants to merge 2 commits into from

Conversation

Keboo
Copy link
Member

@Keboo Keboo commented Apr 25, 2024

There were two problems addressed here.
First, the call to Diagram was causing the second error to be added to the ParseResult.Errors collection. The diagram call, was being invoked by the debugger as part of the ParseResult.ToString() method. Causing the number of errors to change when inspecting a parse result.

The second issue was the error message text. It was implying that the option expected a single value, when its arity allowed for more.

There were two problems addressed here.
First, the call to Diagram was causing the second error to be added to the ParseResult.Errors collection. The diagram call, was being invoked by the debugger as part of the ParseResult.ToString() method. Causing the number of errors to change when inspecting a parse result.

The second issue was the error message text. It was implying that the option expected a single value, when its arity allowed for more.
@@ -14,11 +14,23 @@ namespace System.CommandLine
internal static class LocalizationResources
{
/// <summary>
/// Interpolates values into a localized string similar to Command &apos;{0}&apos; expects a single argument but {1} were provided.
/// Interpolates values into a localized string similar to Option &apos;{0}&apos; expects a single argument but {1} were provided.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this used by both options and commands?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was to make the comment match the actual value that is getting returned. I believe the only usage is for options. I suspect this comment was wrong from a copy and paste error when we initially did the localization resources.

@@ -125,7 +125,7 @@ private void ValidateOptions(bool completeValidation)
// When_there_is_an_arity_error_then_further_errors_are_not_reported
if (!ArgumentArity.Validate(argumentResult, out var error))
{
optionResult.AddError(error.ErrorMessage!);
argumentResult.AddError(error.ErrorMessage!);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not understand this change. If this is on options, I thought we got the errors from the option. Do we get them from the option's argument?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was one of the key fixes, that was causing the diagram command to insert a duplicate error.

The process looks like this:

  1. During the Parse, this line was invoked and the error was added to the OptionResult.
  2. The AddError method adds the error into the SymbolResultTree, and keys it based on the symbol. However, ArgumentResult.AddError method has a special case for options, where it will use the OptionResult if that is the parent. So in either case the error is added to the SymbolResultTree with the OptionResult as the key. However, in the case of the ArgumentResult.AddError it also sets the _conversionResult. So the change here still sets the error on the OptionResult, but it also causes the _conversionResult field to be set.
  3. When calling the Diagram (and likely this bug exists with other code paths as well), it will call ArgumentResult.GetArgumentConversionResult. That method will call ValidateAndConvert if the _conversionResult is null. Calling ValidateAndConvert will add an error to the result. This was the result of the second error being added.
  4. Because Diagram was being invoked from the ParseResult.ToString() it was being invoked whenever the debugger inspected a ParseResult. This was the reason for the un-usual behavior that you saw.

@KathleenDollard KathleenDollard added the Powderhouse Work to isolate parser and features label Apr 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Powderhouse Work to isolate parser and features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants