PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Every time we analyze a '.ps1' file with custom rules, we need to import module once again.

Open MoChilia opened this issue 3 years ago • 4 comments

Hi! We are using PSSA to analyze syntax errors in '.ps1' files with custom rules. It seems that a new PowerShell session is created when we invoke-ScriptAnalyzer with custom rules, then all the modules imported before are lost. So, every time we analyze a '.ps1' file with custom rules, we need to import the needed module once again. However, it is time costly for us to import modules again and again. Since we work in the continuous integration environment, we need to import the modules built by CI but not the modules installed locally. 'Import-module' ahead of the '.psm1' file of custom rules seems indispensable. Can you give some suggestions to solve the problem of import modules too many times when analyzing dozens of '.ps1' files? Thanks a lot!

MoChilia avatar Jun 07 '22 03:06 MoChilia

Thanks for the issue @MoChilia! So PSSA is designed in this way because it is both dynamic and static script analyzer. PSSA mostly checks for style or usage issues which are not associated with syntax.

For clarification are you looking specifically at syntax errors? or do you have other custom rules? If you are only doing syntax checking then PSSA might not be the best solution as it has a lot of overhead.

Also can you try running Invoke-ScriptAnalyzer *.ps1 on an entire directory and see if that resolves your issues.

StevenBucher98 avatar Jun 07 '22 22:06 StevenBucher98

Hi, @StevenBucher98! Thanks for your suggestion! We tried running Invoke-ScriptAnalyzer *.ps1 on an entire directory, but it still needed to import-module for every .ps1 file. So we tried to solve the problem by merging all the commands into one .ps1 file and using functions to split their action scopes. It seems to work. However, we meet another problem: the conflict caused by multi-threading of PSSA. We imported modules in the begin block of the function in the custom rule, but an error called Collection was modified; enumeration operation may not execute occurred. Do you have any idea to solve the error?

By the way, we are confused about the difference between "usage issues" and "syntax issues". We want to check errors like "invaild cmdlet", "Invalid parameter name", "Unassigned parameter", etc. We think PSSA is a good tool to check them. What's your opinion on this?

Thanks again.

MoChilia avatar Jun 09 '22 05:06 MoChilia

Interesting. PSSA should just open a new runspace and not remove an already imported module. This sounds like a bug to me, I would like to spend some time to see whether I can repro. Around your Collection was modified; enumeration operation may not execute error, we've seen this a few times and usually related around concurrency issues where PowerShell is not thread safe. Nevertheless please provide us the full error using $error | select *, it might be useful to see from where the error is thrown

bergmeister avatar Jun 15 '22 22:06 bergmeister

Hi, @bergmeister! Following is the full error using $error | select *.

PS /home/shiying/azure-powershell> $error | select *

PSMessageDetails      : 
Exception             : System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
                           at System.Collections.Generic.List`1.Enumerator.MoveNext()
                           at System.Management.Automation.Runspaces.InitialSessionState.UpdateFormats(ExecutionContext context, Boolean update)
                           at System.Management.Automation.Runspaces.InitialSessionState.Bind_UpdateFormats(ExecutionContext context, Boolean updateOnly)
                           at System.Management.Automation.Runspaces.InitialSessionState.<>c__DisplayClass130_0.<Bind>b__1()
                           at System.Management.Automation.Runspaces.InitialSessionState.Bind(ExecutionContext context, Boolean updateOnly, PSModuleInfo module, Boolean noClobber, Bo
                        olean local, Boolean setLocation)
                           at Microsoft.PowerShell.Commands.ModuleCmdletBase.LoadModuleManifest(String moduleManifestPath, ExternalScriptInfo manifestScriptInfo, Hashtable data, Hash
                        table localizedData, ManifestProcessingFlags manifestProcessingFlags, Version minimumVersion, Version maximumVersion, Version requiredVersion, Nullable`1 requ
                        iredModuleGuid, ImportModuleOptions& options, Boolean& containedErrors)
TargetObject          : 
CategoryInfo          : InvalidOperation: (:) [Invoke-ScriptAnalyzer], InvalidOperationException
FullyQualifiedErrorId : FormatXmlUpdateException,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at Measure-ParameterNameAndValue<Begin>, /home/shiying/azure-powershell/tools/StaticAnalysis/ExampleAnalyzer/AnalyzeRules/ParameterNameAndValue.psm1: line 60
PipelineIterationInfo : {0, 1}

MoChilia avatar Jun 16 '22 01:06 MoChilia