Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InitialSessionState: Add support for configuring initial working directory #21446

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

MatejKafka
Copy link

PR Summary

Add support for configuring the initial working directory of a Runspace using a new WorkingDirectory property on InitialSessionState.

I recommend reviewing the 2 commits separately, first one refactors the method without major changes in behavior, second one implements the feature.

PR Context

Resolves #17603.

Design questions

@vexx32 I have 2 questions about the design of the feature and related functionality:

1) If the working directory is invalid (does not exist, unknown drive,...), I propagate the exception back to the caller, without checking ThrowOnRunspaceOpenError. Should I add a check and suppress the exception if it's not set? My reasoning is that if a caller explicitly sets a custom working directory, he probably expects it to be valid and get notified when it's not, even when he's not interested in other initialization errors, but it's inconsistent with how other errors are handled.

2) In the original implementation, there's a check for if (context.EngineSessionState.ProviderCount > 0) and if there are no providers, the function does not do anything, leaving the current drive and location unset. However, this results in subsequent accesses to the location throwing confusing internal errors (even from scripts running inside the Runspace):

$iss = [initialsessionstate]::CreateDefault2()
$iss.ThrowOnRunspaceOpenError = $true
# remove all providers
$iss.Providers.RemoveItem(0, $iss.Providers.Count)

$r = [runspacefactory]::CreateRunspace($Host, $iss)
$r.Open()
[powershell]::Create($r).AddCommand("pwd").Invoke()
# Exception calling "Invoke" with "0" argument(s): "Cannot perform operation because operation "get_CurrentLocation" is not valid. Remove operation "get_CurrentLocation", or investigate why it is not valid."

For now, I've kept the original behavior, but it seems cleaner to me to throw an exception if there are no providers defined to avoid the confusing errors later on. I can either modify the behavior in a subsequent commit, or I can create a new PR after this one is done, just wanted to hear your opinion.

PR Checklist

Copy link
Collaborator

@jborean93 jborean93 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The codefactor tests look like they need fixing, the PowerShell team won't really consider PRs ready until CI is green.

There should also be tests added for this new API to verify the new changes added. We should be testing things like

  • We can set it to a custom location
  • UNC paths work (Windows only and can use \\localhost\...)
  • Can set to a root of another drive, i.e. Temp and/or Env
  • Fails on invalid path (missing and invalid drive)

@SteveL-MSFT SteveL-MSFT added WG-Engine core PowerShell engine, interpreter, and runtime WG-NeedsReview Needs a review by the labeled Working Group labels Apr 15, 2024
@MatejKafka
Copy link
Author

MatejKafka commented Apr 16, 2024

I fixed the two issues mentioned in code review.

The codefactor tests look like they need fixing, the PowerShell team won't really consider PRs ready until CI is green.

All of the codefactor issues are doc only, and most of the surrounding code is also breaking the rules. If you want me to fix the tests, I can, but I don't see any benefit from it. :)

There should also be tests added for this new API to verify the new changes added. We should be testing things like

If the WG wants me to add unit tests, I can, but none of the other properties on InitialSessionState are currently unit tested.

@jborean93
Copy link
Collaborator

All of the codefactor issues are doc only, and most of the surrounding code is also breaking the rules. If you want me to fix the tests, I can, but I don't see any benefit from it. :)

Unfortunately it's part of the CI tests, it needs to be fixed for it to be considered ready. It's part of the style tests of PowerShell and the documentation even derives what will be part of the docs when the it is live.

If the WG wants me to add unit tests, I can, but none of the other properties on InitialSessionState are currently unit tested.

That doesn't mean you shouldn't add tests. Tests are critical to show that the code you've added works and also ensures there are no regressions in the future. I'm not part of the WG but you should always add tests to anything you touch regardless of whether or not there are existing ones.

@MatejKafka MatejKafka force-pushed the iss-working-dir branch 2 times, most recently from cf9f464 to 3402147 Compare April 17, 2024 02:17
…or adding working directory configuration support (see next commit)

Behavior should be unchanged, except that the catch-all `try` block was shortened to the relevant part of the function – exceptions thrown by the rest of the function are unrelated to drive and location configuration and indicate serious issues which should probably be propagated.
@MatejKafka MatejKafka force-pushed the iss-working-dir branch 3 times, most recently from b149256 to 271eb67 Compare April 17, 2024 02:26
@MatejKafka
Copy link
Author

MatejKafka commented Apr 17, 2024

I removed the exception docstrings, which fixes most of the codefactor issues. One of the remaining issues does not make any sense (spacing around new(...)), fixing the other one would make the docstring less readable and no other property in InitialSessionState uses the suggested pattern.

I also added a few unit tests for the Location property.

I consider the PR ready to review.


Tests are critical to show that the code you've added works and also ensures there are no regressions in the future.

Maybe if PowerShell maintainers heeded that advice, I wouldn't be finding PowerShell bugs every other week, but I digress.

@jborean93
Copy link
Collaborator

jborean93 commented Apr 17, 2024

One of the remaining issues does not make any sense (spacing around new(...)

The rules that PowerShell uses don't like the new() syntax without the type name. You need to use new TypeName(...); instead.

Maybe if PowerShell maintainers heeded that advice, I wouldn't be finding PowerShell bugs every other week, but I digress.

That's why adding tests are critical, we can't do much about existing tests but we can be sure that new features and fixes being added now have tests (where feasible).

@MatejKafka
Copy link
Author

I don't believe the Pester test failures are related to my PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Review - Needed The PR is being reviewed WG-Engine core PowerShell engine, interpreter, and runtime WG-NeedsReview Needs a review by the labeled Working Group
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow configuring working directory for a new Runspace in InitialSessionState
4 participants