Repository: microsoft/terminal
File: src/cascadia/WindowsTerminal/WindowEmperor.cpp (C++)

Code before:

        // Get the argv, and turn them into a hstring array to pass to the app.
        wil::unique_any<LPWSTR*, decltype(&::LocalFree), ::LocalFree> argv{ CommandLineToArgvW(commandline, &argc) };
        if (argv)
        {
            for (auto& elem : wil::make_range(argv.get(), argc))
            {
                args.emplace_back(elem);
            }
        }
    }
    if (args.empty())
    {
        args.emplace_back(L"wt.exe");
    }
}

bool WindowEmperor::HandleCommandlineArgs()
{
    std::vector<winrt::hstring> args;
    _buildArgsFromCommandline(args);
    const auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };

    // ALWAYS change the _real_ CWD of the Terminal to system32, so that we
    // don't lock the directory we were spawned in.
    LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectory(L"%SystemRoot%\\System32"));

    // Get the requested initial state of the window from our startup info. For
    // something like `start /min`, this will set the wShowWindow member to
    // SW_SHOWMINIMIZED. We'll need to make sure is bubbled all the way through,
    // so we can open a new window with the same state.
    STARTUPINFOW si;
    GetStartupInfoW(&si);
    const uint32_t showWindow = WI_IsFlagSet(si.dwFlags, STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOW;

    Remoting::CommandlineArgs eventArgs{ { args }, { cwd }, showWindow };

    const auto isolatedMode{ _app.Logic().IsolatedMode() };

    const auto result = _manager.ProposeCommandline(eventArgs, isolatedMode);

    if (result.ShouldCreateWindow())
    {
        _createNewWindowThread(Remoting::WindowRequestedArgs{ result, eventArgs });

        _becomeMonarch();
    }
    else
    {
        const auto res = _app.Logic().GetParseCommandlineMessage(eventArgs.Commandline());
        if (!res.Message.empty())

Review comment (suggestion):
* `SetCurrentDirectory` indeed doesn't expand env variables. You can just call `GetSystemDirectoryW` with a `MAX_PATH` sized buffer though. I would wrap the declaration and execution of the functions inside a scope or a function call though so that the lifetime of the buffer is limited. (FYI you can use `SUCCEEDED_LOG` to check the status code _and_ log errors.)
* Why call this only in `HandleCommandlineArgs()`? Shouldn't we call this as early as possible, right inside the main function? After all, the cwd should be unrelated to a "window emperor". 🤔
* We should probably also use `SetCurrentDirectoryW` explicitly, because the macros can be fairly annoying (and the string is not declared as a TCHAR anyways).

Code after:

        // Get the argv, and turn them into a hstring array to pass to the app.
        wil::unique_any<LPWSTR*, decltype(&::LocalFree), ::LocalFree> argv{ CommandLineToArgvW(commandline, &argc) };
        if (argv)
        {
            for (auto& elem : wil::make_range(argv.get(), argc))
            {
                args.emplace_back(elem);
            }
        }
    }
    if (args.empty())
    {
        args.emplace_back(L"wt.exe");
    }
}

bool WindowEmperor::HandleCommandlineArgs()
{
    std::vector<winrt::hstring> args;
    _buildArgsFromCommandline(args);
    const auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };

    {
        // ALWAYS change the _real_ CWD of the Terminal to system32, so that we
        // don't lock the directory we were spawned in.
        std::wstring system32{};
        if (SUCCEEDED_LOG(wil::GetSystemDirectoryW<std::wstring>(system32)))
        {
            LOG_IF_WIN32_BOOL_FALSE(SetCurrentDirectoryW(system32.c_str()));
        }
    }

    // Get the requested initial state of the window from our startup info. For
    // something like `start /min`, this will set the wShowWindow member to
    // SW_SHOWMINIMIZED. We'll need to make sure is bubbled all the way through,
    // so we can open a new window with the same state.
    STARTUPINFOW si;
    GetStartupInfoW(&si);
    const uint32_t showWindow = WI_IsFlagSet(si.dwFlags, STARTF_USESHOWWINDOW) ? si.wShowWindow : SW_SHOW;

    Remoting::CommandlineArgs eventArgs{ { args }, { cwd }, showWindow };

    const auto isolatedMode{ _app.Logic().IsolatedMode() };

    const auto result = _manager.ProposeCommandline(eventArgs, isolatedMode);

    if (result.ShouldCreateWindow())
    {
        _createNewWindowThread(Remoting::WindowRequestedArgs{ result, eventArgs });