From 46957e54d0ad22ec02220b56c941d9123c5037c9 Mon Sep 17 00:00:00 2001 From: Ilya Date: Thu, 31 Oct 2019 02:22:45 +0500 Subject: [PATCH] Set working directory to current directory in `Start-Job` (#10920) --- .../engine/remoting/commands/StartJob.cs | 11 +++++++++++ test/powershell/engine/Job/Jobs.Tests.ps1 | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/System.Management.Automation/engine/remoting/commands/StartJob.cs b/src/System.Management.Automation/engine/remoting/commands/StartJob.cs index 615d6e9bd3..c63e28fb18 100644 --- a/src/System.Management.Automation/engine/remoting/commands/StartJob.cs +++ b/src/System.Management.Automation/engine/remoting/commands/StartJob.cs @@ -608,6 +608,17 @@ namespace Microsoft.PowerShell.Commands ThrowTerminatingError(errorRecord); } + if (WorkingDirectory == null) + { + try + { + WorkingDirectory = SessionState.Internal.CurrentLocation.Path; + } + catch (PSInvalidOperationException) + { + } + } + CommandDiscovery.AutoloadModulesWithJobSourceAdapters(this.Context, this.CommandOrigin); if (ParameterSetName == DefinitionNameParameterSet) diff --git a/test/powershell/engine/Job/Jobs.Tests.ps1 b/test/powershell/engine/Job/Jobs.Tests.ps1 index 4e0332afdb..f7d73a9fc2 100644 --- a/test/powershell/engine/Job/Jobs.Tests.ps1 +++ b/test/powershell/engine/Job/Jobs.Tests.ps1 @@ -1,5 +1,6 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + Describe 'Basic Job Tests' -Tags 'CI' { BeforeAll { # Make sure we do not have any jobs running @@ -89,6 +90,13 @@ Describe 'Basic Job Tests' -Tags 'CI' { {Start-Job -ScriptBlock { 1 + 1 } -WorkingDirectory $path} | Should -Throw -ErrorId $expectedErrorId } + It 'Verifies that the current working directory is preserved' { + $job = Start-Job -ScriptBlock { $pwd } + $location = $job | Wait-Job | Receive-Job + $job | Remove-Job + $location.Path | Should -BeExactly $pwd.Path + } + It "Create job with native command" { try { $nativeJob = Start-job { pwsh -c 1+1 }