Skip to content

Commit 84cf1ff

Browse files
committed
Merge pull request #161 from PowerShell/daviwil/script-analyzer-fix
Fix #153: Script Analyzer is not returning markers
2 parents 3c1e119 + b792896 commit 84cf1ff

File tree

4 files changed

+48
-11
lines changed

4 files changed

+48
-11
lines changed

Diff for: src/PowerShellEditorServices/Analysis/AnalysisService.cs

+26-11
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
44
//
55

6+
using Microsoft.PowerShell.EditorServices.Utility;
67
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
78
using System;
9+
using System.IO;
810
using System.Linq;
911
using System.Management.Automation.Runspaces;
1012
using System.Threading;
@@ -47,17 +49,30 @@ public class AnalysisService : IDisposable
4749
/// </summary>
4850
public AnalysisService()
4951
{
50-
this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
51-
this.analysisRunspace.ApartmentState = ApartmentState.STA;
52-
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
53-
this.analysisRunspace.Open();
52+
try
53+
{
54+
// Attempt to create a ScriptAnalyzer instance first
55+
// just in case the assembly can't be found and we
56+
// can skip creating an extra runspace.
57+
this.scriptAnalyzer = new ScriptAnalyzer();
58+
59+
this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
60+
this.analysisRunspace.ApartmentState = ApartmentState.STA;
61+
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
62+
this.analysisRunspace.Open();
5463

55-
this.scriptAnalyzer = new ScriptAnalyzer();
56-
this.scriptAnalyzer.Initialize(
57-
this.analysisRunspace,
58-
new AnalysisOutputWriter(),
59-
null,
60-
IncludedRules);
64+
this.scriptAnalyzer.Initialize(
65+
this.analysisRunspace,
66+
new AnalysisOutputWriter(),
67+
includeRuleNames: IncludedRules,
68+
includeDefaultRules: true);
69+
}
70+
catch (FileNotFoundException)
71+
{
72+
Logger.Write(
73+
LogLevel.Warning,
74+
"Script Analyzer binaries not found, AnalysisService will be disabled.");
75+
}
6176
}
6277

6378
#endregion
@@ -72,7 +87,7 @@ public AnalysisService()
7287
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
7388
public ScriptFileMarker[] GetSemanticMarkers(ScriptFile file)
7489
{
75-
if (file.IsAnalysisEnabled)
90+
if (this.scriptAnalyzer != null && file.IsAnalysisEnabled)
7691
{
7792
// TODO: This is a temporary fix until we can change how
7893
// ScriptAnalyzer invokes their async tasks.

Diff for: test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ await this.WaitForEvent(
6565
string.IsNullOrEmpty(diagnostics.Diagnostics[0].Message));
6666
}
6767

68+
[Fact]
69+
public async Task ServiceReturnsSemanticMarkers()
70+
{
71+
// Send the 'didOpen' event
72+
await this.SendOpenFileEvent("TestFiles\\SimpleSemanticError.ps1", false);
73+
74+
// Wait for the diagnostic event
75+
PublishDiagnosticsNotification diagnostics =
76+
await this.WaitForEvent(
77+
PublishDiagnosticsNotification.Type);
78+
79+
// Was there a semantic error?
80+
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
81+
Assert.Contains("unapproved", diagnostics.Diagnostics[0].Message);
82+
}
83+
6884
[Fact]
6985
public async Task ServiceCompletesFunctionName()
7086
{

Diff for: test/PowerShellEditorServices.Test.Host/PowerShellEditorServices.Test.Host.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@
8383
<None Include="TestFiles\MultiLineReplace.ps1">
8484
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8585
</None>
86+
<None Include="TestFiles\SimpleSemanticError.ps1">
87+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
88+
</None>
8689
<None Include="TestFiles\SimpleSyntaxError.ps1">
8790
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
8891
</None>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function Do-Work {
2+
# This should trigger the PSUseApprovedVerbs rule
3+
}

0 commit comments

Comments
 (0)