Skip to content

Commit

Permalink
updated background invocation operator to use -WorkingDirectory when …
Browse files Browse the repository at this point in the history
…starting a new job
  • Loading branch information
ayousuf23 committed Sep 30, 2021
1 parent 7cf1ef8 commit 3279c97
Showing 1 changed file with 13 additions and 8 deletions.
Expand Up @@ -531,12 +531,9 @@ private static string GetParameterText(string parameterName)
var pipelineOffset = pipelineAst.Extent.StartOffset;
var variables = pipelineAst.FindAll(static x => x is VariableExpressionAst, true);

// Used to make sure that the job runs in the current directory
const string cmdPrefix = @"Microsoft.PowerShell.Management\Set-Location -LiteralPath $using:pwd ; ";

// Minimize allocations by initializing the stringbuilder to the size of the source string + prefix + space for ${using:} * 2
System.Text.StringBuilder updatedScriptblock = new System.Text.StringBuilder(cmdPrefix.Length + scriptblockBodyString.Length + 18);
updatedScriptblock.Append(cmdPrefix);
// Minimize allocations by initializing the stringbuilder to the size of the source string + space for ${using:} * 2
System.Text.StringBuilder updatedScriptblock = new System.Text.StringBuilder(scriptblockBodyString.Length + 18);

int position = 0;

// Prefix variables in the scriptblock with $using:
Expand Down Expand Up @@ -568,14 +565,22 @@ private static string GetParameterText(string parameterName)
var sb = ScriptBlock.Create(updatedScriptblock.ToString());
var commandInfo = new CmdletInfo("Start-Job", typeof(StartJobCommand));
commandProcessor = context.CommandDiscovery.LookupCommandProcessor(commandInfo, CommandOrigin.Internal, false, context.EngineSessionState);
var parameter = CommandParameterInternal.CreateParameterWithArgument(
var workingDirectoryParameter = CommandParameterInternal.CreateParameterWithArgument(
parameterAst: pipelineAst,
"WorkingDirectory",
null,
argumentAst: pipelineAst,
context.SessionState.Path.CurrentLocation.Path,
false);
var scriptParameter = CommandParameterInternal.CreateParameterWithArgument(
parameterAst: pipelineAst,
"ScriptBlock",
null,
argumentAst: pipelineAst,
sb,
false);
commandProcessor.AddParameter(parameter);
commandProcessor.AddParameter(workingDirectoryParameter);
commandProcessor.AddParameter(scriptParameter);
pipelineProcessor.Add(commandProcessor);
pipelineProcessor.LinkPipelineSuccessOutput(outputPipe ?? new Pipe(new List<object>()));

Expand Down

0 comments on commit 3279c97

Please sign in to comment.