Repository: microsoft/terminal
File: src/cascadia/TerminalApp/TerminalPage.cpp (C++)

Code before:
            const auto environment = settings.EnvironmentVariables() != nullptr ?
                                         settings.EnvironmentVariables().GetView() :
                                         nullptr;

            // Update the path to be relative to whatever our CWD is.
            //
            // Refer to the examples in
            // https://en.cppreference.com/w/cpp/filesystem/path/append
            //
            // We need to do this here, to ensure we tell the ConptyConnection
            // the correct starting path. If we're being invoked from another
            // terminal instance (e.g. wt -w 0 -d .), then we have switched our
            // CWD to the provided path. We should treat the StartingDirectory
            // as relative to the current CWD.
            //
            // The connection must be informed of the current CWD on
            // construction, because the connection might not spawn the child
            // process until later, on another thread, after we've already
            // restored the CWD to its original value.
            auto newWorkingDirectory{ settings.StartingDirectory() };
            const bool looksLikeLinux = newWorkingDirectory.size() == 1 &&
                                        (newWorkingDirectory[0] == L'~' || newWorkingDirectory[0] == L'/');
            // We only want to resolve the new WD against the CWD if it doesn't look like a Linux path (see GH#592)
            if (!looksLikeLinux)
            {
                const auto cwdString{ _WindowProperties.VirtualWorkingDirectory().c_str() };
                std::filesystem::path cwd{ cwdString };
                cwd /= settings.StartingDirectory().c_str();
                newWorkingDirectory = winrt::hstring{ cwd.wstring() };
            }

            auto conhostConn = TerminalConnection::ConptyConnection();
            auto valueSet = TerminalConnection::ConptyConnection::CreateSettings(settings.Commandline(),
                                                                                 newWorkingDirectory,
                                                                                 settings.StartingTitle(),
                                                                                 environment,
                                                                                 settings.InitialRows(),
                                                                                 settings.InitialCols(),
                                                                                 winrt::guid(),
                                                                                 profile.Guid());

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

            if (inheritCursor)
            {
                valueSet.Insert(L"inheritCursor", Windows::Foundation::PropertyValue::CreateBoolean(true));
            }

            conhostConn.Initialize(valueSet);

Review comment (suggestion):
This may result in an AV, because the return value of `VirtualWorkingDirectory()` is destroyed at the end of the line. First assign `VirtualWorkingDirectory()` to a variable and then pass it to the `cwd`.

`hstring` can be converted to `string_view` which `path` has a constructor for. So instead of using `c_str()`, just cast it to a `string_view`. It'd be nice if you could do the same to the `/=` line below. (BTW We have _a lot_ of `c_str()` calls in our code base. If you ever see one, it'd be awesome if you could swap them out with proper `string_view`s!)

Code after:
        {
            const auto environment = settings.EnvironmentVariables() != nullptr ?
                                         settings.EnvironmentVariables().GetView() :
                                         nullptr;

            // Update the path to be relative to whatever our CWD is.
            //
            // Refer to the examples in
            // https://en.cppreference.com/w/cpp/filesystem/path/append
            //
            // We need to do this here, to ensure we tell the ConptyConnection
            // the correct starting path. If we're being invoked from another
            // terminal instance (e.g. wt -w 0 -d .), then we have switched our
            // CWD to the provided path. We should treat the StartingDirectory
            // as relative to the current CWD.
            //
            // The connection must be informed of the current CWD on
            // construction, because the connection might not spawn the child
            // process until later, on another thread, after we've already
            // restored the CWD to its original value.
            const auto currentVirtualDir{ _WindowProperties.VirtualWorkingDirectory() };
            const auto cwdString{ std::wstring_view{ currentVirtualDir } };
            const auto requestedStartingDir{ settings.StartingDirectory() };
            auto newWorkingDirectory = winrt::hstring{
                Utils::EvaluateStartingDirectory(cwdString, std::wstring_view{ requestedStartingDir })
            };

            auto conhostConn = TerminalConnection::ConptyConnection();
            auto valueSet = TerminalConnection::ConptyConnection::CreateSettings(settings.Commandline(),
                                                                                 newWorkingDirectory,
                                                                                 settings.StartingTitle(),
                                                                                 environment,
                                                                                 settings.InitialRows(),
                                                                                 settings.InitialCols(),
                                                                                 winrt::guid(),
                                                                                 profile.Guid());

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

            if (inheritCursor)
            {
                valueSet.Insert(L"inheritCursor", Windows::Foundation::PropertyValue::CreateBoolean(true));
            }

            conhostConn.Initialize(valueSet);

            sessionGuid = conhostConn.Guid();
            connection = conhostConn;
        }