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 ;
67using Microsoft . Windows . PowerShell . ScriptAnalyzer ;
78using System ;
9+ using System . IO ;
810using System . Linq ;
911using System . Management . Automation . Runspaces ;
1012using 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.
0 commit comments