Skip to content

Fix version parsing in dotnet-validate - #1261

Merged
clairernovotny merged 1 commit into
NuGetPackageExplorer:mainfrom
joelverhagen:jver-parseversion
Mar 7, 2021
Merged

clairernovotny merged 1 commit into
NuGetPackageExplorer:mainfrom
joelverhagen:jver-parseversion

Conversation

@joelverhagen

Copy link
Copy Markdown
Contributor

I noticed that using dotnet-validate did not use provide version as expected. It seemed to be ignored (problem 1):

image

Also, four-number versions were rejected (problem 2):
image

... which of course if wrong since NuGet is okay with this:
image
https://nugettoolsdev.azurewebsites.net/5.8.0/parse-version?version=2.0.2.1

To fix problem 1 and problem 2, I just provided a parseArgument parameter to the appropriate Option<T> overload.

I also noticed this interesting comment where null was not being passed by System.CommandLine when -v is not specified by the user (problem 3):

// null is getting passed in as 0.0.0 for some reason
if(version != null && version.Major == 0 && version.Minor == 0 && version.Patch == 0)
{
version = null;
}

I dug into the System.CommandlLine implementation and found that it was calling an arbitrary constructor on NuGetVersion instead of parsing the provided string.
https://github.com/dotnet/command-line-api/blob/d87107bd40bb629fbee3ae2444b3a04cf631191d/src/System.CommandLine/Binding/ModelBinder.cs#L166-L169
Essentially, it seems to be picking the longest constructor where it can make guesses about the constructor parameters.

From my debugging, it is picking this one:
https://github.com/NuGet/NuGet.Client/blob/63255047fe7052cc33b763356ff995d9166f719e/src/NuGet.Core/NuGet.Versioning/NuGetVersion.cs#L116-L119
... and passing in default values for all of those types.

This is why we were seeing the 0.0.0 version -- because 0 is the default for int and the rest of the string parameters receive null, resulting in a 0.0.0 version.

To fix problem 3 I set a null default value on RunRemoteCommand which make System.CommandLine skip the arbitrary constructor flow and just provide `null.

@clairernovotny
clairernovotny merged commit e497ec9 into NuGetPackageExplorer:main Mar 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants