★ wanayoo — archive 1999 https://github.com/PowerShell/PowerShell/pull/8218Nouvelle recherche | Portail wanayoo
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

Fix incorrect name check when autoloading required modules #8218

Merged

Conversation

@rjmholt
Copy link
Member

commented Nov 9, 2018 •

PR Summary

Fixes #8204.

The change I made in #7125 made the check too strict on the module we try to autoload it from requires and rejected it based on name.

This PR removes that name check but also sets up a fix for the more general problem that a ModuleSpecification can always specify a path rather than a name. I added some reusable logic to make this easier to service.

The main contribution is regression testing for RequiredModules, #requires -Modules and other cmdlets that can use ModuleSpecifications.

However, the general pervasive problem is that ModuleSpecifications contain raw user input and we don't do very much about handling paths that might be given as module names, namely:

  • Normalising directory separators
  • Resolving relative paths (which should be resolved differently depending on context)
  • Dealing with case-sensitivity
  • Handling trailing slashes
  • Matching on module directories accounting for the versioned directory structure
  • Working with special directory names like . and ..

The methods I've added deal with these and I've applied them in one place to help correct the immediate problem and make it more efficient.

But the fact remains that we have no way in the type system to identify when this resolution has already occurred; there are over 120 uses of the ModuleSpecification type in the code base, and we don't have a way to differentiate when they contain raw user input and when they can be trusted to have a canonical path. The way the module cmdlets are written currently, we redo a fair amount of this validation and normalisation logic, and it's probably somewhere we could find serious perf improvements.

PR Checklist

@bergmeister

This comment has been minimized.

Copy link
Contributor

commented Nov 9, 2018

Thanks for handling the issue. I'd consider a regression test for this important feature to be essential.

@rjmholt

This comment has been minimized.

Copy link
Member Author

commented Nov 9, 2018

Agreed that it's essential -- I'm pretty surprised we didn't have one already.

I'm halfway through writing some already.

@rjmholt rjmholt force-pushed the rjmholt:fix-required-module-load-failure branch to ae5c56b Nov 9, 2018

@bergmeister

This comment has been minimized.

Copy link
Contributor

commented Nov 9, 2018

Ok, feel free to add the test in a 2nd PR as I can understand that this is a time-pressing issue that can initially be verified with some manual testing (please include Windows as test platform though)

@rjmholt

This comment has been minimized.

Copy link
Member Author

commented Nov 9, 2018

We also seem to do required module autoloading in #requires pragmas:

ModuleCmdletBase.LoadRequiredModule(
context: context,
currentModule: null,
requiredModuleSpecification: requiredModule,
moduleManifestPath: null,
manifestProcessingFlags: ModuleCmdletBase.ManifestProcessingFlags.LoadElements | ModuleCmdletBase.ManifestProcessingFlags.WriteErrors,
error: out error);

If those are supposed to allow paths, I'll need to do the same path normalisation logic there.

@adityapatwardhan

This comment has been minimized.

Copy link
Member

commented Nov 9, 2018

@rjmholt 1 new test is failing on Linux and macOS. Please have a look.

@rjmholt rjmholt requested a review from Francisco-Gamino as a code owner Nov 9, 2018

@rjmholt

This comment has been minimized.

Copy link
Member Author

commented Nov 12, 2018

The failing AppVeyor test is passing on my machine. Looking into it.

Show resolved Hide resolved src/System.Management.Automation/engine/Modules/GetModuleCommand.cs Outdated
Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleSpecification.cs
Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Outdated
// Paths in the module name may fail here because
// they the wrong directory separator or are relative.
// Fix with the code below:
// FullyQualifiedName = FullyQualifiedName.Select(ms => ms.WithNormalizedName(Context, SessionState.Path.CurrentLocation.Path)).ToArray();

This comment has been minimized.

Copy link
@daxian-dbw

daxian-dbw Nov 13, 2018

Member

What's the concern about putting this fix in place? Regression? Perf degradation?

This comment has been minimized.

Copy link
@rjmholt

rjmholt Nov 13, 2018

Author Member

Just keeping the fix constrained in this PR. It shouldn't cause regressions or perf degradations, but to ensure there are no regressions (since this PR is a regression fix itself) I just left it as a comment.

This comment has been minimized.

Copy link
@daxian-dbw

daxian-dbw Nov 13, 2018

Member

Will you open issue to track all those todo comments? They can be easily forgotten :)

This comment has been minimized.

Copy link
@daxian-dbw

daxian-dbw Nov 13, 2018

Member

Did you open an issue to track those todo's?

This comment has been minimized.

Copy link
@rjmholt

rjmholt Nov 13, 2018

Author Member

Next on my list

Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs Outdated

rjmholt added some commits Nov 13, 2018

Show resolved Hide resolved src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs Outdated
// Paths in the module name may fail here because
// they the wrong directory separator or are relative.
// Fix with the code below:
// FullyQualifiedName = FullyQualifiedName.Select(ms => ms.WithNormalizedName(Context, SessionState.Path.CurrentLocation.Path)).ToArray();

This comment has been minimized.

Copy link
@daxian-dbw

daxian-dbw Nov 13, 2018

Member

Did you open an issue to track those todo's?

@daxian-dbw
Copy link
Member

left a comment •

Only pending action item left is to open an issue tracking all "TODO" comments.

@adityapatwardhan

This comment has been minimized.

Copy link
Member

commented Nov 13, 2018

Since the CodeFactor issues are for existing methods, re-ordering will cause larger diffs. We can ignore CodeFactor issues for this PR.

@adityapatwardhan adityapatwardhan merged commit aa76c68 into PowerShell:master Nov 13, 2018

5 of 6 checks passed

CodeFactor 3 issues found.
Details
PowerShell-CI-linux #PR-8218-20181113.09 succeeded
Details
PowerShell-CI-macos #PR-8218-20181113.09 succeeded
Details
PowerShell-CI-windows #PR-8218-20181113.09 succeeded
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
license/cla All CLA requirements met.
Details

@iSazonov iSazonov added the CL-Engine label Jan 17, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.