Skip to content

Fix #153: Script Analyzer is not returning markers #161

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

Merged
merged 1 commit into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions src/PowerShellEditorServices/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using Microsoft.PowerShell.EditorServices.Utility;
using Microsoft.Windows.PowerShell.ScriptAnalyzer;
using System;
using System.IO;
using System.Linq;
using System.Management.Automation.Runspaces;
using System.Threading;
Expand Down Expand Up @@ -47,17 +49,30 @@ public class AnalysisService : IDisposable
/// </summary>
public AnalysisService()
{
this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
this.analysisRunspace.ApartmentState = ApartmentState.STA;
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
this.analysisRunspace.Open();
try
{
// Attempt to create a ScriptAnalyzer instance first
// just in case the assembly can't be found and we
// can skip creating an extra runspace.
this.scriptAnalyzer = new ScriptAnalyzer();

this.analysisRunspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault2());
this.analysisRunspace.ApartmentState = ApartmentState.STA;
this.analysisRunspace.ThreadOptions = PSThreadOptions.ReuseThread;
this.analysisRunspace.Open();

this.scriptAnalyzer = new ScriptAnalyzer();
this.scriptAnalyzer.Initialize(
this.analysisRunspace,
new AnalysisOutputWriter(),
null,
IncludedRules);
this.scriptAnalyzer.Initialize(
this.analysisRunspace,
new AnalysisOutputWriter(),
includeRuleNames: IncludedRules,
includeDefaultRules: true);
}
catch (FileNotFoundException)
{
Logger.Write(
LogLevel.Warning,
"Script Analyzer binaries not found, AnalysisService will be disabled.");
}
}

#endregion
Expand All @@ -72,7 +87,7 @@ public AnalysisService()
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
public ScriptFileMarker[] GetSemanticMarkers(ScriptFile file)
{
if (file.IsAnalysisEnabled)
if (this.scriptAnalyzer != null && file.IsAnalysisEnabled)
{
// TODO: This is a temporary fix until we can change how
// ScriptAnalyzer invokes their async tasks.
Expand Down
16 changes: 16 additions & 0 deletions test/PowerShellEditorServices.Test.Host/LanguageServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ await this.WaitForEvent(
string.IsNullOrEmpty(diagnostics.Diagnostics[0].Message));
}

[Fact]
public async Task ServiceReturnsSemanticMarkers()
{
// Send the 'didOpen' event
await this.SendOpenFileEvent("TestFiles\\SimpleSemanticError.ps1", false);

// Wait for the diagnostic event
PublishDiagnosticsNotification diagnostics =
await this.WaitForEvent(
PublishDiagnosticsNotification.Type);

// Was there a semantic error?
Assert.NotEqual(0, diagnostics.Diagnostics.Length);
Assert.Contains("unapproved", diagnostics.Diagnostics[0].Message);
}

[Fact]
public async Task ServiceCompletesFunctionName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@
<None Include="TestFiles\MultiLineReplace.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestFiles\SimpleSemanticError.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestFiles\SimpleSyntaxError.ps1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function Do-Work {
# This should trigger the PSUseApprovedVerbs rule
}