From 2ffce0ed6f1e463b638bd4edd672fecf9b2f453f Mon Sep 17 00:00:00 2001 From: David Kale Date: Tue, 17 Dec 2019 12:47:56 -0500 Subject: [PATCH 1/6] Prepend path before locating shell tool --- src/Runner.Worker/Handlers/ScriptHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Runner.Worker/Handlers/ScriptHandler.cs b/src/Runner.Worker/Handlers/ScriptHandler.cs index 6564863a6b8..d676b39e980 100644 --- a/src/Runner.Worker/Handlers/ScriptHandler.cs +++ b/src/Runner.Worker/Handlers/ScriptHandler.cs @@ -144,6 +144,9 @@ public async Task RunAsync(ActionRunStage stage) Inputs.TryGetValue("shell", out var shell); var isContainerStepHost = StepHost is ContainerStepHost; + // Prepend PATH before locating shell tool + AddPrependPathToEnvironment(); + string commandPath, argFormat, shellCommand; // Set up default command and arguments if (string.IsNullOrEmpty(shell)) @@ -205,9 +208,6 @@ public async Task RunAsync(ActionRunStage stage) // Script is written to local path (ie host) but executed relative to the StepHost, which may be a container File.WriteAllText(scriptFilePath, contents, encoding); - // Prepend PATH - AddPrependPathToEnvironment(); - // expose context to environment foreach (var context in ExecutionContext.ExpressionValues) { From 092847a5cfa2899910ebecf80715ea004cfa5c1f Mon Sep 17 00:00:00 2001 From: David Kale Date: Tue, 17 Dec 2019 15:02:44 -0500 Subject: [PATCH 2/6] Join optional prepended path to path before searching it --- src/Runner.Sdk/Util/WhichUtil.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Runner.Sdk/Util/WhichUtil.cs b/src/Runner.Sdk/Util/WhichUtil.cs index d9e2ac5ef5b..373d939d414 100644 --- a/src/Runner.Sdk/Util/WhichUtil.cs +++ b/src/Runner.Sdk/Util/WhichUtil.cs @@ -7,11 +7,11 @@ namespace GitHub.Runner.Sdk { public static class WhichUtil { - public static string Which(string command, bool require = false, ITraceWriter trace = null) + public static string Which(string command, bool require = false, ITraceWriter trace = null, string prependPath = null) { ArgUtil.NotNullOrEmpty(command, nameof(command)); trace?.Info($"Which: '{command}'"); - string path = Environment.GetEnvironmentVariable(PathUtil.PathVariable); + string path = Path.Join(prependPath, Environment.GetEnvironmentVariable(PathUtil.PathVariable)); if (string.IsNullOrEmpty(path)) { trace?.Info("PATH environment variable not defined."); From 2de5fcb9392fec67e3e578c91a690ea3434078e9 Mon Sep 17 00:00:00 2001 From: David Kale Date: Tue, 17 Dec 2019 15:43:40 -0500 Subject: [PATCH 3/6] Use prepended path when whiching shell tool --- src/Runner.Worker/Handlers/ScriptHandler.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Runner.Worker/Handlers/ScriptHandler.cs b/src/Runner.Worker/Handlers/ScriptHandler.cs index d676b39e980..3ff6d146fb1 100644 --- a/src/Runner.Worker/Handlers/ScriptHandler.cs +++ b/src/Runner.Worker/Handlers/ScriptHandler.cs @@ -2,6 +2,7 @@ using System.IO; using System.Text; using System.Threading.Tasks; +using System.Linq; using GitHub.DistributedTask.Pipelines.ContextData; using GitHub.Runner.Common; using GitHub.Runner.Sdk; @@ -146,6 +147,7 @@ public async Task RunAsync(ActionRunStage stage) // Prepend PATH before locating shell tool AddPrependPathToEnvironment(); + string prependPath = string.Join(Path.PathSeparator.ToString(), ExecutionContext.PrependPath.Reverse()); string commandPath, argFormat, shellCommand; // Set up default command and arguments @@ -153,17 +155,17 @@ public async Task RunAsync(ActionRunStage stage) { #if OS_WINDOWS shellCommand = "pwsh"; - commandPath = WhichUtil.Which(shellCommand, require: false, Trace); + commandPath = WhichUtil.Which(shellCommand, require: false, Trace, prependPath); if (string.IsNullOrEmpty(commandPath)) { shellCommand = "powershell"; Trace.Info($"Defaulting to {shellCommand}"); - commandPath = WhichUtil.Which(shellCommand, require: true, Trace); + commandPath = WhichUtil.Which(shellCommand, require: true, Trace, prependPath); } ArgUtil.NotNullOrEmpty(commandPath, "Default Shell"); #else shellCommand = "sh"; - commandPath = WhichUtil.Which("bash", false, Trace) ?? WhichUtil.Which("sh", true, Trace); + commandPath = WhichUtil.Which("bash", false, Trace, prependPath) ?? WhichUtil.Which("sh", true, Trace, prependPath); #endif argFormat = ScriptHandlerHelpers.GetScriptArgumentsFormat(shellCommand); } From 4608399597e7c04125fefb3b5fc19d7158bfafda Mon Sep 17 00:00:00 2001 From: David Kale Date: Tue, 17 Dec 2019 15:49:10 -0500 Subject: [PATCH 4/6] Addition prependPath location --- src/Runner.Worker/Handlers/ScriptHandler.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Runner.Worker/Handlers/ScriptHandler.cs b/src/Runner.Worker/Handlers/ScriptHandler.cs index 3ff6d146fb1..80e907da6a7 100644 --- a/src/Runner.Worker/Handlers/ScriptHandler.cs +++ b/src/Runner.Worker/Handlers/ScriptHandler.cs @@ -174,7 +174,7 @@ public async Task RunAsync(ActionRunStage stage) var parsed = ScriptHandlerHelpers.ParseShellOptionString(shell); shellCommand = parsed.shellCommand; // For non-ContainerStepHost, the command must be located on the host by Which - commandPath = WhichUtil.Which(parsed.shellCommand, !isContainerStepHost, Trace); + commandPath = WhichUtil.Which(parsed.shellCommand, !isContainerStepHost, Trace, prependPath); argFormat = $"{parsed.shellArgs}".TrimStart(); if (string.IsNullOrEmpty(argFormat)) { From a6dc4e1f6eddbab7f0f3d4913059cba8473bbbe6 Mon Sep 17 00:00:00 2001 From: David Kale Date: Tue, 17 Dec 2019 16:11:38 -0500 Subject: [PATCH 5/6] Also use prepended paths when writing out run details --- src/Runner.Sdk/Util/WhichUtil.cs | 6 +++++- src/Runner.Worker/Handlers/ScriptHandler.cs | 9 +++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Runner.Sdk/Util/WhichUtil.cs b/src/Runner.Sdk/Util/WhichUtil.cs index 373d939d414..71acd92d9e8 100644 --- a/src/Runner.Sdk/Util/WhichUtil.cs +++ b/src/Runner.Sdk/Util/WhichUtil.cs @@ -11,12 +11,16 @@ public static string Which(string command, bool require = false, ITraceWriter tr { ArgUtil.NotNullOrEmpty(command, nameof(command)); trace?.Info($"Which: '{command}'"); - string path = Path.Join(prependPath, Environment.GetEnvironmentVariable(PathUtil.PathVariable)); + string path = Environment.GetEnvironmentVariable(PathUtil.PathVariable); if (string.IsNullOrEmpty(path)) { trace?.Info("PATH environment variable not defined."); path = path ?? string.Empty; } + if (!string.IsNullOrEmpty(prependPath)) + { + path = PathUtil.PrependPath(prependPath, path); + } string[] pathSegments = path.Split(new Char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < pathSegments.Length; i++) diff --git a/src/Runner.Worker/Handlers/ScriptHandler.cs b/src/Runner.Worker/Handlers/ScriptHandler.cs index 80e907da6a7..7b582d557de 100644 --- a/src/Runner.Worker/Handlers/ScriptHandler.cs +++ b/src/Runner.Worker/Handlers/ScriptHandler.cs @@ -57,6 +57,7 @@ public override void PrintActionDetails(ActionRunStage stage) string shellCommand; string shellCommandPath = null; bool validateShellOnHost = !(StepHost is ContainerStepHost); + string prependPath = string.Join(Path.PathSeparator.ToString(), ExecutionContext.PrependPath.Reverse()); Inputs.TryGetValue("shell", out var shell); if (string.IsNullOrEmpty(shell)) { @@ -64,19 +65,19 @@ public override void PrintActionDetails(ActionRunStage stage) shellCommand = "pwsh"; if(validateShellOnHost) { - shellCommandPath = WhichUtil.Which(shellCommand, require: false, Trace); + shellCommandPath = WhichUtil.Which(shellCommand, require: false, Trace, prependPath); if (string.IsNullOrEmpty(shellCommandPath)) { shellCommand = "powershell"; Trace.Info($"Defaulting to {shellCommand}"); - shellCommandPath = WhichUtil.Which(shellCommand, require: true, Trace); + shellCommandPath = WhichUtil.Which(shellCommand, require: true, Trace, prependPath); } } #else shellCommand = "sh"; if (validateShellOnHost) { - shellCommandPath = WhichUtil.Which("bash") ?? WhichUtil.Which("sh", true, Trace); + shellCommandPath = WhichUtil.Which("bash", false, Trace, prependPath) ?? WhichUtil.Which("sh", true, Trace, prependPath); } #endif argFormat = ScriptHandlerHelpers.GetScriptArgumentsFormat(shellCommand); @@ -87,7 +88,7 @@ public override void PrintActionDetails(ActionRunStage stage) shellCommand = parsed.shellCommand; if (validateShellOnHost) { - shellCommandPath = WhichUtil.Which(parsed.shellCommand, true, Trace); + shellCommandPath = WhichUtil.Which(parsed.shellCommand, true, Trace, prependPath); } argFormat = $"{parsed.shellArgs}".TrimStart(); From a5172c89541ae7e9fdf64129a98f626a1e962ade Mon Sep 17 00:00:00 2001 From: David Kale Date: Wed, 18 Dec 2019 14:53:45 -0500 Subject: [PATCH 6/6] Small tweak to undo unnecessary change --- src/Runner.Worker/Handlers/ScriptHandler.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Runner.Worker/Handlers/ScriptHandler.cs b/src/Runner.Worker/Handlers/ScriptHandler.cs index 7b582d557de..ccae1350182 100644 --- a/src/Runner.Worker/Handlers/ScriptHandler.cs +++ b/src/Runner.Worker/Handlers/ScriptHandler.cs @@ -146,10 +146,7 @@ public async Task RunAsync(ActionRunStage stage) Inputs.TryGetValue("shell", out var shell); var isContainerStepHost = StepHost is ContainerStepHost; - // Prepend PATH before locating shell tool - AddPrependPathToEnvironment(); string prependPath = string.Join(Path.PathSeparator.ToString(), ExecutionContext.PrependPath.Reverse()); - string commandPath, argFormat, shellCommand; // Set up default command and arguments if (string.IsNullOrEmpty(shell)) @@ -211,6 +208,9 @@ public async Task RunAsync(ActionRunStage stage) // Script is written to local path (ie host) but executed relative to the StepHost, which may be a container File.WriteAllText(scriptFilePath, contents, encoding); + // Prepend PATH + AddPrependPathToEnvironment(); + // expose context to environment foreach (var context in ExecutionContext.ExpressionValues) {