Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,6 +1614,31 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand,

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
Expand Down Expand Up @@ -1694,31 +1719,6 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand,
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)
{
Expand Down
19 changes: 19 additions & 0 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down