Skip to content

Commit 81ba0c0

Browse files
authored
[Ignore] Add linting to PSES, with fixes for obvious cases (#1155)
* Use culture invariant int render * Suppress unhelpful lint messages * Remove useless .editorconfig line * Add suppressions file to PSES project * Add ConfigureAwait(false) to await calls * Check nulls on public APIs * More ConfigureAwait(false) * Ensure BuildInfo data is parsed properly * Suggested changes * Fix suppression files * Update src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs * Use Validate for null check * Use invariant datetime in build * Use validate * Add rule suppressions * Add ConfigureAwait(false) to await calls * Suppress non-awaited tasks * Use Array.Empty() instead of new [] * Fix common tests * Suppress cctor
1 parent 4fc1362 commit 81ba0c0

File tree

70 files changed

+426
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+426
-366
lines changed

.editorconfig

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,3 @@ trim_trailing_whitespace = true
2121

2222
[*.{ps1xml,props,xml,yaml}]
2323
indent_size = 2
24-
25-
# CA1303: Do not pass literals as localized parameters
26-
dotnet_diagnostic.CA1303.severity = none

PowerShellEditorServices.build.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,13 +186,15 @@ task CreateBuildInfo -Before Build {
186186
[string]$buildTime = [datetime]::Now.ToString("s", [System.Globalization.CultureInfo]::InvariantCulture)
187187

188188
$buildInfoContents = @"
189+
using System.Globalization;
190+
189191
namespace Microsoft.PowerShell.EditorServices.Hosting
190192
{
191193
public static class BuildInfo
192194
{
193195
public static readonly string BuildVersion = "$buildVersion";
194196
public static readonly string BuildOrigin = "$buildOrigin";
195-
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime");
197+
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("$buildTime", CultureInfo.InvariantCulture.DateTimeFormat);
196198
}
197199
}
198200
"@
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System.Globalization;
2+
13
namespace Microsoft.PowerShell.EditorServices.Hosting
24
{
35
public static class BuildInfo
46
{
57
public static readonly string BuildVersion = "<development-build>";
68
public static readonly string BuildOrigin = "<development>";
7-
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41");
9+
public static readonly System.DateTime? BuildTime = System.DateTime.Parse("2019-12-06T21:43:41", CultureInfo.InvariantCulture.DateTimeFormat);
810
}
911
}

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Reflection;
1313
using SMA = System.Management.Automation;
1414
using Microsoft.PowerShell.EditorServices.Hosting;
15+
using System.Globalization;
1516

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

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

270273
var fileLogger = StreamLogger.CreateWithNewFile(logPath);

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ namespace Microsoft.PowerShell.EditorServices.Hosting
2727
/// </summary>
2828
public sealed class EditorServicesLoader : IDisposable
2929
{
30-
private const int Net461Version = 394254;
3130

3231
#if !CoreCLR
32+
private const int Net461Version = 394254;
33+
3334
private static readonly string s_psesBaseDirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
3435
#endif
3536

@@ -369,6 +370,7 @@ private string GetOSArchitecture()
369370
return RuntimeInformation.OSArchitecture.ToString();
370371
}
371372

373+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "CA2208:Instantiate argument exceptions correctly", Justification = "Checking user-defined configuration")]
372374
private void ValidateConfiguration()
373375
{
374376
_logger.Log(PsesLogLevel.Diagnostic, "Validating configuration");
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "PSES is not localized", Scope = "module")]

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,17 @@ private async Task CreateEditorServicesAndRunUntilShutdown()
115115
debugServerCreation = CreateDebugServerWithLanguageServerAsync(languageServer, usePSReadLine: _config.ConsoleRepl == ConsoleReplKind.PSReadLine);
116116
}
117117

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

120123
if (creatingDebugServer)
121124
{
125+
#pragma warning disable CS4014
126+
// We don't need to wait for this to start, since we instead wait for it to complete later
122127
StartDebugServer(debugServerCreation);
128+
#pragma warning restore CS4014
123129
}
124130

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

157163
_logger.Log(PsesLogLevel.Diagnostic, "Starting debug server");
164+
165+
#pragma warning disable CS4014
166+
// No need to await, since we just want to kick it off
158167
debugServer.StartAsync();
168+
#pragma warning restore CS4014
159169
}
160170

161171
private Task RestartDebugServerAsync(PsesDebugServer debugServer, bool usePSReadLine)

src/PowerShellEditorServices.Hosting/Internal/PsesLoadContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected override Assembly Load(AssemblyName assemblyName)
5454
return null;
5555
}
5656

57+
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Best effort; we must not throw if we fail")]
5758
private void TrySetName(string name)
5859
{
5960
try
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This file is used by Code Analysis to maintain SuppressMessage
2+
// attributes that are applied to this project.
3+
// Project-level suppressions either have no target or are given
4+
// a specific target and scoped to a namespace, type, member, etc.
5+
6+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "PSES is not localized", Scope = "module")]

src/PowerShellEditorServices/Server/PsesDebugServer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task StartAsync()
117117
.WithHandler<DebugEvaluateHandler>();
118118

119119
logger.LogInformation("Handlers added");
120-
});
120+
}).ConfigureAwait(false);
121121
}
122122

123123
public void Dispose()
@@ -129,7 +129,7 @@ public void Dispose()
129129

130130
public async Task WaitForShutdown()
131131
{
132-
await _serverStopped.Task;
132+
await _serverStopped.Task.ConfigureAwait(false);
133133
}
134134

135135
#region Events

src/PowerShellEditorServices/Server/PsesLanguageServer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ public async Task StartAsync()
110110
{
111111
await serviceProvider.GetService<PowerShellContextService>().SetWorkingDirectoryAsync(
112112
workspaceService.WorkspacePath,
113-
isPathAlreadyEscaped: false);
113+
isPathAlreadyEscaped: false).ConfigureAwait(false);
114114
}
115115
});
116-
});
116+
}).ConfigureAwait(false);
117117

118118
_serverStart.SetResult(true);
119119
}
@@ -124,8 +124,8 @@ await serviceProvider.GetService<PowerShellContextService>().SetWorkingDirectory
124124
/// <returns>A task that completes when the server is shut down.</returns>
125125
public async Task WaitForShutdown()
126126
{
127-
await _serverStart.Task;
128-
await LanguageServer.WaitForExit;
127+
await _serverStart.Task.ConfigureAwait(false);
128+
await LanguageServer.WaitForExit.ConfigureAwait(false);
129129
}
130130
}
131131
}

0 commit comments

Comments
 (0)