Skip to content

Refactor module version/GUID comparison logic - #7125

Merged
Aditya Patwardhan (adityapatwardhan) merged 6 commits into
PowerShell:masterfrom
rjmholt:refactor-module-version-check
Nov 1, 2018
Merged

Aditya Patwardhan (adityapatwardhan) merged 6 commits into
PowerShell:masterfrom
rjmholt:refactor-module-version-check

Conversation

@rjmholt

@rjmholt Rob Holt (rjmholt) commented Jun 21, 2018

Copy link
Copy Markdown
Collaborator

PR Summary

TLDR: Factors out module GUID/version checking logic to all use the same code path.

This PR routes all of the module GUID/version checking through ModuleIntrinsics.IsModuleMatchingSpec() (or a sub-method of it), so that all the version checking is consistent and DRY.

Previously the code had a number of 6-line if-conditions and cascading if-elses that all check module versioning in a slightly different way. I've tried to identify the common logic and unify it all.

Breaking Change Summary

Fix #7496
Previous to this PR, having a module loaded with one version, meant that importing a different version of that module would give you the (wrong) loaded version. This change fixes the version check so that the loaded module is only used when it is the correct version. See #7496.

Tests

Tests for this PR were already added in #7499. This PR just removes the -Pending flags from the tests around the breaking change (on the basis that the previous behaviour was a bug).

Fixes #7496.

PR Checklist


This change is Reviewable

@rjmholt Rob Holt (rjmholt) changed the title Refactor module version/GUID comparison logic WIP: Refactor module version/GUID comparison logic Jun 21, 2018
@rjmholt Rob Holt (rjmholt) added Area-Cmdlets-Core Issue-Code Cleanup the issue is for cleaning up the code with no impact on functionality labels Jun 21, 2018
@rjmholt Rob Holt (rjmholt) changed the title WIP: Refactor module version/GUID comparison logic Refactor module version/GUID comparison logic Jun 21, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Also, if our testing coverage on this should be improved, I'm happy to add tests to this PR

To make a quality check of this change we are forced to do a lot of tests locally. So it makes sense to start by reviewing and extending the tests in the repo. I see that we don't actually have the tests for this code. I think we could add the new tests in new separate PR and then continue the PR. The Import-Module.Tests.ps1 file is already large so we could create new Import-Module-Versions.Tests.ps1.

@rjmholt

Copy link
Copy Markdown
Collaborator Author

Ah so there is an Import-Module.Tests.ps1? It’s not under /test/powershell/engine/Module like I expected. Where is it?

Yes agreed we should bulk up the tests in another PR to merge before this one. I’ve been thinking about doing that but now I know there’s a test file I will see what we already have...

@iSazonov

Copy link
Copy Markdown
Collaborator

See test\powershell\Modules\Microsoft.PowerShell.Core\ subdirectory.

@anmenaga

Copy link
Copy Markdown

Rob Holt (@rjmholt) It would be good to see a green [Feature] test pass result. Can you please run it again?

@anmenaga

Copy link
Copy Markdown

Dongbo Wang (@daxian-dbw) Bruce Payette (@BrucePay) We need to get some traction on this; this PR is sitting without a review for more than a month now...

@rjmholt

Rob Holt (rjmholt) commented Jul 30, 2018

Copy link
Copy Markdown
Collaborator Author

I need to write tests for this I would say. Will mark as WIP.

@rjmholt Rob Holt (rjmholt) changed the title Refactor module version/GUID comparison logic WIP: Refactor module version/GUID comparison logic Jul 30, 2018
@rjmholt Rob Holt (rjmholt) added the Breaking-Change breaking change that may affect users label Aug 10, 2018
@stale

stale Bot commented Sep 9, 2018

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed if no further activity occurs within 10 days.
Thank you for your contributions.
Community members are welcome to grab these works.

@stale stale Bot added the Stale label Sep 9, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Rob Holt (@rjmholt) Please resolve the merge conflict.

@stale stale Bot removed the Stale label Sep 10, 2018
@rjmholt
Rob Holt (rjmholt) force-pushed the refactor-module-version-check branch from 3467201 to e56b1b4 Compare September 28, 2018 22:28
@rjmholt Rob Holt (rjmholt) changed the title WIP: Refactor module version/GUID comparison logic Refactor module version/GUID comparison logic Sep 28, 2018
@rjmholt

Copy link
Copy Markdown
Collaborator Author

I have now rebased this to include the 774 tests I wrote

@rjmholt Rob Holt (rjmholt) removed the Breaking-Change breaking change that may affect users label Sep 28, 2018
@rjmholt

Rob Holt (rjmholt) commented Sep 29, 2018

Copy link
Copy Markdown
Collaborator Author

I've removed the breaking change tag since I think another PR has already made the relevant change. This PR changes no tests and passes all existing.

This is wrong. That test PR skips those tests. This PR should enable them.

EDIT: Tests are now enabled, and pass.

@rjmholt Rob Holt (rjmholt) added the Breaking-Change breaking change that may affect users label Oct 10, 2018
@daxian-dbw

Copy link
Copy Markdown
Member

Rob Holt (@rjmholt) Can you please summarize what the breaking change is in the PR description?

@rjmholt

Copy link
Copy Markdown
Collaborator Author

Ilya (@iSazonov) Tests were added in #7499. I added the tests for the new behaviour then and have removed the -Pending flag in this PR.

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for revisit some of the optional parameters.

Comment thread src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Comment thread src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
/// </summary>
/// <param name="alreadyLoadedModule">The already loaded module that matched the name of the module to load.</param>
/// <returns>True if the pre-loaded module matches all GUID and version constraints provided, false otherwise.</returns>
internal bool IsModuleAlreadyLoaded(PSModuleInfo alreadyLoadedModule)

@iSazonov Ilya (iSazonov) Oct 12, 2018

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.

Seems the name should be IsAlreadyLoadedModuleMatchingConstraints. The module description should be corrected too.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This method has been in PowerShell with this name (and with this behaviour) since at least Win8. I would be happy to change it, so long as it's not going to cause any issues.

Comment thread src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs
@anmenaga

Copy link
Copy Markdown

Rob Holt (@rjmholt) Please address remaining couple of minor issues that Ilya pointed out and this will be ready.

@anmenaga

Copy link
Copy Markdown

Aditya Patwardhan (@adityapatwardhan) this looks ready.

@iSazonov

Copy link
Copy Markdown
Collaborator

Rob Holt (@rjmholt) Please address two comments.

@adityapatwardhan

Copy link
Copy Markdown
Member

Rob Holt (@rjmholt) Please address comments from Ilya (@iSazonov). After that, is seems ready to merge.

@rjmholt

Copy link
Copy Markdown
Collaborator Author

Aditya Patwardhan (@adityapatwardhan), Ilya (@iSazonov) Feedback addressed -- let me know if any further changes are required.

@adityapatwardhan

Aditya Patwardhan (adityapatwardhan) commented Oct 30, 2018

Copy link
Copy Markdown
Member

Rob Holt (@rjmholt) please have a look at failing CI.

@adityapatwardhan

Copy link
Copy Markdown
Member

Steve Lee (@SteveL-MSFT) The newly added test is failing. It is marked as Feature and Feature tests were not run when the PR got merged.

@SteveL-MSFT

Copy link
Copy Markdown
Member

Looking

@SteveL-MSFT

Steve Lee (SteveL-MSFT) commented Oct 31, 2018

Copy link
Copy Markdown
Member

Test fix #8152

@iSazonov

Copy link
Copy Markdown
Collaborator

Rob Holt (@rjmholt) Please look CodeFactor issues and fix if they in your code.

@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) merged commit 5d06fba into PowerShell:master Nov 1, 2018
@iSazonov

Copy link
Copy Markdown
Collaborator

Aditya Patwardhan (@adityapatwardhan) Travis Plunk (@TravisEz13) Could you please reword the merged commit? [Breaking change] was skipped in description.

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 Issue-Code Cleanup the issue is for cleaning up the code with no impact on functionality WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Import-Module on path ignores version constraints

8 participants