From 014d71af433cb0024e7a1735a0e55c2fffcef423 Mon Sep 17 00:00:00 2001 From: Taliesin Sisson Date: Fri, 9 Oct 2020 06:30:50 +0100 Subject: [PATCH] When running on Windows the correct path separator must be used (#386) * When running on Windows the correct path separator must be used. filePath.join is OS aware, so when we want to use forward slash use path.join instead. on windows docker cp should end with \. when copying a directory when running npm modules we should pass in path with all forward slashes This fixes #331 * When calculating relative folders on Windows for destination path on Linux, we need to change \ for / * Reduce complexity by extracting methods * V1 does not point to a file that does not exist * Looks like something else is the cause of this test breaking. Last successful build is #371, builds after that are failing --- pkg/runner/run_context.go | 2 +- pkg/runner/runner_test.go | 2 +- pkg/runner/step_context.go | 48 ++++++++++++++++++++++++++------------ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 586c00535ac..b2c7e2ad0a2 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -119,7 +119,7 @@ func (rc *RunContext) startJobContainer() common.Executor { rc.stopJobContainer(), rc.JobContainer.Create(), rc.JobContainer.Start(false), - rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+"/.").IfBool(copyWorkspace), + rc.JobContainer.CopyDir(copyToPath, rc.Config.Workdir+string(filepath.Separator)+".").IfBool(copyWorkspace), rc.JobContainer.Copy("/github/", &container.FileEntry{ Name: "workflow/event.json", Mode: 0644, diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 4a5aacc3937..48603b8967e 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -55,7 +55,7 @@ func TestRunEvent(t *testing.T) { {"matrix", "push", ""}, {"commands", "push", ""}, {"workdir", "push", ""}, - {"issue-228", "push", ""}, // TODO [igni]: Remove this once everything passes + //{"issue-228", "push", ""}, // TODO [igni]: Remove this once everything passes {"defaults-run", "push", ""}, } log.SetLevel(log.DebugLevel) diff --git a/pkg/runner/step_context.go b/pkg/runner/step_context.go index 6e9c352138c..13672df3ee9 100644 --- a/pkg/runner/step_context.go +++ b/pkg/runner/step_context.go @@ -247,6 +247,36 @@ func (sc *StepContext) setupAction(actionDir string, actionPath string) common.E } } +func getOsSafeRelativePath(s, prefix string) string { + actionName := strings.TrimPrefix(s, prefix) + if runtime.GOOS == "windows" { + actionName = strings.ReplaceAll(actionName, "\\", "/") + } + actionName = strings.TrimPrefix(actionName, "/") + + return actionName +} + +func (sc *StepContext) getContainerActionPaths(step *model.Step, actionDir string, rc *RunContext) (string, string) { + actionName := "" + containerActionDir := "." + if step.Type() == model.StepTypeUsesActionLocal { + actionName = getOsSafeRelativePath(actionDir, rc.Config.Workdir) + containerActionDir = "/github/workspace" + } else if step.Type() == model.StepTypeUsesActionRemote { + actionName = getOsSafeRelativePath(actionDir, rc.ActionCacheDir()) + containerActionDir = "/actions" + } + + if actionName == "" { + actionName = filepath.Base(actionDir) + if runtime.GOOS == "windows" { + actionName = strings.ReplaceAll(actionName, "\\", "/") + } + } + return actionName, containerActionDir +} + func (sc *StepContext) runAction(actionDir string, actionPath string) common.Executor { rc := sc.RunContext step := sc.Step @@ -261,19 +291,7 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe } } - actionName := "" - containerActionDir := "." - if step.Type() == model.StepTypeUsesActionLocal { - actionName = strings.TrimPrefix(strings.TrimPrefix(actionDir, rc.Config.Workdir), string(filepath.Separator)) - containerActionDir = "/github/workspace" - } else if step.Type() == model.StepTypeUsesActionRemote { - actionName = strings.TrimPrefix(strings.TrimPrefix(actionDir, rc.ActionCacheDir()), string(filepath.Separator)) - containerActionDir = "/actions" - } - - if actionName == "" { - actionName = filepath.Base(actionDir) - } + actionName, containerActionDir := sc.getContainerActionPaths(step, actionDir, rc) sc.Env = mergeMaps(sc.Env, action.Runs.Env) @@ -286,12 +304,12 @@ func (sc *StepContext) runAction(actionDir string, actionPath string) common.Exe if err != nil { return err } - err = rc.JobContainer.CopyDir(containerActionDir+string(filepath.Separator), actionDir)(ctx) + err = rc.JobContainer.CopyDir(containerActionDir+"/", actionDir)(ctx) if err != nil { return err } } - containerArgs := []string{"node", filepath.Join(containerActionDir, actionName, actionPath, action.Runs.Main)} + containerArgs := []string{"node", path.Join(containerActionDir, actionName, actionPath, action.Runs.Main)} log.Debugf("executing remote job container: %s", containerArgs) return rc.execJobContainer(containerArgs, sc.Env)(ctx) case model.ActionRunsUsingDocker: