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

Allow configuring working directory for a new Runspace in InitialSessionState #17603

Closed
MatejKafka opened this issue Jun 28, 2022 · 5 comments · May be fixed by #21446
Closed

Allow configuring working directory for a new Runspace in InitialSessionState #17603

MatejKafka opened this issue Jun 28, 2022 · 5 comments · May be fixed by #21446
Labels
In-PR Indicates that a PR is out for the issue Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime

Comments

@MatejKafka
Copy link

MatejKafka commented Jun 28, 2022

Summary of the new feature / enhancement

When running a PowerShell script in a new Runspace, I want to set the working directory. Currently, I cannot set it using InitialSessionState, and instead have to first .Open() the Runspace and then set it by calling runspace.SessionStateProxy.Path.SetLocation(...);.

This is

  1. cumbersome – almost everything else can be set declaratively using initialSessionState, but not the working directory, even through the working directory setup is handled there (in .Bind and .SetSessionStateDrive),
  2. wrong – any modules imported during .Open() (configured using initialSessionState.ImportPSModules(...)) will see the process-wide .NET working directory, especially script modules with top-level statements.

To avoid issue 2., I believe this ugly hack is currently required (which could have side-effects for other running threads):

var originalWorkingDirectory = Environment.CurrentDirectory;
try {
    Environment.CurrentDirectory = ...;
    runspace.Open();
} finally {
    Environment.CurrentDirectory = originalWorkingDirectory;
}

Proposed technical implementation details (optional)

Add an initialSessionState.WorkingDirectory string property, initially set to null, which can be overwritten with a custom working directory. If unchanged, the current behavior is kept, so it should be a backwards compatible change. If the directory does not exist, throw an exception.

If we agree on this solution, I could try implementing a PR.

@MatejKafka MatejKafka added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Jun 28, 2022
@daxian-dbw daxian-dbw added the WG-Engine core PowerShell engine, interpreter, and runtime label Jun 29, 2022
@vexx32
Copy link
Collaborator

vexx32 commented Nov 15, 2022

WG-Engine discussed this today.

We think this overall is a good idea, just want to clarify the desired behaviour a little. We think you're suggesting that we have the runspace's Open() method essentially pre-insert a SetLocation() call using the suggested InitialSessionState.WorkingDirectory before running any other code in the runspace (for example, to handle module imports and other operations defined in its InitialSessionState). Is that accurate?

Concerns were raised over mimicking your "hack" in the runspace code, we definitely don't want to be changing the process-wide current directory during runspace operations. I don't think that was what you were suggesting, but we felt we should clarify just in case. 🙂

If that matches your expectations, we'd be happy to accept a PR for this. Thanks for raising this issue! 💖

@vexx32 vexx32 removed the Needs-Triage The issue is new and needs to be triaged by a work group. label Nov 15, 2022
@vexx32 vexx32 removed their assignment Jan 9, 2023
Copy link
Contributor

This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Resolution-No Activity Issue has had no activity for 6 months or more label Nov 15, 2023
Copy link
Contributor

This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

@MatejKafka
Copy link
Author

not completed, please reopen.

@microsoft-github-policy-service microsoft-github-policy-service bot added the In-PR Indicates that a PR is out for the issue label Apr 8, 2024
MatejKafka added a commit to MatejKafka/PowerShell that referenced this issue Apr 8, 2024
MatejKafka added a commit to MatejKafka/PowerShell that referenced this issue Apr 16, 2024
MatejKafka added a commit to MatejKafka/PowerShell that referenced this issue Apr 16, 2024
MatejKafka added a commit to MatejKafka/PowerShell that referenced this issue Apr 17, 2024
MatejKafka added a commit to MatejKafka/PowerShell that referenced this issue Apr 17, 2024
@daxian-dbw daxian-dbw reopened this Apr 29, 2024
@MatejKafka
Copy link
Author

Eh, still not completed. Could you tame your hyperactive bot?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In-PR Indicates that a PR is out for the issue Issue-Enhancement the issue is more of a feature request than a bug Resolution-No Activity Issue has had no activity for 6 months or more WG-Engine core PowerShell engine, interpreter, and runtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants