Skip to content

Add linting to PSES, with fixes for obvious cases #1155

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 22 commits into from
Jan 14, 2020
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
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ trim_trailing_whitespace = true

[*.{ps1xml,props,xml,yaml}]
indent_size = 2

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none
4 changes: 3 additions & 1 deletion PowerShellEditorServices.build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ task CreateBuildInfo -Before Build {
[string]$buildTime = [datetime]::Now.ToString("s", [System.Globalization.CultureInfo]::InvariantCulture)

$buildInfoContents = @"
using System.Globalization;

namespace Microsoft.PowerShell.EditorServices.Hosting
{
public static class BuildInfo
{
public static readonly string BuildVersion = "$buildVersion";
public static readonly string BuildOrigin = "$buildOrigin";
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime");
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime", CultureInfo.InvariantCulture.DateTimeFormat);
}
}
"@
Expand Down
4 changes: 3 additions & 1 deletion src/PowerShellEditorServices.Hosting/BuildInfo.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System.Globalization;

namespace Microsoft.PowerShell.EditorServices.Hosting
{
public static class BuildInfo
{
public static readonly string BuildVersion = "<development-build>";
public static readonly string BuildOrigin = "<development>";
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41");
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41", CultureInfo.InvariantCulture.DateTimeFormat);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Reflection;
using SMA = System.Management.Automation;
using Microsoft.PowerShell.EditorServices.Hosting;
using System.Globalization;

#if DEBUG
using System.Diagnostics;
Expand All @@ -25,6 +26,7 @@ namespace Microsoft.PowerShell.EditorServices.Commands
/// <summary>
/// The Start-EditorServices command, the conventional entrypoint for PowerShell Editor Services.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1819:Properties should not return arrays", Justification = "Cmdlet parameters can be arrays")]
[Cmdlet(VerbsLifecycle.Start, "EditorServices", DefaultParameterSetName = "NamedPipe")]
public sealed class StartEditorServicesCommand : PSCmdlet
{
Expand Down Expand Up @@ -200,6 +202,7 @@ protected override void BeginProcessing()
StartLogging();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Uses ThrowTerminatingError() instead")]
protected override void EndProcessing()
{
_logger.Log(PsesLogLevel.Diagnostic, "Beginning EndProcessing block");
Expand Down Expand Up @@ -264,7 +267,7 @@ private void StartLogging()
if (File.Exists(logPath))
{
int randomInt = new Random().Next();
logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X")}.log");
logPath = Path.Combine(logDirPath, $"StartEditorServices-temp{randomInt.ToString("X", CultureInfo.InvariantCulture.NumberFormat)}.log");
}

var fileLogger = StreamLogger.CreateWithNewFile(logPath);
Expand Down
4 changes: 3 additions & 1 deletion src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ namespace Microsoft.PowerShell.EditorServices.Hosting
/// </summary>
public sealed class EditorServicesLoader : IDisposable
{
private const int Net461Version = 394254;

#if !CoreCLR
private const int Net461Version = 394254;

private static readonly string s_psesBaseDirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#endif

Expand Down Expand Up @@ -369,6 +370,7 @@ private string GetOSArchitecture()
return RuntimeInformation.OSArchitecture.ToString();
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "Checking user-defined configuration")]
private void ValidateConfiguration()
{
_logger.Log(PsesLogLevel.Diagnostic, "Validating configuration");
Expand Down
6 changes: 6 additions & 0 deletions src/PowerShellEditorServices.Hosting/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "PSES is not localized", Scope = "module")]
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
debugServerCreation = CreateDebugServerWithLanguageServerAsync(languageServer, usePSReadLine: _config.ConsoleRepl == ConsoleReplKind.PSReadLine);
}

#pragma warning disable CS4014
// We don't need to wait for this to start, since we instead wait for it to complete later
languageServer.StartAsync();
#pragma warning restore CS4014

if (creatingDebugServer)
{
#pragma warning disable CS4014
// We don't need to wait for this to start, since we instead wait for it to complete later
StartDebugServer(debugServerCreation);
#pragma warning restore CS4014
}

await languageServer.WaitForShutdown().ConfigureAwait(false);
Expand Down Expand Up @@ -155,7 +161,11 @@ private async Task StartDebugServer(Task<PsesDebugServer> debugServerCreation)
}

_logger.Log(PsesLogLevel.Diagnostic, "Starting debug server");

#pragma warning disable CS4014
// No need to await, since we just want to kick it off
debugServer.StartAsync();
#pragma warning restore CS4014
}

private Task RestartDebugServerAsync(PsesDebugServer debugServer, bool usePSReadLine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected override Assembly Load(AssemblyName assemblyName)
return null;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Best effort; we must not throw if we fail")]
private void TrySetName(string name)
{
try
Expand Down
6 changes: 6 additions & 0 deletions src/PowerShellEditorServices/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "PSES is not localized", Scope = "module")]
4 changes: 2 additions & 2 deletions src/PowerShellEditorServices/Server/PsesDebugServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task StartAsync()
.WithHandler<DebugEvaluateHandler>();

logger.LogInformation("Handlers added");
});
}).ConfigureAwait(false);
}

public void Dispose()
Expand All @@ -129,7 +129,7 @@ public void Dispose()

public async Task WaitForShutdown()
{
await _serverStopped.Task;
await _serverStopped.Task.ConfigureAwait(false);
}

#region Events
Expand Down
8 changes: 4 additions & 4 deletions src/PowerShellEditorServices/Server/PsesLanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ public async Task StartAsync()
{
await serviceProvider.GetService<PowerShellContextService>().SetWorkingDirectoryAsync(
workspaceService.WorkspacePath,
isPathAlreadyEscaped: false);
isPathAlreadyEscaped: false).ConfigureAwait(false);
}
});
});
}).ConfigureAwait(false);

_serverStart.SetResult(true);
}
Expand All @@ -124,8 +124,8 @@ await serviceProvider.GetService<PowerShellContextService>().SetWorkingDirectory
/// <returns>A task that completes when the server is shut down.</returns>
public async Task WaitForShutdown()
{
await _serverStart.Task;
await LanguageServer.WaitForExit;
await _serverStart.Task.ConfigureAwait(false);
await LanguageServer.WaitForExit.ConfigureAwait(false);
}
}
}
Loading