Skip to content

Commit 7e27e63

Browse files
authored
Handle global tool specially when prepending PSHome to PATH (#24228)
1 parent 4a47d35 commit 7e27e63

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,23 @@ internal static int Start(
123123
throw new ConsoleHostStartupException(ConsoleHostStrings.ShellCannotBeStartedWithConfigConflict);
124124
}
125125

126-
// put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version
126+
// Put PSHOME in front of PATH so that calling `pwsh` within `pwsh` always starts the same running version.
127127
string path = Environment.GetEnvironmentVariable("PATH");
128-
string pshome = Utils.DefaultPowerShellAppBase + Path.PathSeparator;
128+
string pshome = Utils.DefaultPowerShellAppBase;
129+
string dotnetToolsPathSegment = $"{Path.DirectorySeparatorChar}.store{Path.DirectorySeparatorChar}powershell{Path.DirectorySeparatorChar}";
129130

130-
// to not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front
131+
int index = pshome.IndexOf(dotnetToolsPathSegment, StringComparison.Ordinal);
132+
if (index > 0)
133+
{
134+
// We're running PowerShell global tool. In this case the real entry executable should be the 'pwsh'
135+
// or 'pwsh.exe' within the tool folder which should be the path right before the '\.store', not what
136+
// PSHome is pointing to.
137+
pshome = pshome[0..index];
138+
}
139+
140+
pshome += Path.PathSeparator;
141+
142+
// To not impact startup perf, we don't remove duplicates, but we avoid adding a duplicate to the front
131143
// we also don't handle the edge case where PATH only contains $PSHOME
132144
if (string.IsNullOrEmpty(path))
133145
{

0 commit comments

Comments
 (0)