Skip to content

Commit

Permalink
InitialSessionState: Add support for configuring initial working dire…
Browse files Browse the repository at this point in the history
…ctory (#17603)
  • Loading branch information
MatejKafka committed Apr 16, 2024
1 parent 16ad989 commit cf9f464
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/System.Management.Automation/engine/InitialSessionState.cs
Expand Up @@ -1678,6 +1678,7 @@ public InitialSessionState Clone()
ss.ThreadOptions = this.ThreadOptions;
ss.ThrowOnRunspaceOpenError = this.ThrowOnRunspaceOpenError;
ss.ApartmentState = this.ApartmentState;
ss.DefaultLocation = this.DefaultLocation;

foreach (ModuleSpecification modSpec in this.ModuleSpecificationsToImport)
{
Expand Down Expand Up @@ -1831,6 +1832,12 @@ public Microsoft.PowerShell.ExecutionPolicy ExecutionPolicy
/// </summary>
public bool ThrowOnRunspaceOpenError { get; set; } = false;

/// <summary>
/// If not null, the working location of the runspace is set to this path. If null,
/// the <see cref="Environment.CurrentDirectory">process working directory</see> is used as a default.
/// </summary>
public string DefaultLocation { get; set; } = null;

/// <summary>
/// This property will be set only if we are refreshing the Type/Format settings by calling UpdateTypes/UpdateFormats directly.
/// In this case, we should wait until all type/format entries get processed. After that, if there were errors
Expand Down Expand Up @@ -2231,7 +2238,7 @@ internal void Bind(ExecutionContext context, bool updateOnly, PSModuleInfo modul
}
}

SetSessionStateDrive(context, setLocation: setLocation);
SetSessionStateDrive(context, setLocation, DefaultLocation);
}

private void Bind_SetVariables(SessionStateInternal ss)
Expand Down Expand Up @@ -3370,7 +3377,7 @@ internal void ResetRunspaceState(ExecutionContext context)
InitialSessionState.CreateQuestionVariable(context);

// Reset the path for this runspace.
SetSessionStateDrive(context, true);
SetSessionStateDrive(context, true, DefaultLocation);

// Reset the event, transaction and debug managers.
context.ResetManagers();
Expand Down Expand Up @@ -3408,13 +3415,11 @@ private static bool TryInitSessionStateCurrentDrive(ExecutionContext context)
}
else
{
ItemNotFoundException itemNotFound = new(Environment.CurrentDirectory, "PathNotFound", SessionStateStrings.PathNotFound);
context.ReportEngineStartupError(itemNotFound);
return false;
}
}

internal static void SetSessionStateDrive(ExecutionContext context, bool setLocation)
internal static void SetSessionStateDrive(ExecutionContext context, bool setLocation, string location)
{
if (context.EngineSessionState.ProviderCount == 0)
{
Expand All @@ -3426,6 +3431,9 @@ internal static void SetSessionStateDrive(ExecutionContext context, bool setLoca
// UNC paths
if (context.EngineSessionState.CurrentDrive == null && !TryInitSessionStateCurrentDrive(context))
{
// FIXME: this is a wrong exception to throw, we don't yet know that the path was not found
ItemNotFoundException itemNotFound = new(location ?? Environment.CurrentDirectory, "PathNotFound", SessionStateStrings.PathNotFound);
context.ReportEngineStartupError(itemNotFound);
return;
}

Expand All @@ -3436,7 +3444,16 @@ internal static void SetSessionStateDrive(ExecutionContext context, bool setLoca

var providerContext = new CmdletProviderContext(context) { SuppressWildcardExpansion = true };

// Set the starting location to the current process working directory
// User set a custom initial working directory.
if (location != null)
{
// If the location is invalid or does not exist, let the exception bubble up; since the user explicitly
// configured the working directory, he probably wants to get notified on failure.
context.EngineSessionState.SetLocation(location, providerContext);
return;
}

// As a fallback, set the starting location to the current process working directory.
// Ignore any errors as the file system provider may not be loaded or
// a drive with the same name as the real file system drive may not have
// been mounted.
Expand Down

0 comments on commit cf9f464

Please sign in to comment.