Skip to content

Commit bb62ce9

Browse files
authored
Improve VS profile generation (#19025)
* Make PowerShell profile generation try to find `pwsh.exe` before falling back to legacy powershell * Make profiles generated on an `arm64` host work properly ## Validation Steps Performed * Local build ran * Verified the new `arm64` profile works * Verified `pwsh.exe` is used if present * Verified `powershell.exe` is used if `pwsh` is not present in path * Verified we don't attempt to create `arm64` host cmd/pwsh profiles if VS is not >= 17.4
1 parent 685499d commit bb62ce9

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

src/cascadia/TerminalSettingsModel/VsDevCmdGenerator.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ std::wstring VsDevCmdGenerator::GetProfileCommandLine(const VsSetupConfiguration
4545
// The "-startdir" parameter will prevent "vsdevcmd" from automatically
4646
// setting the shell path so the path in the profile will be used instead.
4747
#if defined(_M_ARM64)
48-
commandLine.append(LR"(" -startdir=none -arch=arm64 -host_arch=x64)");
48+
commandLine.append(LR"(" -startdir=none -arch=arm64 -host_arch=)");
49+
if (instance.VersionInRange(L"[17.4,"))
50+
{
51+
commandLine.append(LR"(arm64)");
52+
}
53+
else
54+
{
55+
commandLine.append(LR"(x64)");
56+
}
4957
#elif defined(_M_AMD64)
5058
commandLine.append(LR"(" -startdir=none -arch=x64 -host_arch=x64)");
5159
#else

src/cascadia/TerminalSettingsModel/VsDevShellGenerator.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,43 @@ std::wstring VsDevShellGenerator::GetProfileName(const VsSetupConfiguration::VsS
3939

4040
std::wstring VsDevShellGenerator::GetProfileCommandLine(const VsSetupConfiguration::VsSetupInstance& instance) const
4141
{
42-
// The triple-quotes are a PowerShell path escape sequence that can safely be stored in a JSON object.
43-
// The "SkipAutomaticLocation" parameter will prevent "Enter-VsDevShell" from automatically setting the shell path
44-
// so the path in the profile will be used instead.
42+
// Build this in stages, so reserve space now
4543
std::wstring commandLine;
4644
commandLine.reserve(256);
47-
commandLine.append(LR"(powershell.exe -NoExit -Command "&{Import-Module """)");
45+
46+
// Try to detect if `pwsh.exe` is available in the PATH, if so we want to use that
47+
// Allow some extra space in case user put it somewhere odd
48+
// We do need to allocate space for the full path even if we don't want to paste the whole thing in
49+
wchar_t pwshPath[MAX_PATH] = { 0 };
50+
const auto pwshExeName = L"pwsh.exe";
51+
if (SearchPathW(nullptr, pwshExeName, nullptr, MAX_PATH, pwshPath, nullptr))
52+
{
53+
commandLine.append(pwshExeName);
54+
}
55+
else
56+
{
57+
commandLine.append(L"powershell.exe");
58+
}
59+
60+
// The triple-quotes are a PowerShell path escape sequence that can safely be stored in a JSON object.
61+
// The "SkipAutomaticLocation" parameter will prevent "Enter-VsDevShell" from automatically setting the shell path
62+
// so the path in the profile will be used instead
63+
commandLine.append(LR"( -NoExit -Command "&{Import-Module """)");
4864
commandLine.append(GetDevShellModulePath(instance));
4965
commandLine.append(LR"("""; Enter-VsDevShell )");
5066
commandLine.append(instance.GetInstanceId());
5167
#if defined(_M_ARM64)
52-
commandLine.append(LR"( -SkipAutomaticLocation -DevCmdArguments """-arch=arm64 -host_arch=x64"""}")");
68+
// This part stays constant no matter what
69+
commandLine.append(LR"( -SkipAutomaticLocation -DevCmdArguments """-arch=arm64 -host_arch=)");
70+
if (instance.VersionInRange(L"[17.4,"))
71+
{
72+
commandLine.append(LR"("arm64 """}")");
73+
}
74+
// If an old version of VS is installed without ARM64 host support
75+
else
76+
{
77+
commandLine.append(LR"("x64 """}")");
78+
}
5379
#elif defined(_M_AMD64)
5480
commandLine.append(LR"( -SkipAutomaticLocation -DevCmdArguments """-arch=x64 -host_arch=x64"""}")");
5581
#else

0 commit comments

Comments
 (0)