Skip to content

Commit dba6515

Browse files
committed
Merge branch 'master' into develop
2 parents 9b22397 + 50ab29f commit dba6515

File tree

12 files changed

+111
-36
lines changed

12 files changed

+111
-36
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# PowerShell Editor Services Release History
22

3+
## 0.7.2
4+
### Friday, September 2, 2016
5+
6+
- Fixed #284: PowerShellContext.AbortException crashes when called more than once
7+
- Fixed #285: PSScriptAnalyzer settings are not being passed to Invoke-ScriptAnalyzer
8+
- Fixed #287: Language service crashes when invalid path chars are used in dot-sourced script reference
9+
310
## 0.7.1
411
### Tuesday, August 23, 2016
512

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ clone_depth: 10
55
skip_tags: true
66

77
environment:
8-
core_version: '0.7.1'
8+
core_version: '0.7.2'
99
prerelease_name: '-beta'
1010

1111
branches:

module/PowerShellEditorServices/PowerShellEditorServices.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PowerShellEditorServices.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.7.1'
15+
ModuleVersion = '0.7.2'
1616

1717
# ID used to uniquely identify this module
1818
GUID = '9ca15887-53a2-479a-9cda-48d26bcb6c47'

src/PowerShellEditorServices.Protocol/MessageProtocol/MessageDispatcher.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ private void OnListenTaskCompleted(Task listenTask)
331331
{
332332
if (listenTask.IsFaulted)
333333
{
334+
Logger.Write(
335+
LogLevel.Error,
336+
string.Format(
337+
"MessageDispatcher loop terminated due to unhandled exception:\r\n\r\n{0}",
338+
listenTask.Exception.ToString()));
339+
334340
this.OnUnhandledException(listenTask.Exception);
335341
}
336342
else if (listenTask.IsCompleted || listenTask.IsCanceled)

src/PowerShellEditorServices.Protocol/Server/DebugAdapter.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,15 +203,20 @@ protected Task HandleDisconnectRequest(
203203
if (e.NewSessionState == PowerShellContextState.Ready)
204204
{
205205
await requestContext.SendResult(null);
206-
editorSession.PowerShellContext.SessionStateChanged -= handler;
206+
this.editorSession.PowerShellContext.SessionStateChanged -= handler;
207207

208208
// Stop the server
209209
await this.Stop();
210210
}
211211
};
212212

213-
editorSession.PowerShellContext.SessionStateChanged += handler;
214-
editorSession.PowerShellContext.AbortExecution();
213+
// In some rare cases, the EditorSession will already be disposed
214+
// so we shouldn't try to abort because PowerShellContext will be null
215+
if (this.editorSession != null && this.editorSession.PowerShellContext != null)
216+
{
217+
this.editorSession.PowerShellContext.SessionStateChanged += handler;
218+
this.editorSession.PowerShellContext.AbortExecution();
219+
}
215220

216221
return Task.FromResult(true);
217222
}

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 33 additions & 7 deletions
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/Language/FindDotSourcedVisitor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
3434
{
3535
if (commandAst.InvocationOperator.Equals(TokenKind.Dot))
3636
{
37-
string fileName = commandAst.CommandElements[0].Extent.Text;
37+
// Strip any quote characters off of the string
38+
string fileName = commandAst.CommandElements[0].Extent.Text.Trim('\'', '"');
3839
DotSourcedFiles.Add(fileName);
3940
}
41+
4042
return base.VisitCommand(commandAst);
4143
}
4244
}

src/PowerShellEditorServices/Session/EditorSession.cs

Lines changed: 0 additions & 10 deletions
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

src/PowerShellEditorServices/Session/PowerShellContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ public async Task LoadHostProfiles()
601601
/// </summary>
602602
public void AbortExecution()
603603
{
604-
if (this.SessionState != PowerShellContextState.Aborting)
604+
if (this.SessionState != PowerShellContextState.Aborting &&
605+
this.SessionState != PowerShellContextState.Disposed)
605606
{
606607
Logger.Write(LogLevel.Verbose, "Execution abort requested...");
607608

@@ -617,7 +618,8 @@ public void AbortExecution()
617618
{
618619
Logger.Write(
619620
LogLevel.Verbose,
620-
"Execution abort requested while already aborting");
621+
string.Format(
622+
$"Execution abort requested when already aborted (SessionState = {this.SessionState})"));
621623
}
622624
}
623625

src/PowerShellEditorServices/Workspace/Workspace.cs

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ private void RecursivelyFindReferences(
203203
baseFilePath,
204204
referencedFileName);
205205

206+
// If there was an error resolving the string, skip this reference
207+
if (resolvedScriptPath == null)
208+
{
209+
continue;
210+
}
211+
206212
Logger.Write(
207213
LogLevel.Verbose,
208214
string.Format(
@@ -287,18 +293,49 @@ private string GetBaseFilePath(string filePath)
287293

288294
private string ResolveRelativeScriptPath(string baseFilePath, string relativePath)
289295
{
290-
if (Path.IsPathRooted(relativePath))
296+
string combinedPath = null;
297+
Exception resolveException = null;
298+
299+
try
291300
{
292-
return relativePath;
301+
// If the path is already absolute there's no need to resolve it relatively
302+
// to the baseFilePath.
303+
if (Path.IsPathRooted(relativePath))
304+
{
305+
return relativePath;
306+
}
307+
308+
// Get the directory of the original script file, combine it
309+
// with the given path and then resolve the absolute file path.
310+
combinedPath =
311+
Path.GetFullPath(
312+
Path.Combine(
313+
baseFilePath,
314+
relativePath));
315+
}
316+
catch (NotSupportedException e)
317+
{
318+
// Occurs if the path is incorrectly formatted for any reason. One
319+
// instance where this occurred is when a user had curly double-quote
320+
// characters in their source instead of normal double-quotes.
321+
resolveException = e;
322+
}
323+
catch (ArgumentException e)
324+
{
325+
// Occurs if the path contains invalid characters, specifically those
326+
// listed in System.IO.Path.InvalidPathChars.
327+
resolveException = e;
293328
}
294329

295-
// Get the directory of the original script file, combine it
296-
// with the given path and then resolve the absolute file path.
297-
string combinedPath =
298-
Path.GetFullPath(
299-
Path.Combine(
300-
baseFilePath,
301-
relativePath));
330+
if (resolveException != null)
331+
{
332+
Logger.Write(
333+
LogLevel.Error,
334+
$"Could not resolve relative script path\r\n" +
335+
$" baseFilePath = {baseFilePath}\r\n " +
336+
$" relativePath = {relativePath}\r\n\r\n" +
337+
$"{resolveException.ToString()}");
338+
}
302339

303340
return combinedPath;
304341
}

test/PowerShellEditorServices.Test.Host/ServerTestsBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected async Task<Tuple<int, int>> LaunchService(
3434
string scriptPath = Path.Combine(modulePath, "Start-EditorServices.ps1");
3535

3636
// TODO: Need to determine the right module version programmatically!
37-
string editorServicesModuleVersion = "0.7.1";
37+
string editorServicesModuleVersion = "0.7.2";
3838

3939
string scriptArgs =
4040
string.Format(

0 commit comments

Comments
 (0)