Skip to content

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

@mklement0

Description

@mklement0

Following up from the discussion in PowerShell/PowerShell#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions