-
Notifications
You must be signed in to change notification settings - Fork 389
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 parse errors caused by missing dsc resouces #524
Conversation
Whenever Invoke-ScriptAnalyzer (isa) is run on a script having the dynamic keyword "Import-DSCResource -ModuleName <somemodule>", if <somemodule> is not present in any of the PSModulePath, isa gives parse error. This error is caused by the powershell parser not being able to find the symbol for <somemodule>.
One solution to solve this is to download the missing modules to a temporary path and then add that temporary path to PSModulePath when isa is run and remove the temp path when isa is finished running. But at this iteration this approach doesn't seem to work and needs further investigation. The solution, that we pursue, is to download the missing module to a temp path. This temp path and its data is persistent in the sense that the temp path information is stored in $LOCALAPPDATA/PSScriptAnalyzer (pssaappdata). Whenver isa in invoked, it checks the temp path pointed in pssaappdata, checks if the modules are present in the temp directory and if so, copies them to the user psmodule path, which is typcially $HOME/Documents/WindowsPowerShell/Modules. And, when isa is finished running we remove those copied modues from the user psmodule path.
Use powershell copy-item to copy directories instead of writing a routine to copy directories recursively.
Adds a temporary path to the PSModulePath that contains the downloaded modules. This prevents the powershell parser from throwing parse errors when it cannot find a symbol that refers to an external module.
Adds a switch named "ResolveDSCResourceDependency" to Invoke-ScriptAnalyzer (isa). If the switch is given, then isa with try to resolve the dependency, whenever parse error is thrown due to modulenotfound exception, through the ModuleDependencyHandler class. If the switch is not provided, isa will not try to resolve the dependency.
Previously, SaveModule method in ModuleDependency handler would invoke Find-Module and then Save-Module. This is inefficient as two queries are made the PSGallery server which are redundant. This commit removes Find-Module invocation.
A brief summary of the changes introduced by this PR:
|
@lzybkr @raghushantha @daviwil please review. Thanks! |
Review status: 0 of 6 files reviewed at latest revision, 18 unresolved discussions. Engine/ScriptAnalyzer.cs, line 1315 [r1] (raw file):
Errors will never be null. Engine/Generic/ModuleDependencyHandler.cs, line 24 [r1] (raw file):
I find this confusing - you aren't using symbolic links, right? Engine/Generic/ModuleDependencyHandler.cs, line 53 [r1] (raw file):
IsNullOrWhitespace covers the empty case as well, so this is redundant. Engine/Generic/ModuleDependencyHandler.cs, line 55 [r1] (raw file):
Path.GetTempPath () is probably more reliable. Engine/Generic/ModuleDependencyHandler.cs, line 73 [r1] (raw file):
Redundant Engine/Generic/ModuleDependencyHandler.cs, line 75 [r1] (raw file):
You should use Environment.GetFolderPath(Environment.SpecialFolder.LocalAppData) Engine/Generic/ModuleDependencyHandler.cs, line 93 [r1] (raw file):
Redundant Engine/Generic/ModuleDependencyHandler.cs, line 112 [r1] (raw file):
IsNullOrEmpty is redundant Engine/Generic/ModuleDependencyHandler.cs, line 115 [r1] (raw file):
You have a hidden dependency on ordering of assignments with LocalAppDataPath here - it might be better to not use private setters at all and just move this logic to the constructor. Engine/Generic/ModuleDependencyHandler.cs, line 145 [r1] (raw file):
You have lots of trailing whitespace in this file. Engine/Generic/ModuleDependencyHandler.cs, line 159 [r1] (raw file):
The null check is redundant - Directory.Exists will make the same check. Engine/Generic/ModuleDependencyHandler.cs, line 188 [r1] (raw file):
What happens if this fails, e.g. access denied? Engine/Generic/ModuleDependencyHandler.cs, line 207 [r1] (raw file):
This just looks wrong - read all lines, then forget all but the first? And it's not clear what's in the file you're reading anyway. Engine/Generic/ModuleDependencyHandler.cs, line 306 [r1] (raw file):
using ... - PowerShell implements IDisposable Engine/Generic/ModuleDependencyHandler.cs, line 335 [r1] (raw file):
Is this saved module ever deleted? Engine/Generic/ModuleDependencyHandler.cs, line 356 [r1] (raw file):
using ... - PowerShell implements IDisposable Engine/Generic/ModuleDependencyHandler.cs, line 390 [r1] (raw file):
There are other arguments that you need to worry about, e.g. the following is valid Engine/Generic/ModuleDependencyHandler.cs, line 422 [r1] (raw file):
People forget to call Dispose all the time (like you did). I'd rather see PSModulePath only changed around operations that require it, and restored afterwards. Comments from Reviewable |
Engine/Generic/ModuleDependencyHandler.cs, line 24 [r1] (raw file):
|
Engine/Generic/ModuleDependencyHandler.cs, line 335 [r1] (raw file):
|
Engine/Generic/ModuleDependencyHandler.cs, line 188 [r1] (raw file):
|
1 similar comment
Engine/Generic/ModuleDependencyHandler.cs, line 188 [r1] (raw file):
|
The rest of it LGTM. :) |
|
ModuleDependencyHandler class usage is changed to utilize the IDisposable pattern. It is instantiated only before analyzing the files and disposed right after that.
We should bring in @JKeithB for the naming of the parameter. |
Resolves #520
This change is