PaulHigin / PSThreadJob

A PowerShell module for running concurrent jobs based on threads rather than processes
MIT License
180 stars 18 forks source link

Make $PWD default to the caller's current location in the script blocks passed to Start-ThreadJob #46

Closed mklement0 closed 5 years ago

mklement0 commented 5 years ago

Following up from the discussion in https://github.com/PowerShell/PowerShell/issues/10537:

Currently, script blocks passed to Start-ThreadJob default to the process-wide current directory, as reflected in [Environment]::CurrentDirectory.

However, this is problematic, because that directory has no guaranteed or obvious relationship with the caller's current location.

Therefore, defaulting to the caller's current location instead is the most useful and intuitive behavior.

While technically a breaking change, my sense is that it falls into Bucket 3: Unlikely Grey Area, given that the effective $PWD is virtually unpredictable by the user:

$PWD is whatever PowerShell's startup directory was - irrespective of a -WorkingDirectory argument passed to the CLI - or whatever in-session code that changed the process-wide current directory last set it to.

A simple example:

PS> Set-Location /; pwsh -WorkingDirectory $HOME -noprofile -c 'Start-ThreadJob { $pwd } | Receive-Job -Wait -AutoRemove; $PWD'

Path
----
/
/Users/jdoe

That is, the above made / (or, on Windows, C:\) the $PWD for thread jobs, just because the pwsh instance was invoked while that directory happened to have been the process-wide working directory - even though the session's $PWD is $HOME.

PaulHigin commented 5 years ago

Implemented: #47