Skip to content

Throw error message when MaxumimVersion is less than Version when using Import-Module cmdlet with -FullyQualifiedName parameter. - #7347

Closed
Sergey Vasin (sethvs) wants to merge 7 commits into
PowerShell:masterfrom
sethvs:importModule
Closed

Sergey Vasin (sethvs) wants to merge 7 commits into
PowerShell:masterfrom
sethvs:importModule

Conversation

@sethvs

@sethvs Sergey Vasin (sethvs) commented Jul 24, 2018

Copy link
Copy Markdown
Contributor

PR Summary

Fix #7337

Also fixing following error message to include 'MaximumVersion' in the list of 'valid members':

  <data name="InvalidModuleSpecificationMember" xml:space="preserve">
    <value>The hashtable describing a module contains one or more members that are not valid.  The valid members are ({0}). Remove the members that are not valid ({1}), then try again.</value>
  </data>

Concerning if the change is breaking, I would refer it to Unlikely Grey Area.

PR Checklist

…ng Import-Module cmdlet with -FullyQualifiedName parameter.
if (badKeys.Length != 0)
{
message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, RequiredVersion, GUID", badKeys);
message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, MaximumVersion, RequiredVersion, GUID", badKeys);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please add to PR Summary:

Also fixing following error message to include 'MaximumVersion' in the list of 'valid members':
  <data name="InvalidModuleSpecificationMember" xml:space="preserve">
    <value>The hashtable describing a module contains one or more members that are not valid.  The valid members are ({0}). Remove the members that are not valid ({1}), then try again.</value>
  </data>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ideally this should use $"{nameof(...)}" for the bad members

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nameof(badKeys) returns 'badKeys', but we need its actual value.
Or am I missing something?

@anmenaga

Copy link
Copy Markdown

Dongbo Wang (@daxian-dbw) Steve Lee (@SteveL-MSFT) please evaluate if this technically breaking (however unlikely) change has to go to PS Committee for review.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Review - Committee The PR/Issue needs a review from the PowerShell Committee Breaking-Change breaking change that may affect users labels Aug 2, 2018
@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee agrees this is a breaking change. For example using this in a pipeline would terminate the pipeline now whereas a $null being returned would not execute the rest of the pipeline. We agree that this should have been an error and would accept returning a non-terminating error and not a terminating one.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Aug 8, 2018
@sethvs

Sergey Vasin (sethvs) commented Aug 12, 2018

Copy link
Copy Markdown
Contributor Author

Can you guide me a little bit?
How can I use WriteError in there?

if (moduleSpecification.Version != null && moduleSpecification.MaximumVersion != null &&
moduleSpecification.Version > ModuleCmdletBase.GetMaximumVersion(moduleSpecification.MaximumVersion))
{
message = StringUtil.Format(Modules.ModuleSpecificationMemberIsLessThanOther, "MaximumVersion", "ModuleVersion");
return new ArgumentException(message);
}

Or is there another way to return a non-terminating error?

@SteveL-MSFT

Copy link
Copy Markdown
Member

Sergey Vasin (@sethvs) I took a look at the code. Since the validation is in the ModuleSpecification class that should continue to return an exception that gets thrown. You'll probably need to update GetModuleCommand.cs to handle this specific case.

@rjmholt Rob Holt (rjmholt) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Apologies, I should have reviewed this sooner. Looks like a good change! Just left a couple of minor comments.

Thanks for your contribution Sergey Vasin (@sethvs)!

if (badKeys.Length != 0)
{
message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, RequiredVersion, GUID", badKeys);
message = StringUtil.Format(Modules.InvalidModuleSpecificationMember, "ModuleName, ModuleVersion, MaximumVersion, RequiredVersion, GUID", badKeys);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ideally this should use $"{nameof(...)}" for the bad members

<data name="InvalidModuleSpecificationMember" xml:space="preserve">
<value>The hashtable describing a module contains one or more members that are not valid. The valid members are ({0}). Remove the members that are not valid ({1}), then try again.</value>
</data>
<data name="ModuleSpecificationMemberIsLessThanOther" xml:space="preserve">

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The error message should ideally be slightly more specific. Something like:

The hashtable describing a module cannot have its ModuleVersion ({0}) greater than its MaximumVersion ({1}).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

(Get-Module TestModule).Version | Should -BeIn "1.1"
}

It "should throw if 'MaximumVersion' is less than 'Version' when using -FullyQualifiedName parameter" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In addition to this test, we should add a more specific one using the [ModuleSpecification] constructor.

You should find those tests here (I know that's not an obvious place at all).

Note to self: once #7499 is merged, this test will be included in that.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let me know if you need any help on creating that test btw, I'm always happy to

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done.

@rjmholt

Copy link
Copy Markdown
Collaborator

Steve Lee (@SteveL-MSFT) is it possible for a parameter binding exception to be non-terminating? The error when called with Get-Module will occur when the hashtable is cast to a ModuleSpecification; execution will never reach Get-Module.

@rjmholt

Copy link
Copy Markdown
Collaborator

Also for reference, here is Import-Module's version check:

if (BaseMaximumVersion != null && BaseMinimumVersion != null && BaseMaximumVersion < BaseMinimumVersion)
{
string message = StringUtil.Format(Modules.MinimumVersionAndMaximumVersionInvalidRange, BaseMinimumVersion, BaseMaximumVersion);
throw new PSArgumentOutOfRangeException(message);
}

I think that's a terminating error, no?

@sethvs

Copy link
Copy Markdown
Contributor Author

Is this OK with VSTS: PowerShell-CI-macos?

@SteveL-MSFT

Copy link
Copy Markdown
Member

Sergey Vasin (@sethvs) the VSTS runs are currently not blocking for merging, we're still working on improving them. They currently run in parallel to allow us to validate it's going to work for us.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Review - Committee The PR/Issue needs a review from the PowerShell Committee and removed Committee-Reviewed PS-Committee has reviewed this and made a decision labels Aug 21, 2018
@SteveL-MSFT

Copy link
Copy Markdown
Member

In light of recent discussions about returning nothing vs an error. I'd like @PowerShell/powershell-committee to re-review this

@sethvs

Copy link
Copy Markdown
Contributor Author

Close and reopen to restart tests.

@SteveL-MSFT

Copy link
Copy Markdown
Member

@PowerShell/powershell-committee re-reviewed this. The conclusion is that well-formed filters should return nothing if the result set is nothing and not return an error. In this case, the filter is well-formed, although invalid. Since this is also a breaking change, our recommendation is to not take this change as it's inconsistent with the desired filtering behavior and introduces a breaking change.

@SteveL-MSFT Steve Lee (SteveL-MSFT) added Committee-Reviewed PS-Committee has reviewed this and made a decision and removed Review - Committee The PR/Issue needs a review from the PowerShell Committee labels Sep 5, 2018
@kilasuit Ryan Yates (kilasuit) added WG-Cmdlets general cmdlet issues and removed WG-Cmdlets-Core labels Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Breaking-Change breaking change that may affect users Committee-Reviewed PS-Committee has reviewed this and made a decision WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Get-Module accepts nonsensical combinations of ModuleVersion and MaximumVersion entries for -FullyQualifiedName

6 participants