Skip to content

Commit f35b8c6

Browse files
authored
fix: os.Remove should ignore non existing errors (#4502)
1 parent d8eb27f commit f35b8c6

7 files changed

+25
-10
lines changed

server/core/runtime/apply_step_runner.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
version "github.com/hashicorp/go-version"
1313
"github.com/runatlantis/atlantis/server/events/command"
1414
"github.com/runatlantis/atlantis/server/events/models"
15+
"github.com/runatlantis/atlantis/server/utils"
1516
)
1617

1718
// ApplyStepRunner runs `terraform apply`.
@@ -56,7 +57,7 @@ func (a *ApplyStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pa
5657
// If the apply was successful, delete the plan.
5758
if err == nil {
5859
ctx.Log.Info("apply successful, deleting planfile")
59-
if removeErr := os.Remove(planPath); removeErr != nil {
60+
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
6061
ctx.Log.Warn("failed to delete planfile after successful apply: %s", removeErr)
6162
}
6263
}
@@ -116,7 +117,6 @@ func (a *ApplyStepRunner) runRemoteApply(
116117
absPlanPath string,
117118
tfVersion *version.Version,
118119
envs map[string]string) (string, error) {
119-
120120
// The planfile contents are needed to ensure that the plan didn't change
121121
// between plan and apply phases.
122122
planfileBytes, err := os.ReadFile(absPlanPath)

server/core/runtime/import_step_runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
version "github.com/hashicorp/go-version"
88
"github.com/runatlantis/atlantis/server/events/command"
9+
"github.com/runatlantis/atlantis/server/utils"
910
)
1011

1112
type importStepRunner struct {
@@ -37,7 +38,7 @@ func (p *importStepRunner) Run(ctx command.ProjectContext, extraArgs []string, p
3738
if err == nil {
3839
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
3940
ctx.Log.Info("import successful, deleting planfile")
40-
if removeErr := os.Remove(planPath); removeErr != nil {
41+
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
4142
ctx.Log.Warn("failed to delete planfile after successful import: %s", removeErr)
4243
}
4344
}

server/core/runtime/init_step_runner.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package runtime
22

33
import (
4-
"os"
54
"path/filepath"
65

76
version "github.com/hashicorp/go-version"
87
"github.com/runatlantis/atlantis/server/core/runtime/common"
98
"github.com/runatlantis/atlantis/server/events/command"
9+
"github.com/runatlantis/atlantis/server/utils"
1010
)
1111

1212
// InitStep runs `terraform init`.
@@ -21,14 +21,13 @@ func (i *InitStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pat
2121
terraformLockFileTracked, err := common.IsFileTracked(path, lockFileName)
2222
if err != nil {
2323
ctx.Log.Warn("Error checking if %s is tracked in %s", lockFileName, path)
24-
2524
}
2625
// If .terraform.lock.hcl is not tracked in git and it exists prior to init
2726
// delete it as it probably has been created by a previous run of
2827
// terraform init
2928
if common.FileExists(terraformLockfilePath) && !terraformLockFileTracked {
3029
ctx.Log.Debug("Deleting `%s` that was generated by previous terraform init", terraformLockfilePath)
31-
delErr := os.Remove(terraformLockfilePath)
30+
delErr := utils.RemoveIgnoreNonExistent(terraformLockfilePath)
3231
if delErr != nil {
3332
ctx.Log.Info("Error Deleting `%s`", lockFileName)
3433
}

server/core/runtime/state_rm_step_runner.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
version "github.com/hashicorp/go-version"
88
"github.com/runatlantis/atlantis/server/events/command"
9+
"github.com/runatlantis/atlantis/server/utils"
910
)
1011

1112
type stateRmStepRunner struct {
@@ -37,7 +38,7 @@ func (p *stateRmStepRunner) Run(ctx command.ProjectContext, extraArgs []string,
3738
if err == nil {
3839
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
3940
ctx.Log.Info("state rm successful, deleting planfile")
40-
if removeErr := os.Remove(planPath); removeErr != nil {
41+
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
4142
ctx.Log.Warn("failed to delete planfile after successful state rm: %s", removeErr)
4243
}
4344
}

server/events/pending_plan_finder.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/runatlantis/atlantis/server/core/runtime"
11+
"github.com/runatlantis/atlantis/server/utils"
1112
)
1213

1314
//go:generate pegomock generate --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder
@@ -92,7 +93,7 @@ func (p *DefaultPendingPlanFinder) DeletePlans(pullDir string) error {
9293
return err
9394
}
9495
for _, path := range absPaths {
95-
if err := os.Remove(path); err != nil {
96+
if err := utils.RemoveIgnoreNonExistent(path); err != nil {
9697
return errors.Wrapf(err, "delete plan at %s", path)
9798
}
9899
}

server/events/working_dir.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/runatlantis/atlantis/server/core/runtime"
2727
"github.com/runatlantis/atlantis/server/events/models"
2828
"github.com/runatlantis/atlantis/server/logging"
29+
"github.com/runatlantis/atlantis/server/utils"
2930
)
3031

3132
const workingDirPrefix = "repos"
@@ -179,7 +180,6 @@ func (w *FileWorkspace) recheckDiverged(logger logging.SimpleLogging, p models.P
179180
cmd.Dir = cloneDir
180181

181182
output, err := cmd.CombinedOutput()
182-
183183
if err != nil {
184184
logger.Warn("getting remote update failed: %s", string(output))
185185
return false
@@ -420,7 +420,7 @@ func (w *FileWorkspace) SetCheckForUpstreamChanges() {
420420
func (w *FileWorkspace) DeletePlan(logger logging.SimpleLogging, r models.Repo, p models.PullRequest, workspace string, projectPath string, projectName string) error {
421421
planPath := filepath.Join(w.cloneDir(r, p, workspace), projectPath, runtime.GetPlanFilename(workspace, projectName))
422422
logger.Info("Deleting plan: " + planPath)
423-
return os.Remove(planPath)
423+
return utils.RemoveIgnoreNonExistent(planPath)
424424
}
425425

426426
// getGitUntrackedFiles returns a list of Git untracked files in the working dir.

server/utils/os.go

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package utils
2+
3+
import "os"
4+
5+
// RemoveIgnoreNonExistent removes a file, ignoring if it doesn't exist.
6+
func RemoveIgnoreNonExistent(file string) error {
7+
err := os.Remove(file)
8+
if err == nil || os.IsNotExist(err) {
9+
return nil
10+
}
11+
12+
return err
13+
}

0 commit comments

Comments
 (0)