From 17bd3716b4470bb70b74497774940f5ee42b6437 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 11 Aug 2025 14:08:39 -0700 Subject: [PATCH] Remove hardcoded `--cd ~` from WSL terminal fragment profiles to support Terminal's startingDirectory (#13350) * Initial plan * Fix WSL terminal fragment profiles to support Terminal's startingDirectory Co-authored-by: benhillis <17727402+benhillis@users.noreply.github.com> * clang format --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: benhillis <17727402+benhillis@users.noreply.github.com> Co-authored-by: Ben Hillis --- src/windows/service/exe/LxssUserSession.cpp | 11 +++++++++-- test/windows/UnitTests.cpp | 5 ++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/windows/service/exe/LxssUserSession.cpp b/src/windows/service/exe/LxssUserSession.cpp index 4b1c247..f8b066a 100644 --- a/src/windows/service/exe/LxssUserSession.cpp +++ b/src/windows/service/exe/LxssUserSession.cpp @@ -2678,13 +2678,20 @@ try std::wstring systemDirectory; THROW_IF_FAILED(wil::GetSystemDirectory(systemDirectory)); - e["commandline"] = WideToMultiByte(std::format( - L"{}\\{} {} {} {} {}", systemDirectory, WSL_BINARY_NAME, WSL_DISTRIBUTION_ID_ARG, distributionIdString, WSL_CHANGE_DIRECTORY_ARG, WSL_CWD_HOME)); + e["commandline"] = + WideToMultiByte(std::format(L"{}\\{} {} {}", systemDirectory, WSL_BINARY_NAME, WSL_DISTRIBUTION_ID_ARG, distributionIdString)); e["name"] = WideToMultiByte(Configuration.Name); e["guid"] = WideToMultiByte(distributionProfileId); e["icon"] = WideToMultiByte(IconPath.native()); + // Set default starting directory to home directory if not already specified + // This allows Windows Terminal to override with startingDirectory setting + if (e.find("startingDirectory") == e.end()) + { + e["startingDirectory"] = "~"; + } + // See https://github.com/microsoft/terminal/pull/18195. Supported in terminal >= 1.23 e["pathTranslationStyle"] = "wsl"; diff --git a/test/windows/UnitTests.cpp b/test/windows/UnitTests.cpp index aabb72a..d9ffe67 100644 --- a/test/windows/UnitTests.cpp +++ b/test/windows/UnitTests.cpp @@ -2539,9 +2539,12 @@ Error code: Wsl/InstallDistro/WSL_E_DISTRO_NOT_FOUND wil::GetSystemDirectoryW(systemDir); VERIFY_ARE_EQUAL( - std::format("{}\\{} {} {} {} {}", systemDir, WSL_BINARY_NAME, WSL_DISTRIBUTION_ID_ARG, distroIdString, WSL_CHANGE_DIRECTORY_ARG, WSL_CWD_HOME), + std::format("{}\\{} {} {}", systemDir, WSL_BINARY_NAME, WSL_DISTRIBUTION_ID_ARG, distroIdString), launchProfile["commandline"].get()); + // Verify that startingDirectory is set to home directory + VERIFY_ARE_EQUAL(launchProfile["startingDirectory"].get(), "~"); + auto iconLocation = wsl::shared::string::MultiByteToWide(launchProfile["icon"].get()); if (defaultIcon) {