Skip to content

Commit ec4bc2f

Browse files
committed
Treat missing .ConfigureAwait(false) as error (and fix)
1 parent de15ce4 commit ec4bc2f

File tree

17 files changed

+119
-135
lines changed

17 files changed

+119
-135
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ trim_trailing_whitespace = true
1313
csharp_space_before_open_square_brackets = true
1414
csharp_space_after_keywords_in_control_flow_statements = true
1515

16+
# CA2007: Do not directly await a Task
17+
dotnet_diagnostic.CA2007.severity = error
18+
1619
[*.{json}]
1720
indent_size = 2
1821
trim_trailing_whitespace = true

src/PowerShellEditorServices.VSCode/CustomViews/CustomViewBase.cs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,31 @@ public CustomViewBase(
2828
this.languageServer = languageServer;
2929
}
3030

31-
internal async Task CreateAsync()
32-
{
33-
await languageServer.SendRequestAsync(
31+
internal Task CreateAsync() =>
32+
languageServer.SendRequestAsync(
3433
NewCustomViewRequest.Method,
3534
new NewCustomViewRequest
3635
{
3736
Id = this.Id,
3837
Title = this.Title,
3938
ViewType = this.ViewType,
40-
}
41-
);
42-
}
39+
});
4340

44-
public async Task Show(ViewColumn viewColumn)
45-
{
46-
await languageServer.SendRequestAsync(
41+
public Task Show(ViewColumn viewColumn) =>
42+
languageServer.SendRequestAsync(
4743
ShowCustomViewRequest.Method,
4844
new ShowCustomViewRequest
4945
{
5046
Id = this.Id,
5147
ViewColumn = viewColumn
52-
}
53-
);
54-
}
48+
});
5549

56-
public async Task Close()
57-
{
58-
await languageServer.SendRequestAsync(
50+
public Task Close() =>
51+
languageServer.SendRequestAsync(
5952
CloseCustomViewRequest.Method,
6053
new CloseCustomViewRequest
6154
{
6255
Id = this.Id,
63-
}
64-
);
65-
}
56+
});
6657
}
6758
}

src/PowerShellEditorServices.VSCode/CustomViews/HtmlContentView.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,40 @@ public HtmlContentView(
2121
{
2222
}
2323

24-
public async Task SetContentAsync(string htmlBodyContent)
25-
{
26-
await languageServer.SendRequestAsync(
24+
public Task SetContentAsync(string htmlBodyContent) =>
25+
languageServer.SendRequestAsync(
2726
SetHtmlContentViewRequest.Method,
2827
new SetHtmlContentViewRequest
2928
{
3029
Id = this.Id,
3130
HtmlContent = new HtmlContent { BodyContent = htmlBodyContent }
3231
}
3332
);
34-
}
35-
36-
public async Task SetContentAsync(HtmlContent htmlContent)
37-
{
38-
HtmlContent validatedContent =
39-
new HtmlContent()
40-
{
41-
BodyContent = htmlContent.BodyContent,
42-
JavaScriptPaths = this.GetUriPaths(htmlContent.JavaScriptPaths),
43-
StyleSheetPaths = this.GetUriPaths(htmlContent.StyleSheetPaths)
44-
};
4533

46-
await languageServer.SendRequestAsync(
34+
public Task SetContentAsync(HtmlContent htmlContent) =>
35+
languageServer.SendRequestAsync(
4736
SetHtmlContentViewRequest.Method,
4837
new SetHtmlContentViewRequest
4938
{
5039
Id = this.Id,
51-
HtmlContent = validatedContent
40+
HtmlContent = new HtmlContent()
41+
{
42+
BodyContent = htmlContent.BodyContent,
43+
JavaScriptPaths = this.GetUriPaths(htmlContent.JavaScriptPaths),
44+
StyleSheetPaths = this.GetUriPaths(htmlContent.StyleSheetPaths)
45+
}
5246
}
5347
);
54-
}
5548

56-
public async Task AppendContentAsync(string appendedHtmlBodyContent)
57-
{
58-
await languageServer.SendRequestAsync(
49+
public Task AppendContentAsync(string appendedHtmlBodyContent) =>
50+
languageServer.SendRequestAsync(
5951
AppendHtmlContentViewRequest.Method,
6052
new AppendHtmlContentViewRequest
6153
{
6254
Id = this.Id,
6355
AppendedHtmlBodyContent = appendedHtmlBodyContent
6456
}
6557
);
66-
}
6758

6859
private string[] GetUriPaths(string[] filePaths)
6960
{

src/PowerShellEditorServices.VSCode/CustomViews/HtmlContentViewsFeature.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public async Task<IHtmlContentView> CreateHtmlContentViewAsync(string viewTitle)
2121
viewTitle,
2222
languageServer);
2323

24-
await htmlView.CreateAsync();
24+
await htmlView.CreateAsync().ConfigureAwait(false);
2525
this.AddView(htmlView);
2626

2727
return htmlView;

src/PowerShellEditorServices/Extensions/Api/EditorUIService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public async Task<string> PromptInputAsync(string message)
116116
new ShowInputPromptRequest
117117
{
118118
Name = message,
119-
}).Returning<ShowInputPromptResponse>(CancellationToken.None);
119+
}).Returning<ShowInputPromptResponse>(CancellationToken.None).ConfigureAwait(false);
120120

121121
if (response.PromptCancelled)
122122
{
@@ -142,7 +142,7 @@ public async Task<IReadOnlyList<string>> PromptMultipleSelectionAsync(string mes
142142
Message = message,
143143
Choices = choiceDetails,
144144
DefaultChoices = defaultChoiceIndexes?.ToArray(),
145-
}).Returning<ShowChoicePromptResponse>(CancellationToken.None);
145+
}).Returning<ShowChoicePromptResponse>(CancellationToken.None).ConfigureAwait(false);
146146

147147
if (response.PromptCancelled)
148148
{
@@ -168,7 +168,7 @@ public async Task<string> PromptSelectionAsync(string message, IReadOnlyList<Pro
168168
Message = message,
169169
Choices = choiceDetails,
170170
DefaultChoices = defaultChoiceIndex > -1 ? new[] { defaultChoiceIndex } : null,
171-
}).Returning<ShowChoicePromptResponse>(CancellationToken.None);
171+
}).Returning<ShowChoicePromptResponse>(CancellationToken.None).ConfigureAwait(false);
172172

173173
if (response.PromptCancelled)
174174
{

src/PowerShellEditorServices/Services/DebugAdapter/BreakpointService.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public async Task<List<Breakpoint>> GetBreakpointsAsync()
4747
// Legacy behavior
4848
PSCommand psCommand = new PSCommand();
4949
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Get-PSBreakpoint");
50-
IEnumerable<Breakpoint> breakpoints = await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand);
50+
IEnumerable<Breakpoint> breakpoints = await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
5151
return breakpoints.ToList();
5252
}
5353

@@ -133,7 +133,7 @@ public async Task<IEnumerable<BreakpointDetails>> SetBreakpointsAsync(string esc
133133
if (psCommand != null)
134134
{
135135
IEnumerable<Breakpoint> setBreakpoints =
136-
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand);
136+
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
137137
configuredBreakpoints.AddRange(
138138
setBreakpoints.Select(BreakpointDetails.Create));
139139
}
@@ -210,7 +210,7 @@ public async Task<IEnumerable<CommandBreakpointDetails>> SetCommandBreakpoints(I
210210
if (psCommand != null)
211211
{
212212
IEnumerable<Breakpoint> setBreakpoints =
213-
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand);
213+
await _powerShellContextService.ExecuteCommandAsync<Breakpoint>(psCommand).ConfigureAwait(false);
214214
configuredBreakpoints.AddRange(
215215
setBreakpoints.Select(CommandBreakpointDetails.Create));
216216
}
@@ -301,7 +301,7 @@ public async Task RemoveBreakpointsAsync(IEnumerable<Breakpoint> breakpoints)
301301
psCommand.AddCommand(@"Microsoft.PowerShell.Utility\Remove-PSBreakpoint");
302302
psCommand.AddParameter("Id", breakpoints.Select(b => b.Id).ToArray());
303303

304-
await _powerShellContextService.ExecuteCommandAsync<object>(psCommand);
304+
await _powerShellContextService.ExecuteCommandAsync<object>(psCommand).ConfigureAwait(false);
305305
}
306306
}
307307

src/PowerShellEditorServices/Services/DebugAdapter/DebugService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public async Task<BreakpointDetails[]> SetLineBreakpointsAsync(
190190
return await dscBreakpoints.SetLineBreakpointsAsync(
191191
this.powerShellContext,
192192
escapedScriptPath,
193-
breakpoints);
193+
breakpoints).ConfigureAwait(false);
194194
}
195195

196196
/// <summary>
@@ -208,7 +208,9 @@ public async Task<CommandBreakpointDetails[]> SetCommandBreakpointsAsync(
208208
if (clearExisting)
209209
{
210210
// Flatten dictionary values into one list and remove them all.
211-
await _breakpointService.RemoveBreakpointsAsync((await _breakpointService.GetBreakpointsAsync()).Where( i => i is CommandBreakpoint)).ConfigureAwait(false);
211+
await _breakpointService.RemoveBreakpointsAsync(
212+
(await _breakpointService.GetBreakpointsAsync().ConfigureAwait(false))
213+
.Where( i => i is CommandBreakpoint)).ConfigureAwait(false);
212214
}
213215

214216
if (breakpoints.Length > 0)

src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
264264

265265
await _powerShellContextService.ExecuteScriptStringAsync(
266266
$"Enter-PSHostProcess -Id {processId}",
267-
errorMessages);
267+
errorMessages).ConfigureAwait(false);
268268

269269
if (errorMessages.Length > 0)
270270
{
@@ -280,7 +280,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
280280

281281
await _powerShellContextService.ExecuteScriptStringAsync(
282282
$"Enter-PSHostProcess -CustomPipeName {request.CustomPipeName}",
283-
errorMessages);
283+
errorMessages).ConfigureAwait(false);
284284

285285
if (errorMessages.Length > 0)
286286
{
@@ -307,7 +307,7 @@ await _powerShellContextService.ExecuteScriptStringAsync(
307307
.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace")
308308
.AddParameter("Name", request.RunspaceName)
309309
.AddCommand("Microsoft.PowerShell.Utility\\Select-Object")
310-
.AddParameter("ExpandProperty", "Id"));
310+
.AddParameter("ExpandProperty", "Id")).ConfigureAwait(false);
311311
foreach (var id in ids)
312312
{
313313
_debugStateService.RunspaceId = id;
@@ -396,12 +396,12 @@ private async Task OnExecutionCompletedAsync(Task executeTask)
396396
{
397397
try
398398
{
399-
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSHostProcess");
399+
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSHostProcess").ConfigureAwait(false);
400400

401401
if (_debugStateService.IsRemoteAttach &&
402402
_powerShellContextService.CurrentRunspace.Location == RunspaceLocation.Remote)
403403
{
404-
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSSession");
404+
await _powerShellContextService.ExecuteScriptStringAsync("Exit-PSSession").ConfigureAwait(false);
405405
}
406406
}
407407
catch (Exception e)

src/PowerShellEditorServices/Services/PowerShellContext/Handlers/GetVersionHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private enum PowerShellProcessArchitecture
7777
private async Task CheckPackageManagement()
7878
{
7979
PSCommand getModule = new PSCommand().AddCommand("Get-Module").AddParameter("ListAvailable").AddParameter("Name", "PackageManagement");
80-
foreach (PSModuleInfo module in await _powerShellContextService.ExecuteCommandAsync<PSModuleInfo>(getModule))
80+
foreach (PSModuleInfo module in await _powerShellContextService.ExecuteCommandAsync<PSModuleInfo>(getModule).ConfigureAwait(false))
8181
{
8282
// The user has a good enough version of PackageManagement
8383
if (module.Version >= s_desiredPackageManagementVersion)
@@ -109,7 +109,7 @@ private async Task CheckPackageManagement()
109109
Title = "Not now"
110110
}
111111
}
112-
});
112+
}).ConfigureAwait(false);
113113

114114
// If the user chose "Not now" ignore it for the rest of the session.
115115
if (messageAction?.Title == takeActionText)

test/PowerShellEditorServices.Test.E2E/DebugAdapterProtocolMessageTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public async Task InitializeAsync()
4141
{
4242
var factory = new LoggerFactory();
4343
_psesProcess = new PsesStdioProcess(factory, true);
44-
await _psesProcess.Start();
44+
await _psesProcess.Start().ConfigureAwait(false);
4545

4646
var initialized = new TaskCompletionSource<bool>();
4747
PsesDebugAdapterClient = DebugAdapterClient.Create(options =>

test/PowerShellEditorServices.Test.E2E/LSPTestsFixures.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task InitializeAsync()
4343
{
4444
var factory = new LoggerFactory();
4545
_psesProcess = new PsesStdioProcess(factory, IsDebugAdapterTests);
46-
await _psesProcess.Start();
46+
await _psesProcess.Start().ConfigureAwait(false);
4747

4848
Diagnostics = new List<Diagnostic>();
4949
TelemetryEvents = new List<PsesTelemetryEvent>();

0 commit comments

Comments
 (0)