Pengwin icon indicating copy to clipboard operation
Pengwin copied to clipboard

Fix pengwin.exe to respect starting directory from Windows Terminal

Open Copilot opened this issue 3 months ago • 0 comments

The pengwin.exe launcher ignores the starting directory when invoked from Windows Terminal's "Open in Terminal" context menu, always launching in the user's home directory instead.

Changes

  • Added IsCurrentDirNotSystem32() helper: Detects whether the launcher was invoked from Explorer/Terminal vs. Start Menu by checking if CWD != System32
  • Modified launch logic: Pass useCurrentWorkingDirectory=true to WslLaunchInteractive() when not in System32
  • Error handling: API failures default to true (respect CWD) for fail-safe behavior

Implementation

bool IsCurrentDirNotSystem32()
{
    wchar_t system32Dir[MAX_PATH];
    const auto system32DirLength = GetSystemDirectoryW(system32Dir, MAX_PATH);
    if (system32DirLength == 0 || system32DirLength >= MAX_PATH)
    {
        return true;  // On error, respect current directory
    }

    wchar_t currentDir[MAX_PATH];
    const auto currentDirLength = GetCurrentDirectoryW(MAX_PATH, currentDir);
    if (currentDirLength == 0 || currentDirLength >= MAX_PATH)
    {
        return true;
    }

    return _wcsicmp(system32Dir, currentDir) != 0;
}

// In wmain():
const bool useCurrentWorkingDirectory = IsCurrentDirNotSystem32();
hr = g_wslApi.WslLaunchInteractive(L"", useCurrentWorkingDirectory, &exitCode);

Based on the proven fix from Pengwin Enterprise.

Original prompt

This section details on the original issue you should resolve

<issue_title>Windows Terminal "Open in Terminal" explorer.exe right-click context menu not working properly</issue_title> <issue_description>In Windows Terminal settings when the Command line setting it set to: %ProgramFiles%\WindowsApps\WhitewaterFoundryLtd.Co.16571368D6CFF_22.3.2.0_x64__kd1vv0z0vy70w\DistroLauncher\pengwin.exe the explorer.exe right-click context menu item to "Open in Terminal" doesn't work, as "does not respect the starting directory".

To get around this I have changed Command line: to wsl -d WLinux and Starting directory to ~.

Apparently this problem also occurs in WSL Ubuntu.

Ah, this is the same root cause as the Ubuntu startingDirectory bug. Pengwin has included a JSON file that overrides the commandline to point to pengwin.exe, but pengwin.exe does not respect the starting directory. Any executable that ignores its working directory will exhibit this behavior.

https://github.com/microsoft/terminal/issues/13952?notification_referrer_id=NT_kwDOAAxwz7E0MzYzOTU2MDczOjgxNTMxMQ#issuecomment-1241393694

I don't yet know if there are any problems caused by this workaround. I'll report them here if I find any. </issue_description>

<agent_instructions>this issue was already fixed in pengwin enterprise. basically you need to modify distrolauncher (pengwin.exe) to be aware of the current directory. copy the implementation from this project https://github.com/WhitewaterFoundry/Pengwin-Enterprise</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@crramirez Probably because we needed to put a Starting Directory in the Json fragment as a workaround for an issue that Windows Terminal had.

Another workaround would be installing the Open with Pengwin option in pengwin-setup.

Thanks for reporting, we will figure out how to fix it.

Regards, Carlos </comment_new>

  • Fixes WhitewaterFoundry/Pengwin#752

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Nov 21 '25 21:11 Copilot