From 9e1926d3b8227a51a6a821c2aa87b7f87250d75a Mon Sep 17 00:00:00 2001 From: Adam Harvey Date: Wed, 4 Nov 2020 17:17:52 -0800 Subject: [PATCH] campaigns: fix cidfile lifecycle on Windows Fixes #367. --- CHANGELOG.md | 2 ++ internal/campaigns/run_steps.go | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3eafc4ae53..d7bdbd5d31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ All notable changes to `src-cli` are documented in this file. ### Fixed +- Executing campaigns on Windows would fail due to obscure `--cidfile` errors: namely, the temporary cidfile would not be removed before `docker run` was invoked. This has been fixed. [#368](https://github.com/sourcegraph/src-cli/pull/368) + ## 3.21.5 ### Added diff --git a/internal/campaigns/run_steps.go b/internal/campaigns/run_steps.go index 96711b3aee..17ebb6e326 100644 --- a/internal/campaigns/run_steps.go +++ b/internal/campaigns/run_steps.go @@ -68,11 +68,24 @@ func runSteps(ctx context.Context, wc *WorkspaceCreator, repo *graphql.Repositor stepContext.PreviousStep = results[i-1] } + // Find a location that we can use for a cidfile, which will contain the + // container ID that is used below. We can then use this to remove the + // container on a successful run, rather than leaving it dangling. cidFile, err := ioutil.TempFile(tempDir, repo.Slug()+"-container-id") if err != nil { return nil, errors.Wrap(err, "Creating a CID file failed") } - _ = os.Remove(cidFile.Name()) // docker exits if this file exists upon `docker run` starting + + // However, Docker will fail if the cidfile actually exists, so we need + // to remove it. Because Windows can't remove open files, we'll first + // close it, even though that's unnecessary elsewhere. + cidFile.Close() + if err = os.Remove(cidFile.Name()); err != nil { + return nil, errors.Wrap(err, "removing cidfile") + } + + // Since we went to all that effort, we can now defer a function that + // uses the cidfile to clean up after this function is done. defer func() { cid, err := ioutil.ReadFile(cidFile.Name()) _ = os.Remove(cidFile.Name())