From b1e2745b5315a9511ff0e0f2ecff42f3f8db3e3b Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 29 Oct 2018 13:56:13 -0700 Subject: [PATCH] move processing of `-WorkingDirectory` before processing of profiles (#8079) --- .../host/msh/ConsoleHost.cs | 50 +++++++++---------- test/powershell/Host/ConsoleHost.Tests.ps1 | 19 +++++++ 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs index 07f7dba601..091ccb76af 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs @@ -1614,6 +1614,31 @@ namespace Microsoft.PowerShell Executor exec = new Executor(this, false, false); + // If working directory was specified, set it + if (s_cpp != null && s_cpp.WorkingDirectory != null) + { + Pipeline tempPipeline = exec.CreatePipeline(); + var command = new Command("Set-Location"); + command.Parameters.Add("LiteralPath", s_cpp.WorkingDirectory); + tempPipeline.Commands.Add(command); + + Exception exception; + if (IsRunningAsync) + { + exec.ExecuteCommandAsyncHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); + } + else + { + exec.ExecuteCommandHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); + } + + if (exception != null) + { + _lastRunspaceInitializationException = exception; + ReportException(exception, exec); + } + } + if (!string.IsNullOrEmpty(configurationName)) { // If an endpoint configuration is specified then create a loop-back remote runspace targeting @@ -1694,31 +1719,6 @@ namespace Microsoft.PowerShell TelemetryAPI.ReportStartupTelemetry(this); #endif - // If working directory was specified, set it - if (s_cpp != null && s_cpp.WorkingDirectory != null) - { - Pipeline tempPipeline = exec.CreatePipeline(); - var command = new Command("Set-Location"); - command.Parameters.Add("LiteralPath", s_cpp.WorkingDirectory); - tempPipeline.Commands.Add(command); - - Exception exception; - if (IsRunningAsync) - { - exec.ExecuteCommandAsyncHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); - } - else - { - exec.ExecuteCommandHelper(tempPipeline, out exception, Executor.ExecutionOptions.AddOutputter); - } - - if (exception != null) - { - _lastRunspaceInitializationException = exception; - ReportException(exception, exec); - } - } - // If a file was specified as the argument to run, then run it... if (s_cpp != null && s_cpp.File != null) { diff --git a/test/powershell/Host/ConsoleHost.Tests.ps1 b/test/powershell/Host/ConsoleHost.Tests.ps1 index 7120283af1..4599f41612 100644 --- a/test/powershell/Host/ConsoleHost.Tests.ps1 +++ b/test/powershell/Host/ConsoleHost.Tests.ps1 @@ -567,6 +567,25 @@ foo $LASTEXITCODE | Should -Be $ExitCodeBadCommandLineParameter $output | Should -Not -BeNullOrEmpty } + + It "-WorkingDirectory should be processed before profiles" { + + $currentProfile = Get-Content $PROFILE + @" + (Get-Location).Path + Set-Location $testdrive +"@ > $PROFILE + + try { + $out = pwsh -workingdirectory ~ -c '(Get-Location).Path' + $out | Should -HaveCount 2 + $out[0] | Should -BeExactly (Get-Item ~).FullName + $out[1] | Should -BeExactly "$testdrive" + } + finally { + Set-Content $PROFILE -Value $currentProfile + } + } } }