diff --git a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Commands.cs b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Commands.cs index 3a07bdd5a02..25587647a35 100644 --- a/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Commands.cs +++ b/src/Razor/test/Microsoft.VisualStudio.Razor.IntegrationTests/InProcess/EditorInProcess_Commands.cs @@ -85,12 +85,17 @@ private async Task ExecuteCommandAsync(Guid commandGuid, uint commandId, Cancell var dispatcher = await TestServices.Shell.GetRequiredGlobalServiceAsync(cancellationToken); + // Set up the command structure to query the command's status var cmds = new OLECMD[1]; cmds[0].cmdID = commandId; cmds[0].cmdf = 0; + // Wait for the command to become enabled and supported before executing it. + // This prevents COM exceptions that can occur when trying to execute commands + // before they're fully initialized (e.g., during language server startup). await Helper.RetryAsync(ct => { + // Query whether the command is currently available ErrorHandler.ThrowOnFailure(dispatcher.QueryStatus(ref commandGuid, 1, cmds, IntPtr.Zero)); var status = (OLECMDF)cmds[0].cmdf; @@ -103,6 +108,7 @@ await Helper.RetryAsync(ct => return SpecializedTasks.False; }, TimeSpan.FromMilliseconds(100), cancellationToken); + // Execute the command now that we've confirmed it's ready ErrorHandler.ThrowOnFailure(dispatcher.Exec(commandGuid, commandId, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, IntPtr.Zero, IntPtr.Zero)); }