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

Code before:
            if (!cwd.empty())
            {
            // ignore errors, we'll just power on through. We'd rather do
            // something rather than fail silently if the directory doesn't
            // actually exist.
            _WindowProperties.VirtualWorkingDirectory(originalVirtualCwd);
            _WindowProperties.VirtualEnvVars(originalVirtualEnv);
            }
        });
            _WindowProperties.VirtualWorkingDirectory(cwd);
        _WindowProperties.VirtualEnvVars(env);

        for (size_t i = 0; i < actions.size(); ++i)
        {
            if (i != 0)
            {
                // Each action may rely on the XAML layout of a preceding action.
                // Most importantly, this is the case for the combination of NewTab + SplitPane,
                // as the former appears to only have a layout size after at least 1 resume_foreground,
                // while the latter relies on that information. This is also why it uses Low priority.
                //
                // Curiously, this does not seem to be required when using startupActions, but only when
                // tearing out a tab (this currently creates a new window with injected startup actions).
                // This indicates that this is really more of an architectural issue and not a fundamental one.
                co_await wil::resume_foreground(Dispatcher(), CoreDispatcherPriority::Low);
            }

            _actionDispatch->DoAction(actions[i]);
            }

            // GH#6586: now that we're done processing all startup commands,
            // focus the active control. This will work as expected for both
            // commandline invocations and for `wt` action invocations.
            if (const auto& terminalTab{ _GetFocusedTabImpl() })
            {
                if (const auto& content{ terminalTab->GetActiveContent() })
                {
                    content.Focus(FocusState::Programmatic);
                }
            }

        if (initial)
        {
            _CompleteInitialization();
        }
    }

    // Method Description:
    // - Perform and steps that need to be done once our initial state is all
    //   set up. This includes entering fullscreen mode and firing our
    //   Initialized event.

Review comment (suggestion):
Subjectively speaking, I found this highly confusing. Rewrote it to be a literal translation of the comment above.
(= "If the caller provided a CWD, [...] switch back once we're done" = `if (!cwd.empty())`)

Code after:
            if (!cwd.empty())
            {
                // ignore errors, we'll just power on through. We'd rather do
                // something rather than fail silently if the directory doesn't
                // actually exist.
                _WindowProperties.VirtualWorkingDirectory(originalVirtualCwd);
                _WindowProperties.VirtualEnvVars(originalVirtualEnv);
            }
        });
        _WindowProperties.VirtualWorkingDirectory(cwd);
        _WindowProperties.VirtualEnvVars(env);

        for (size_t i = 0; i < actions.size(); ++i)
        {
            if (i != 0)
            {
                // Each action may rely on the XAML layout of a preceding action.
                // Most importantly, this is the case for the combination of NewTab + SplitPane,
                // as the former appears to only have a layout size after at least 1 resume_foreground,
                // while the latter relies on that information. This is also why it uses Low priority.
                //
                // Curiously, this does not seem to be required when using startupActions, but only when
                // tearing out a tab (this currently creates a new window with injected startup actions).
                // This indicates that this is really more of an architectural issue and not a fundamental one.
                co_await wil::resume_foreground(Dispatcher(), CoreDispatcherPriority::Low);
            }

            _actionDispatch->DoAction(actions[i]);
        }

        // GH#6586: now that we're done processing all startup commands,
        // focus the active control. This will work as expected for both
        // commandline invocations and for `wt` action invocations.
        if (const auto& terminalTab{ _GetFocusedTabImpl() })
        {
            if (const auto& content{ terminalTab->GetActiveContent() })
            {
                content.Focus(FocusState::Programmatic);
            }
        }

        if (initial)
        {
            _CompleteInitialization();
        }
    }

    // Method Description:
    // - Perform and steps that need to be done once our initial state is all
    //   set up. This includes entering fullscreen mode and firing our
    //   Initialized event.