Skip to content

After updating Windows Terminal, pressing Ctrl+Profile will not inherit the working directory #15173

Closed

Description

@TGSAN

Windows Terminal version

1.16.10262.0

Windows build number

10.0.22621.1537

Other Software

No response

Steps to reproduce

  1. Set the startup directory in Windows Terminal settings to ".".
  2. Go to a folder in File Explorer (for example: D:\SomeDir).
  3. Right-click and select "Open with Terminal" from the context menu.
  4. Hold down the Ctrl key on the title bar of the new Windows Terminal and create any tab (to create as an administrator).`

Expected Behavior

In the new Windows Terminal that runs as an administrator, the working directory is inherited as D:\SomeDir

Actual Behavior

In the new Windows Terminal that runs as an administrator, the working directory is reset to C:\Windows\System32

Activity

  1. added
    Issue-BugIt either shouldn't be doing this or needs an investigation.
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Apr 13, 2023
  2. TGSAN commented on Apr 13, 2023

    @TGSAN
    Author

    In the first stable version that supports creating an administrator profile window by holding down Ctrl (Version 1.15.2525.0), its behavior was as expected. After Windows Store updated Windows Terminal recently, I found that its behavior was broken.
    Currently I can only solve this behavior change by using the powershell -Command Start-Process wt -Verb runAs command, but it is very inconvenient.

  3. TGSAN commented on Apr 13, 2023

    @TGSAN
    Author

    I just tested every stable version and found that 1.15.3465.0 and 1.15.3466.0 are the last stable versions that can inherit the working directory, while 1.16.10261.0 and 1.16.10262.0 are the first stable versions that will not inherit the working directory.

  4. DHowett commented on Apr 13, 2023

    @DHowett
    Member

    Mike Griese (@zadjii-msft) hm. ShellExecute("appsfolder:...") fallout?

  5. zadjii-msft commented on Apr 18, 2023

    @zadjii-msft
    Member

    Mike Griese (@zadjii-msft) hm. ShellExecute("appsfolder:...") fallout?

    probably

  6. added and removed
    Needs-TriageIt's a new issue that the core contributor team needs to triage at the next triage meeting
    on Apr 18, 2023
  7. kovdu commented on Apr 26, 2023

    @kovdu
    Contributor

    Also noticed this when looking into #14817 and in my case I was not able to set the working directory for ShellExecute. Somehow it was never talking into account and googling around didn't return anything useful.

    1.15.2525.0 is just doing CreateProcessW here https://github.com/microsoft/terminal/blob/release-1.15/src/cascadia/ShellExtension/OpenTerminalHere.cpp#L47 but was not starting elevated then.

    So indeed moving to ShellExecute is causing this. But don't know what would the best way to set the working directory then tbh.

  8. zadjii-msft commented on May 1, 2023

    @zadjii-msft
    Member

    Hmm. I bet this especially breaks in 1.18. I wonder actually if "startingDirectory": "." will be really broken, because the Terminal's CWD will always be the CWD of the first terminal window.

    Butts.

  9. zadjii-msft commented on May 1, 2023

    @zadjii-msft
    Member

    Okay, I've got a possible solution, but I think I ended up building it on top of #5506, and that might be too much to pull in this late in the release.

    Basically, the simple idea is:

    • Manually pass the evaluated CWD to _OpenElevatedWT,
    • so that we launch elevate-shim new-tab -p {guid} -d <path>.
    • That'll then ShellExecute({ "shell:AppsFolder\...", "new-tab -p {guid} -d <path>"}),
    • and then the elevated window would treat that as "manually start this profile with the CWD <path>"

    I deeply worry that'll cause mysterious edge cases with like, c:\ or other string escaping nonsense.

  10. added a commit that references this issue on May 1, 2023
  11. zadjii-msft commented on May 1, 2023

    @zadjii-msft
    Member

    note:

    diff --git a/src/cascadia/ElevateShim/elevate-shim.cpp b/src/cascadia/ElevateShim/elevate-shim.cpp
    index 50449ddca..ee90aec45 100644
    --- a/src/cascadia/ElevateShim/elevate-shim.cpp
    +++ b/src/cascadia/ElevateShim/elevate-shim.cpp
    @@ -13,6 +13,7 @@
     #include <gsl/pointers>
     #include <shellapi.h>
     #include <appmodel.h>
    +#include <wil/filesystem.h>^M
    
     // BODGY
     //
    @@ -96,12 +97,15 @@ int __stdcall wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
         SHELLEXECUTEINFOW seInfo{ 0 };
     #pragma warning(pop)
    
    +    auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };^M
    +^M
         seInfo.cbSize = sizeof(seInfo);
         seInfo.fMask = SEE_MASK_DEFAULT;
         seInfo.lpVerb = L"runas"; // This asks the shell to elevate the process
         seInfo.lpFile = cmd.c_str(); // This is `shell:AppsFolder\...` or `...\WindowsTerminal.exe`
         seInfo.lpParameters = GetCommandLine(); // This is `new-tab -p {guid}`
         seInfo.nShow = SW_SHOWNORMAL;
    +    seInfo.lpDirectory = cwd.c_str();^M
         LOG_IF_WIN32_BOOL_FALSE(ShellExecuteExW(&seInfo));
    
         return 0;
    diff --git a/src/cascadia/TerminalApp/TerminalPage.cpp b/src/cascadia/TerminalApp/TerminalPage.cpp
    index 99a1fdafd..00008642f 100644
    --- a/src/cascadia/TerminalApp/TerminalPage.cpp
    +++ b/src/cascadia/TerminalApp/TerminalPage.cpp
    @@ -4181,6 +4181,8 @@ namespace winrt::TerminalApp::implementation
             STARTUPINFOW si{};
             si.cb = sizeof(si);
    
    +        auto cwd{ wil::GetCurrentDirectoryW<std::wstring>() };
    +
             LOG_IF_WIN32_BOOL_FALSE(CreateProcessW(exePath.c_str(),
                                                    cmdline.data(),
                                                    nullptr,
    @@ -4188,7 +4190,7 @@ namespace winrt::TerminalApp::implementation
                                                    FALSE,
                                                    0,
                                                    nullptr,
    -                                               nullptr,
    +                                               cwd.c_str(),
                                                    &si,
                                                    &pi));
    

    did not just work

  12. added a commit that references this issue on May 4, 2023
  13. added a commit that references this issue on May 15, 2023
    9a4f4ab
  14. DHowett commented on May 15, 2023

    @DHowett
    Member

    Mike Griese (@zadjii-msft) you marked this up for 1.17 servicing but it has a hard dependency on the virtual CWD work. Do you think we should backport them as a pair, or would you be willing to retool the fix to be 1.17-specific?

  15. added a commit that references this issue on May 15, 2023
    6942992
  16. zadjii-msft commented on May 17, 2023

    @zadjii-msft
    Member

    Dustin L. Howett (@DHowett) Uh, maybe there's a way to retool. The vCWD work is definitely too scary to backport straight to Stable

  17. TGSAN commented on Jun 30, 2023

    @TGSAN
    Author

    In version 1.17 Preview, the working directory inheritance was restored, but in the recent update to version 1.18, it broke again. What happened? 🤔 Mike Griese (@zadjii-msft)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRThis issue has a related PRIssue-BugIt either shouldn't be doing this or needs an investigation.Needs-Tag-FixDoesn't match tag requirementsProduct-TerminalThe new Windows Terminal.Severity-BlockingWe won't ship a release like this! No-siree.

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions