Skip to content

Commit

Permalink
Azure: fall back to powershell when no preferred shell is set (#8197)
Browse files Browse the repository at this point in the history
I am still not sure what is the full set of scenarios that the problem
might occur, but for me it occurred for an "old" cloud shell account,
and didn't reproduce since I have reconfigured it. These behavior might
be explained by the fact that "preferred shell type" did not exist in
the API originally and thus was not set. In such case, Terminal
succeeds to retrieve to the settings but then crashes when reading the
missing field.  To fix it, I handle the case where the field is missing
and fallback to PowerShell.

## Validation Steps Performed
* Tested manually, only once.

Closes #7056
  • Loading branch information
Don-Vito authored Nov 9, 2020
1 parent e9a7b24 commit 0c0830b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/cascadia/TerminalConnection/AzureConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,25 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
_state = AzureState::TermConnecting;
}

// Method description:
// - Helper function to parse the preferred shell type from user settings returned by cloud console API.
// We need this function because the field might be missing in the settings
// created with old versions of cloud console API.
std::optional<utility::string_t> AzureConnection::_ParsePreferredShellType(const web::json::value& settingsResponse)
{
if (settingsResponse.has_object_field(L"properties"))
{
const auto userSettings = settingsResponse.at(L"properties");
if (userSettings.has_string_field(L"preferredShellType"))
{
const auto preferredShellTypeValue = userSettings.at(L"preferredShellType");
return preferredShellTypeValue.as_string();
}
}

return std::nullopt;
}

// Method description:
// - helper function to connect the user to the Azure cloud shell
void AzureConnection::_RunConnectState()
Expand All @@ -706,9 +725,9 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
_WriteStringWithNewline(RS_(L"AzureSuccess"));

// Request for a terminal for said cloud shell
const auto shellType = settingsResponse.at(L"properties").at(L"preferredShellType").as_string();
const auto shellType = _ParsePreferredShellType(settingsResponse);
_WriteStringWithNewline(RS_(L"AzureRequestingTerminal"));
const auto socketUri = _GetTerminal(shellType);
const auto socketUri = _GetTerminal(shellType.value_or(L"pwsh"));
_TerminalOutputHandlers(L"\r\n");

// Step 8: connecting to said terminal
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalConnection/AzureConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
std::optional<std::wstring> _ReadUserInput(InputMode mode);

web::websockets::client::websocket_client _cloudShellSocket;

static std::optional<utility::string_t> _ParsePreferredShellType(const web::json::value& settingsResponse);
};
}

Expand Down

0 comments on commit 0c0830b

Please sign in to comment.