move processing of -WorkingDirectory before processing of profiles (#8079)

This commit is contained in:
Steve Lee 2018-10-29 13:56:13 -07:00 committed by Aditya Patwardhan
parent b3f315eb15
commit b1e2745b53
2 changed files with 44 additions and 25 deletions

View file

@ -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)
{

View file

@ -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
}
}
}
}