From 97e2791619ee2e6defe72d851b04d16235d8c709 Mon Sep 17 00:00:00 2001 From: SteveL-MSFT Date: Thu, 3 Aug 2017 10:52:13 -0700 Subject: [PATCH 1/2] ensure running `powershell` within PowerShell starts instance of currently running PowerShell modify PATH env var at startup so that $PSHOME is in front --- .../host/msh/ConsoleHost.cs | 12 ++++++++++++ test/powershell/Host/ConsoleHost.Tests.ps1 | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index c16bd481692..2f2c56be565 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -134,6 +134,18 @@ internal static int Start( } #endif + // put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version + StringBuilder path = new StringBuilder(Environment.GetEnvironmentVariable("PATH")); + string pathSeparator = ";"; + if (!Platform.IsWindows) + { + pathSeparator = ":"; + } + string pshome = Utils.DefaultPowerShellAppBase; + path.Replace(pathSeparator + pshome, ""); + path.Insert(0, pshome + pathSeparator); + Environment.SetEnvironmentVariable("PATH", path.ToString()); + try { string profileDir; diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 2c3b8ccaaaf..4bb8b8b0581 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -486,6 +486,12 @@ foo bash -c "unset HOME;$powershell -c '1+1'" | Should BeExactly 2 } } + + Context "PATH environment variable" { + It "`$PSHOME should be in front so that powershell.exe starts current running PowerShell" { + powershell -v | Should Match $psversiontable.GitCommitId + } + } } Describe "Console host api tests" -Tag CI { From e59aca1d4c2b5a43dac199bbb97e608d479bffc0 Mon Sep 17 00:00:00 2001 From: "Steve Lee [MSFT]" Date: Thu, 3 Aug 2017 17:07:31 -0700 Subject: [PATCH 2/2] address PR feedback --- .../host/msh/ConsoleHost.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 2f2c56be565..c2eb41a1f85 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -135,16 +135,12 @@ internal static int Start( #endif // put PSHOME in front of PATH so that calling `powershell` within `powershell` always starts the same running version - StringBuilder path = new StringBuilder(Environment.GetEnvironmentVariable("PATH")); - string pathSeparator = ";"; - if (!Platform.IsWindows) + string path = Environment.GetEnvironmentVariable("PATH"); + string pshome = Utils.DefaultPowerShellAppBase; + if (!path.Contains(pshome)) { - pathSeparator = ":"; + Environment.SetEnvironmentVariable("PATH", pshome + Path.PathSeparator + path); } - string pshome = Utils.DefaultPowerShellAppBase; - path.Replace(pathSeparator + pshome, ""); - path.Insert(0, pshome + pathSeparator); - Environment.SetEnvironmentVariable("PATH", path.ToString()); try {