Skip to content

Invoke-ScriptAnalyzer do not load the required modules declared in a module manifest #599

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

Closed
LaurentDardenne opened this issue Aug 16, 2016 · 5 comments
Assignees
Milestone

Comments

@LaurentDardenne
Copy link

The module manifest does not seem loaded :
bug-requiredmodule

@kapilmb
Copy link

kapilmb commented Aug 17, 2016

@LaurentDardenne Can you please elaborate on this issue a bit more. If you can provide me the steps and files to reproduce this error that would be very helpful as well. Thanks!

@LaurentDardenne
Copy link
Author

Here is an archive containing both modules.
See Readme.txt

Issue-With-RequiredModule.zip

@kapilmb
Copy link

kapilmb commented Aug 18, 2016

@LaurentDardenne Thanks for the information.

Looks like even though Log4Posh is specified in the RequiredModules field in the manifest of ParameterSetRules module, it is not being imported when Invoke-ScriptAnalyzer imports ParameterSetRules module for running the custom rule.

PS> gc -Path C:\temp\test.txt
TEST RequiredModule
DebugLogger -is null : True
Microsoft.PowerShell.Management,Microsoft.PowerShell.Utility,ParameterSetRules

Importing ParameterSetRules through command line though imports the RequiredModules

PS> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     1.1        PSReadline                          {Get-PSReadlineKeyHandler, Get-PSReadlineOption, Remove-PS...

PS> Import-Module .\ParameterSetRules -Verbose
VERBOSE: Loading module from path
'C:\temp\Issue-With-RequiredModule\ParameterSetRules\ParameterSetRules.psd1'.
VERBOSE: Loading module from path
'C:\temp\Issue-With-RequiredModule\ParameterSetRules\ParameterSetRules.psm1'.
VERBOSE: Importing function 'Measure-DetectingErrorsInDefaultParameterSetName'.

PS> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.2.0.0    Log4Posh                            {ConvertTo-Log4NetCoreLevel, Get-DefaultAppenderFileName, ...
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Script     0.0.0.1    ParameterSetRules                   Measure-DetectingErrorsInDefaultParameterSetName
Script     1.1        PSReadline                          {Get-PSReadlineKeyHandler, Get-PSReadlineOption, Remove-PS...
Script     1.6.0      PSScriptAnalyzer                    {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer}

This looks like some kind of bug.

@kapilmb kapilmb added this to the 1609 milestone Aug 18, 2016
@kapilmb kapilmb self-assigned this Aug 18, 2016
@kapilmb kapilmb modified the milestones: backlog, 1609 Oct 5, 2016
@LaurentDardenne
Copy link
Author

Same problem with the key 'RequiredAssemblies', the assemblies declared are not loaded :

RequiredAssemblies = @('System.Data.Entity.Design','System.Globalization')

Module code :

#$DebugLogger.PSDebug("$([System.AppDomain]::CurrentDomain.GetAssemblies().Location|out-string)") 
$script:PluralSrvc =[System.Data.Entity.Design.PluralizationServices.PluralizationService]::CreateService(([System.Globalization.CultureInfo]::GetCultureInfo("en-us")))
$DebugLogger.PSDebug("is `$script:PluralSrvc null: $($null -eq $script:PluralSrvc)") # -> True

image

WorkAround : use Add-Type inside the module code.

Add-Type -AssemblyName System.Data.Entity.Design,System.Globalization
$script:PluralSrvc =[System.Data.Entity.Design.PluralizationServices.PluralizationService]::CreateService(([System.Globalization.CultureInfo]::GetCultureInfo("en-us")))
$DebugLogger.PSDebug("is `$script:PluralSrvc null: $($null -eq $script:PluralSrvc)") # -> False

image

@LaurentDardenne
Copy link
Author

For a script module, the scriptblock assigned to $MyInvocation.MyCommand.ScriptBlock.Module.OnRemove is not called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants