Skip to content

Commit fd81167

Browse files
committed
Fix #325: Module dependency loading should pick latest module version
This change fixes an issue with the loading of both the PSScriptAnalyzer and Plaster modules. We were simply using Import-Module without specifying a version to be loaded. Apparently PowerShell prioritizes the PSModulePath order over the module version order so there are instances where an older version of one of these module will be loaded even though a newer version is available at a different location on the machine. This change adds extra logic to sort the list of returned modules so that the latest version of the module is always loaded.
1 parent b695ba3 commit fd81167

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/PowerShellEditorServices/Analysis/AnalysisService.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,18 @@ private void FindPSScriptAnalyzer()
179179
{
180180
ps.Runspace = this.analysisRunspace;
181181

182-
var modules = ps.AddCommand("Get-Module")
183-
.AddParameter("List")
184-
.AddParameter("Name", "PSScriptAnalyzer")
185-
.Invoke();
182+
ps.AddCommand("Get-Module")
183+
.AddParameter("ListAvailable")
184+
.AddParameter("Name", "PSScriptAnalyzer");
185+
186+
ps.AddCommand("Sort-Object")
187+
.AddParameter("Descending")
188+
.AddParameter("Property", "Version");
189+
190+
ps.AddCommand("Select-Object")
191+
.AddParameter("First", 1);
192+
193+
var modules = ps.Invoke();
186194

187195
var psModule = modules == null ? null : modules.FirstOrDefault();
188196
if (psModule != null)

src/PowerShellEditorServices/Templates/TemplateService.cs

+21-8
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,29 @@ public async Task<bool> ImportPlasterIfInstalled()
5252
if (!this.isPlasterInstalled.HasValue)
5353
{
5454
PSCommand psCommand = new PSCommand();
55-
psCommand.AddCommand("Get-Module");
56-
psCommand.AddParameter("ListAvailable");
57-
psCommand.AddParameter("Name", "Plaster");
55+
56+
psCommand
57+
.AddCommand("Get-Module")
58+
.AddParameter("ListAvailable")
59+
.AddParameter("Name", "Plaster");
60+
61+
psCommand
62+
.AddCommand("Sort-Object")
63+
.AddParameter("Descending")
64+
.AddParameter("Property", "Version");
65+
66+
psCommand
67+
.AddCommand("Select-Object")
68+
.AddParameter("First", 1);
5869

5970
Logger.Write(LogLevel.Verbose, "Checking if Plaster is installed...");
6071

6172
var getResult =
62-
await this.powerShellContext.ExecuteCommand<object>(
73+
await this.powerShellContext.ExecuteCommand<PSObject>(
6374
psCommand, false, false);
6475

65-
this.isPlasterInstalled = getResult.Any();
76+
PSObject moduleObject = getResult.First();
77+
this.isPlasterInstalled = moduleObject != null;
6678
string installedQualifier =
6779
this.isPlasterInstalled.Value
6880
? string.Empty : "not ";
@@ -77,9 +89,10 @@ await this.powerShellContext.ExecuteCommand<object>(
7789
Logger.Write(LogLevel.Verbose, "Loading Plaster...");
7890

7991
psCommand = new PSCommand();
80-
psCommand.AddCommand("Import-Module");
81-
psCommand.AddParameter("Name", "Plaster");
82-
psCommand.AddParameter("PassThru");
92+
psCommand
93+
.AddCommand("Import-Module")
94+
.AddParameter("ModuleInfo", (PSModuleInfo)moduleObject.ImmediateBaseObject)
95+
.AddParameter("PassThru");
8396

8497
var importResult =
8598
await this.powerShellContext.ExecuteCommand<object>(

0 commit comments

Comments
 (0)