Skip to content

ForEach -Parallel $pwd #10537

Closed
Closed

Description

@vors

Steps to reproduce

Go to some directory other then ~. Run 1 | % -Parallel {$pwd}

Expected behavior

Output should be the same as just $pwd

Actual behavior

Output is always the ~ directory.

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.3
PSEdition                      Core
GitCommitId                    7.0.0-preview.3
OS                             Darwin 18.7.0 Darwin Kernel Version 18.7.0: Tue Aug 20 16:57:14 PDT 2019; root:xnu-4903.271.2~2/RELEASE_X86_64
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Activity

  1. added
    Issue-Question
    on Sep 13, 2019
  2. iSazonov commented on Sep 14, 2019

    @iSazonov
    Collaborator

    Runspaces is always initialized to FileSystemProvider and Directory.GetCurrentDirectory().

  3. added
    WG-Engine-Providers
    and removed
    WG-Engine-Providers
    on Sep 14, 2019
  4. mklement0 commented on Sep 26, 2019

    @mklement0
    Contributor

    The behavior is certainly unexpected; there's no good reason for the current location to differ only because -parallel is added.

    Technically, the behavior is consistent with Start-ThreadJob behavior (which uses [environment]::CurrentDirectory] as Ilya (@iSazonov) notes).

    Start-ThreadJob, in turn, behaves differently from Start-Job, which defaults to $HOME (unlike postpositional &, which sensibly - but inconsistently with Start-Job - defaults to the current location - see #4267).

    Sadly, Start-ThreadJob and Start-Job can't be fixed without potentially breaking existing code - though here's our chance to get it right for ForEach-Object -Parallel.

    The next best thing to get the behavior that should have been the default all along is to use -WorkingDirectory $PWD with Start-Job; sadly, Start-ThreadJob doesn't support -WorkingDirectory as of v2.0.1 - see PaulHigin/PSThreadJob#44

  5. iSazonov commented on Sep 27, 2019

    @iSazonov
    Collaborator

    While working on # 10401, I was thinking over the idea that maybe we should initialize not to Directory.CurrentDirectory(), but to a value from the parent runspace (both provider and current location), but I came to the conclusion that users often don’t understand that many of pwd values ​​can exist (one per runspace) and as a result we will only complicate things and confuse users even more.

    The fact is that when users create runspaces manually, they have the same behavior (ForEach-Object -Parallel) for many years. (I don't understand why we ignore this many years but want change this for ForEach-Object -Parallel.)
    Changing this either way would be a huge breaking change.

    Also there is a problem with $Home in Snap and AppX. It would be more reliable to have Start-ThreadJob initilized to current location too. A script that runs on a regular system may suddenly not work in Snap and AppX #9278 /cc Paul Higinbotham (@PaulHigin).

    Taking into account that users often do not understand how this works, waiting for a certain initial value cannot be considered reliable practice and we should recommend always setting current location explicitly.

  6. vors commented on Sep 27, 2019

    @vors
    CollaboratorAuthor

    This is pure ergonomics question. Sure when somebody uses runspaces and writes 3 non-obvious lines to create a new runspace and control a lot of things about how it's created, it's reasonable to expect to set the location explicitly. When you have a % loop and realize that it can speed up by parallelization, it would be nice to change as little as possible after adding -Parallel. I don't think that people would know they need the parallelization before actually writing and trying the code for a normal foreach. So less friction is it to include -Parallel the better.

    Changing this either way would be a huge breaking change.

    ForEach -Parallel haven't been realized in any stable build yet - I don't see changing behavior of it as a breaking change. The behavior of it doesn't need to match the runspace creation 1:1.

  7. mklement0 commented on Sep 27, 2019

    @mklement0
    Contributor

    Ilya (@iSazonov): Let me add to sergei (@vors)' comment:

    users often don’t understand that many of pwd values ​​can exist (one per runspace)

    Nor should they have to understand, unless it is inevitable.

    The following user expectation is perfectible reasonable and should never be subverted unless absolutely necessary for technical reaons:

    Code invoked by a caller (the current runspace) on the same machine should see the same current location as the caller, unless requested otherwise.

    Fortunately, this is true in the following cases:

    • running PowerShell code in the same runspace (but not .NET method calls).

    • invoking an external process (in which case the filesystem provider's current location is used).

      • Including use of Start-Process, even with -Verb RunAs, if the target executable is pwsh itself.
    • when using ... & to create a background job - but not with Start-Job.

    It is not true in the following cases - regrettably so, but I presume it's too late to change that:

    • Start-Job (defaults to $HOME)
    • Start-ThreadJob (uses the process-wide working dir, [Environment]::CurrentDirectory)
    • (This may be a technical necessity, I'm not sure) Start-Process -Verb RunAs with an executable other than pwsh (uses $env:windir\System32).

    As sergei (@vors) points out, it is not too late to fix this for ForEach-Object -Parallel - and I don't see a problem with diverging from Start-ThreadJob, if necessary, as the connection between the two isn't obvious, and it's conceivable that users will make do with ForEach-Object -Parallel without ever needing Start-ThreadJob.

    It cannot be done for technical reasons when calling .NET methods (per-runspace location in PowerShell vs. single, process-wide working directory in .NET)

    This is and will remain a perennial pain point when attempting to pass relative paths to .NET methods.

    I'm starting to wonder if we should ask CoreFx for an opt-in to thread-local working directories to resolve this problem.

    (By contrast, execution of code on a different computer justifiably should carry no expectation of the caller's location being retained (which may be impossible anyway)).

  8. iSazonov commented on Sep 28, 2019

    @iSazonov
    Collaborator

    Last days I was somewhere deep in PowerShell code :-) so my comment was from that side.
    If look from user side I full agree.

    So we have two rules:
    rule A - "new runspace inherits current provider and location".
    rule B that "new runspace has FileSystem provider default and Directory.CurrentDirectory location".

    I am sure that we should have single rule and it is rule A so as not to mislead users and fix all three cases.
    We could expose this as new experimental feature.
    I think there are not problems with -Parallel and Start-ThreadJob. As for Start-Job defaulting to $Home it looks like a bug ("I am on my working dir but a job puts results to home dir". also there could be problems in containers as mentioned aboved) and we should fix it.

  9. mklement0 commented on Sep 28, 2019

    @mklement0
    Contributor

    Thanks, Ilya (@iSazonov) - I'm definitely in favor of a fix as well.

    I agree that fixing Start-ThreadJob is probably unproblematic, given that Start-ThreadJob's behavior is virtually unpredictable from the user's perspective (it's whatever PowerShell's startup directory was - irrespective of a -WorkingDirectory argument - or whatever in-session code that changed the process-wide current directory last set it to). I've created an issue for it: PaulHigin/PSThreadJob#46

    However, given that for Start-Job it is a fixed directory, users are much more likely to have come to depend on it.

    Given that experimental features are an "on-ramp" for regular features, how do you see that transition unfolding?

    Even if users adopt the experimental feature in significant enough numbers, turning it into a regular feature can still break existing code if it is turned into a regular feature.

  10. iSazonov commented on Sep 28, 2019

    @iSazonov
    Collaborator

    I suggest to consider Start-Job case as bug.
    If my work folder is c:\work and I run a job which switched to $home that is c:\users\isazonov - the last place is the worst place where user would like to see a resulting file. We could guess that user would use relative path to Documents folder. But most likely when faced with this inconsistency, the user will use the exact path to the working directory (c:\work), especially since the job is perceived as some kind of background process.

  11. PaulHigin commented on Oct 1, 2019

    @PaulHigin
    Contributor

    Sorry for the late reply, I was out of town last week. I agree that this is a bug and that foreach -parallel should preserve the current working directory, and be consistent with foreach as much as possible. I am not as sure about threadjob since my goal there was to make it work like Start-Job.

  12. iSazonov commented on Oct 1, 2019

    @iSazonov
    Collaborator

    Steve Lee (@SteveL-MSFT) It would good to get a conclusion for the issue before 7.0 GA. Maybe PowerShell committee do it?

  13. mklement0 commented on Oct 1, 2019

    @mklement0
    Contributor

    Paul Higinbotham (@PaulHigin)

    Making Start-ThreadJob follow Start-Job's example generally makes sense, but note that:

    (a) it already behaves differently from Start-Job

    (b) even if that were to be fixed, Start-Job's behavior is unfortunate

    (c) Ideally, we'd have a consistent experience across all job/thread-related features, meaning:

    • Start-Job
    • Start-ThreadJob
    • postpositional & to start a job
    • ForEach-Object -Parallel

    The question now is:

    Can we consider Start-Job's default-to-$HOME behavior a bug / something worth fixing as a Bucket 3: Unlikely Grey Area change, as argued by Ilya (@iSazonov) above?

    If so, we can achieve the desired consistency, by making all features default to the caller's current location.

    I'm certainly personally in favor of that, but I haven't looked into how likely it is that existing Start-Job code relies on $HOME to be the default directory.

  14. PaulHigin commented on Oct 1, 2019

    @PaulHigin
    Contributor

    I have submitted a PR for the fix for ForEach-Object -Parallel (#10672 ).

  15. mklement0 commented on Oct 1, 2019

    @mklement0
    Contributor

    Thanks, Paul Higinbotham (@PaulHigin) - I've created #10673 to address the larger issue.

  16. added
    WG-Cmdlets
    and removed on Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-QuestionResolution-DuplicateWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions