question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make the current location for script blocks executing in background/thread jobs default to the caller's in all scenarios

See original GitHub issue

Update: This proposal has been approved - the remaining work is summarized in this comment.


Ideally, we’d have a consistent experience across all background-/thread-job related features, meaning that all of the following should exhibit the same behavior with respect to the current location that the script block being passed sees (by default:

  • (a) Start-Job
  • (b) Start-ThreadJob
  • © postpositional & to start a job (implicit Start-Job)
  • (d) ForEach-Object -Parallel

The most sensible location to use is the caller’s current location, which © already uses and (d) will, once #10672 is merged.

The proposal is to make (a) and (b) behave like © and (d) for overall consistency and use of a sensible default:

(b) (Start-ThreadJob) was meant to be modeled after Start-Job’s example, but currently isn’t: it uses the - virtually unpredictable from a user’s perspective - process-wide working directory; given that, it shouldn’t be too late to change it - see https://github.com/PaulHigin/PSThreadJob/issues/46

(a) (Start-Job) regrettably has always defaulted to $HOME (the filesystem provider’s home location).

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?

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.

@iSazonov’s take (from his comment at https://github.com/PowerShell/PowerShell/issues/10537#issuecomment-536199173):

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
SteveL-MSFTcommented, Oct 9, 2019

@PowerShell/powershell-committee discussed this, we agree that each of these should standardize on using the current working directory as the working directory. Start-Job is already inconsistent with itself in that Linux/macOS starts in user’s home directory, but on Windows it starts in user’s Documents folder. We agree to accept a breaking change to always start in the filesystem current working directory where the cmdlet/operator is used for all of the scenarios.

2reactions
PaulHigincommented, Oct 23, 2019

@MathiasMagnus This has already been fixed for ForEach-Object -Parallel (#10672)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Start-ThreadJob - PowerShell
Start-ThreadJob creates background jobs similar to the Start-Job cmdlet. ... By default, the jobs use the current working directory of the caller that ......
Read more >
Start-Job - Module: Microsoft.PowerShell.Core
The default location is the current working directory of the caller that started the job. This parameter was introduced in PowerShell 7.
Read more >
Powershell start-job scope
In PowerShell (Core) 7+ background job now - fortunately - use the same working directory as the caller.
Read more >
Manage events with background scripts - Chrome Developers
Effective background scripts stay dormant until an event they are listening for fires, react with specified instructions, then unload.
Read more >
Using Web Workers - Web APIs - MDN Web Docs
Web Workers are a simple means for web content to run scripts in background threads. The worker thread can perform tasks without interfering ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found