-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fixed ScriptsToProcess when -Version param is used in Import-Module #3897
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2768,38 +2768,58 @@ internal PSModuleInfo LoadModuleManifest( | |
| } | ||
|
|
||
| if (scriptsToProcess != null) | ||
| { | ||
| foreach (string scriptFile in scriptsToProcess) | ||
| { | ||
| bool found = false; | ||
| PSModuleInfo module = LoadModule(scriptFile, | ||
| moduleBase, | ||
| string.Empty, // prefix (-Prefix shouldn't be applied to dot sourced scripts) | ||
| null, | ||
| ref options, | ||
| manifestProcessingFlags, | ||
| out found); | ||
|
|
||
| // If we're in analysis, add the detected exports to this module's | ||
| // exports | ||
| if (found && (ss == null)) | ||
| { | ||
| Version savedBaseMinimumVersion = BaseMinimumVersion; | ||
| Version savedBaseMaximumVersion = BaseMaximumVersion; | ||
| Version savedBaseRequiredVersion = BaseRequiredVersion; | ||
| Guid? savedBaseGuid = BaseGuid; | ||
|
|
||
| try | ||
| { | ||
| BaseMinimumVersion = null; | ||
| BaseMaximumVersion = null; | ||
| BaseRequiredVersion = null; | ||
| BaseGuid = null; | ||
|
|
||
| foreach (string scriptFile in scriptsToProcess) | ||
| { | ||
| foreach (string detectedCmdlet in module.ExportedCmdlets.Keys) | ||
| { | ||
| manifestInfo.AddDetectedCmdletExport(detectedCmdlet); | ||
| } | ||
| bool found = false; | ||
| PSModuleInfo module = LoadModule(scriptFile, | ||
| moduleBase, | ||
| string.Empty, // prefix (-Prefix shouldn't be applied to dot sourced scripts) | ||
| null, | ||
| ref options, | ||
| manifestProcessingFlags, | ||
| out found); | ||
|
|
||
| foreach (string detectedFunction in module.ExportedFunctions.Keys) | ||
| // If we're in analysis, add the detected exports to this module's | ||
| // exports | ||
| if (found && (ss == null)) | ||
| { | ||
| manifestInfo.AddDetectedFunctionExport(detectedFunction); | ||
| } | ||
| foreach (string detectedCmdlet in module.ExportedCmdlets.Keys) | ||
| { | ||
| manifestInfo.AddDetectedCmdletExport(detectedCmdlet); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AddDetected* methods from PSModuleInfo in most cases seem to be called multiple times using a for loop. Will it make sense to add an overload which accepts IEnumerable?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe. But as previous comment - this would be refactoring of unrelated code - out of scope of this bug fix. |
||
| } | ||
|
|
||
| foreach (string detectedAlias in module.ExportedAliases.Keys) | ||
| { | ||
| manifestInfo.AddDetectedAliasExport(detectedAlias, | ||
| module.ExportedAliases[detectedAlias].Definition); | ||
| foreach (string detectedFunction in module.ExportedFunctions.Keys) | ||
| { | ||
| manifestInfo.AddDetectedFunctionExport(detectedFunction); | ||
| } | ||
|
|
||
| foreach (string detectedAlias in module.ExportedAliases.Keys) | ||
| { | ||
| manifestInfo.AddDetectedAliasExport(detectedAlias, | ||
| module.ExportedAliases[detectedAlias].Definition); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| BaseMinimumVersion = savedBaseMinimumVersion; | ||
| BaseMaximumVersion = savedBaseMaximumVersion; | ||
| BaseRequiredVersion = savedBaseRequiredVersion; | ||
| BaseGuid = savedBaseGuid; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,3 +22,44 @@ | |
| (Get-Module -Name $moduleName).Name | Should Be $moduleName | ||
| } | ||
| } | ||
|
|
||
| Describe "Import-Module with ScriptsToProcess" -Tags "CI" { | ||
|
|
||
| BeforeAll { | ||
| $moduleRootPath = Join-Path $TestDrive 'TestModules' | ||
| New-Item $moduleRootPath -ItemType Directory -Force | Out-Null | ||
| Push-Location $moduleRootPath | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to push location? Can you use full paths for all cmdlets like New-Item, New-ModuleManifest etc?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose push location specifically because I didn't want to deal with full paths. :) |
||
|
|
||
| "1 | Out-File out.txt -Append -NoNewline" | Out-File script1.ps1 | ||
| "2 | Out-File out.txt -Append -NoNewline" | Out-File script2.ps1 | ||
| New-ModuleManifest module1.psd1 -ScriptsToProcess script1.ps1 | ||
| New-ModuleManifest module2.psd1 -ScriptsToProcess script2.ps1 -NestedModules module1.psd1 | ||
| } | ||
|
|
||
| AfterAll { | ||
| Pop-Location | ||
| } | ||
|
|
||
| BeforeEach { | ||
| New-Item out.txt -ItemType File -Force | Out-Null | ||
| } | ||
|
|
||
| AfterEach { | ||
| $m = @('module1','module2','script1','script2') | ||
| remove-module $m -Force -ErrorAction SilentlyContinue | ||
| Remove-Item out.txt -Force -ErrorAction SilentlyContinue | ||
| } | ||
|
|
||
| $testCases = @( | ||
| @{ TestNameSuffix = 'for top-level module'; ipmoParms = @{'Name'='.\module1.psd1'}; Expected = '1' } | ||
| @{ TestNameSuffix = 'for top-level and nested module'; ipmoParms = @{'Name'='.\module2.psd1'}; Expected = '21' } | ||
| @{ TestNameSuffix = 'for top-level module when -Version is specified'; ipmoParms = @{'Name'='.\module1.psd1'; 'Version'='1.0'}; Expected = '1' } | ||
| @{ TestNameSuffix = 'for top-level and nested module when -Version is specified'; ipmoParms = @{'Name'='.\module2.psd1'; 'Version'='1.0'}; Expected = '21' } | ||
| ) | ||
|
|
||
| It "Verify ScriptsToProcess are executed <TestNameSuffix>" -TestCases $testCases { | ||
| param($TestNameSuffix,$ipmoParms,$Expected) | ||
| Import-Module @ipmoParms | ||
| Get-Content out.txt | Should Be $Expected | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use named parameters. Example: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is actually existing code; I didn't change it; looks like Github's diff'er has room for improvement :) - see picture below.
Anyway, there are multiple LoadModule calls like this one throughout the file, so if refactoring is to be done - it has to be done to all of them - and that is out of scope of this bugfix.