Skip to content

Commit f08ad85

Browse files
committed
Fix #285: User's PSScriptAnalyzer settings path not being used
This change fixes an issue caused by the recent refactoring to use PSScriptAnalyzer as a module rather than a C# library. We forgot to pass along the user-specified settings path to the -Settings parameter of the Invoke-ScriptAnalyzer cmdlet.
1 parent dc7257e commit f08ad85

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ protected async Task HandleDidChangeConfigurationNotification(
366366
string newSettingsPath = this.currentSettings.ScriptAnalysis.SettingsPath;
367367
if (!string.Equals(oldScriptAnalysisSettingsPath, newSettingsPath, StringComparison.OrdinalIgnoreCase))
368368
{
369-
this.editorSession.RestartAnalysisService(newSettingsPath);
369+
this.editorSession.AnalysisService.SettingsPath = newSettingsPath;
370370
settingsPathChanged = true;
371371
}
372372

src/PowerShellEditorServices/Analysis/AnalysisService.cs

+33-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using Microsoft.PowerShell.EditorServices.Utility;
77
using System;
8-
using System.IO;
98
using System.Linq;
109
using System.Management.Automation.Runspaces;
1110
using System.Threading;
@@ -44,6 +43,21 @@ public class AnalysisService : IDisposable
4443

4544
#endregion // Private Fields
4645

46+
47+
#region Properties
48+
49+
/// <summary>
50+
/// Gets or sets the path to a settings file (.psd1)
51+
/// containing PSScriptAnalyzer settings.
52+
/// </summary>
53+
public string SettingsPath
54+
{
55+
get;
56+
set;
57+
}
58+
59+
#endregion
60+
4761
#region Constructors
4862

4963
/// <summary>
@@ -56,6 +70,7 @@ public AnalysisService(IConsoleHost consoleHost, string settingsPath = null)
5670
{
5771
try
5872
{
73+
this.SettingsPath = settingsPath;
5974
this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
6075
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
6176
this.analysisRunspace.Open();
@@ -219,17 +234,28 @@ private IEnumerable<PSObject> GetDiagnosticRecords(ScriptFile file)
219234

220235
if (this.scriptAnalyzerModuleInfo != null)
221236
{
222-
using (var ps = System.Management.Automation.PowerShell.Create())
237+
using (var powerShell = System.Management.Automation.PowerShell.Create())
223238
{
224-
ps.Runspace = this.analysisRunspace;
239+
powerShell.Runspace = this.analysisRunspace;
225240
Logger.Write(
226241
LogLevel.Verbose,
227242
String.Format("Running PSScriptAnalyzer against {0}", file.FilePath));
228243

229-
diagnosticRecords = ps.AddCommand("Invoke-ScriptAnalyzer")
230-
.AddParameter("ScriptDefinition", file.Contents)
231-
.AddParameter("IncludeRule", IncludedRules)
232-
.Invoke();
244+
powerShell
245+
.AddCommand("Invoke-ScriptAnalyzer")
246+
.AddParameter("ScriptDefinition", file.Contents);
247+
248+
// Use a settings file if one is provided, otherwise use the default rule list.
249+
if (!string.IsNullOrWhiteSpace(this.SettingsPath))
250+
{
251+
powerShell.AddParameter("Settings", this.SettingsPath);
252+
}
253+
else
254+
{
255+
powerShell.AddParameter("IncludeRule", IncludedRules);
256+
}
257+
258+
diagnosticRecords = powerShell.Invoke();
233259
}
234260
}
235261

src/PowerShellEditorServices/Session/EditorSession.cs

-10
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,6 @@ public void StartDebugSession(HostDetails hostDetails, ProfilePaths profilePaths
104104
this.Workspace = new Workspace(this.PowerShellContext.PowerShellVersion);
105105
}
106106

107-
/// <summary>
108-
/// Restarts the AnalysisService so it can be configured with a new settings file.
109-
/// </summary>
110-
/// <param name="settingsPath">Path to the settings file.</param>
111-
public void RestartAnalysisService(string settingsPath)
112-
{
113-
this.AnalysisService?.Dispose();
114-
InstantiateAnalysisService(settingsPath);
115-
}
116-
117107
internal void InstantiateAnalysisService(string settingsPath = null)
118108
{
119109
// Only enable the AnalysisService if the machine has PowerShell

0 commit comments

Comments
 (0)