Skip to content

Fix PSSA settings discovery + pick up changes to settings file #1206

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 11 commits into from
Feb 26, 2020
46 changes: 35 additions & 11 deletions src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
Expand Down Expand Up @@ -93,6 +94,8 @@ internal static string GetUniqueIdFromDiagnostic(Diagnostic diagnostic)

private Lazy<PssaCmdletAnalysisEngine> _analysisEngine;

private FileSystemWatcher _pssaSettingsFileWatcher;

private CancellationTokenSource _diagnosticsCancellationTokenSource;

/// <summary>
Expand Down Expand Up @@ -253,13 +256,29 @@ public void ClearMarkers(ScriptFile file)
/// <param name="sender">The sender of the configuration update event.</param>
/// <param name="settings">The new language server settings.</param>
public void OnConfigurationUpdated(object sender, LanguageServerSettings settings)
{
ReinitializeAnalysisEngine();
}

private void OnSettingsFileUpdated(object sender, FileSystemEventArgs args)
{
ReinitializeAnalysisEngine();
}

private void ReinitializeAnalysisEngine()
{
ClearOpenFileMarkers();
_analysisEngine = new Lazy<PssaCmdletAnalysisEngine>(InstantiateAnalysisEngine);
}

private PssaCmdletAnalysisEngine InstantiateAnalysisEngine()
{
if (_pssaSettingsFileWatcher != null)
{
_pssaSettingsFileWatcher.Dispose();
_pssaSettingsFileWatcher = null;
}

if (!(_configurationService.CurrentSettings.ScriptAnalysis.Enable ?? false))
{
return null;
Expand All @@ -271,6 +290,11 @@ private PssaCmdletAnalysisEngine InstantiateAnalysisEngine()
if (TryFindSettingsFile(out string settingsFilePath))
{
_logger.LogInformation($"Configuring PSScriptAnalyzer with rules at '{settingsFilePath}'");
_pssaSettingsFileWatcher = new FileSystemWatcher(settingsFilePath)
{
EnableRaisingEvents = true,
};
_pssaSettingsFileWatcher.Changed += OnSettingsFileUpdated;
pssaCmdletEngineBuilder.WithSettingsFile(settingsFilePath);
}
else
Expand All @@ -286,22 +310,22 @@ private bool TryFindSettingsFile(out string settingsFilePath)
{
string configuredPath = _configurationService.CurrentSettings.ScriptAnalysis.SettingsPath;

if (!string.IsNullOrEmpty(configuredPath))
if (string.IsNullOrEmpty(configuredPath))
{
settingsFilePath = _workplaceService.ResolveWorkspacePath(configuredPath);
settingsFilePath = null;
return false;
}

if (settingsFilePath == null)
{
_logger.LogError($"Unable to find PSSA settings file at '{configuredPath}'. Loading default rules.");
}
settingsFilePath = _workplaceService.ResolveWorkspacePath(configuredPath);

return settingsFilePath != null;
if (settingsFilePath == null
|| !File.Exists(settingsFilePath))
{
_logger.LogError($"Unable to find PSSA settings file at '{configuredPath}'. Loading default rules.");
return false;
}

// TODO: Could search for a default here

settingsFilePath = null;
return false;
return true;
}

private void ClearOpenFileMarkers()
Expand Down