Fix incorrect name check when autoloading required modules#8218
Fix incorrect name check when autoloading required modules#8218adityapatwardhan merged 41 commits intoPowerShell:masterfrom
Conversation
|
Thanks for handling the issue. I'd consider a regression test for this important feature to be essential. |
|
Agreed that it's essential -- I'm pretty surprised we didn't have one already. I'm halfway through writing some already. |
3205e0d to
ae5c56b
Compare
|
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) |
|
We also seem to do required module autoloading in PowerShell/src/System.Management.Automation/engine/CommandDiscovery.cs Lines 307 to 313 in 4118fd2 If those are supposed to allow paths, I'll need to do the same path normalisation logic there. |
|
@rjmholt 1 new test is failing on Linux and macOS. Please have a look. |
|
The failing AppVeyor test is passing on my machine. Looking into it. |
src/System.Management.Automation/engine/Modules/GetModuleCommand.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Outdated
Show resolved
Hide resolved
| // 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(); |
There was a problem hiding this comment.
What's the concern about putting this fix in place? Regression? Perf degradation?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Will you open issue to track all those todo comments? They can be easily forgotten :)
There was a problem hiding this comment.
Did you open an issue to track those todo's?
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Outdated
Show resolved
Hide resolved
src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs
Outdated
Show resolved
Hide resolved
| // 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(); |
There was a problem hiding this comment.
Did you open an issue to track those todo's?
|
Since the CodeFactor issues are for existing methods, re-ordering will cause larger diffs. We can ignore CodeFactor issues for this PR. |
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
ModuleSpecificationcan 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 -Modulesand other cmdlets that can useModuleSpecifications.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:.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
ModuleSpecificationtype 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
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.[feature]if the change is significant or affects feature tests