Skip to content

Commit

Permalink
this might fix #15487. Dunno how I feel about it
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed May 31, 2023
1 parent c9e993a commit 2e069b2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1253,9 +1253,10 @@ namespace winrt::TerminalApp::implementation
winrt::guid(),
profile.Guid());

valueSet.Insert(L"passthroughMode", Windows::Foundation::PropertyValue::CreateBoolean(settings.VtPassthrough()));
valueSet.Insert(L"passthroughMode", Windows::Foundation::PropertyValue::CreateString(_WindowProperties.VirtualWorkingDirectory()));
valueSet.Insert(L"reloadEnvironmentVariables",
Windows::Foundation::PropertyValue::CreateBoolean(_settings.GlobalSettings().ReloadEnvironmentVariables()));
valueSet.Insert(L"terminalCwd", Windows::Foundation::PropertyValue::CreateString(_WindowProperties.VirtualWorkingDirectory()));

if (inheritCursor)
{
Expand Down
39 changes: 37 additions & 2 deletions src/cascadia/TerminalConnection/ConptyConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
auto [newCommandLine, newStartingDirectory] = Utils::MangleStartingDirectoryForWSL(cmdline, _startingDirectory);
const auto startingDirectory = newStartingDirectory.size() > 0 ? newStartingDirectory.c_str() : nullptr;

RETURN_IF_WIN32_BOOL_FALSE(CreateProcessW(
const auto succeeded = CreateProcessW(
nullptr,
newCommandLine.data(),
nullptr, // lpProcessAttributes
Expand All @@ -181,7 +181,41 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
startingDirectory,
&siEx.StartupInfo, // lpStartupInfo
&_piClient // lpProcessInformation
));
);
if (!succeeded)
{
const auto gle = GetLastError();
if (gle == ERROR_FILE_NOT_FOUND)
{
std::wstring system32{};
if (SUCCEEDED(wil::GetSystemDirectoryW<std::wstring>(system32)))
{
// try again with the terminal's CWD changed
if (SetCurrentDirectoryW(_terminalCwd.c_str()))
{
auto pop = wil::scope_exit([&]() {
LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectoryW(system32.c_str()));
});
RETURN_IF_WIN32_BOOL_FALSE(CreateProcessW(
nullptr,
newCommandLine.data(),
nullptr, // lpProcessAttributes
nullptr, // lpThreadAttributes
false, // bInheritHandles
EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT, // dwCreationFlags
lpEnvironment, // lpEnvironment
startingDirectory,
&siEx.StartupInfo, // lpStartupInfo
&_piClient // lpProcessInformation
));
}
}
}
else
{
return HRESULT_FROM_WIN32(gle);
}
}

DeleteProcThreadAttributeList(siEx.lpAttributeList);

Expand Down Expand Up @@ -286,6 +320,7 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
_reloadEnvironmentVariables = winrt::unbox_value_or<bool>(settings.TryLookup(L"reloadEnvironmentVariables").try_as<Windows::Foundation::IPropertyValue>(),
_reloadEnvironmentVariables);
_profileGuid = winrt::unbox_value_or<winrt::guid>(settings.TryLookup(L"profileGuid").try_as<Windows::Foundation::IPropertyValue>(), _profileGuid);
_terminalCwd = winrt::unbox_value_or<winrt::hstring>(settings.TryLookup(L"terminalCwd").try_as<Windows::Foundation::IPropertyValue>(), _terminalCwd);
}

if (_guid == guid{})
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalConnection/ConptyConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
guid _guid{}; // A unique session identifier for connected client
hstring _clientName{}; // The name of the process hosted by this ConPTY connection (as of launch).

hstring _terminalCwd{};

bool _receivedFirstByte{ false };
std::chrono::high_resolution_clock::time_point _startTime{};

Expand Down

0 comments on commit 2e069b2

Please sign in to comment.