Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: os.Remove should ignore non existing errors #4502

Merged
merged 2 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/core/runtime/apply_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/utils"
)

// ApplyStepRunner runs `terraform apply`.
Expand Down Expand Up @@ -56,7 +57,7 @@ func (a *ApplyStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pa
// If the apply was successful, delete the plan.
if err == nil {
ctx.Log.Info("apply successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful apply: %s", removeErr)
}
}
Expand Down Expand Up @@ -116,7 +117,6 @@ func (a *ApplyStepRunner) runRemoteApply(
absPlanPath string,
tfVersion *version.Version,
envs map[string]string) (string, error) {

// The planfile contents are needed to ensure that the plan didn't change
// between plan and apply phases.
planfileBytes, err := os.ReadFile(absPlanPath)
Expand Down
3 changes: 2 additions & 1 deletion server/core/runtime/import_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)

type importStepRunner struct {
Expand Down Expand Up @@ -37,7 +38,7 @@ func (p *importStepRunner) Run(ctx command.ProjectContext, extraArgs []string, p
if err == nil {
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
ctx.Log.Info("import successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful import: %s", removeErr)
}
}
Expand Down
5 changes: 2 additions & 3 deletions server/core/runtime/init_step_runner.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package runtime

import (
"os"
"path/filepath"

version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/core/runtime/common"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)

// InitStep runs `terraform init`.
Expand All @@ -21,14 +21,13 @@ func (i *InitStepRunner) Run(ctx command.ProjectContext, extraArgs []string, pat
terraformLockFileTracked, err := common.IsFileTracked(path, lockFileName)
if err != nil {
ctx.Log.Warn("Error checking if %s is tracked in %s", lockFileName, path)

}
// If .terraform.lock.hcl is not tracked in git and it exists prior to init
// delete it as it probably has been created by a previous run of
// terraform init
if common.FileExists(terraformLockfilePath) && !terraformLockFileTracked {
ctx.Log.Debug("Deleting `%s` that was generated by previous terraform init", terraformLockfilePath)
delErr := os.Remove(terraformLockfilePath)
delErr := utils.RemoveIgnoreNonExistent(terraformLockfilePath)
if delErr != nil {
ctx.Log.Info("Error Deleting `%s`", lockFileName)
}
Expand Down
3 changes: 2 additions & 1 deletion server/core/runtime/state_rm_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

version "github.com/hashicorp/go-version"
"github.com/runatlantis/atlantis/server/events/command"
"github.com/runatlantis/atlantis/server/utils"
)

type stateRmStepRunner struct {
Expand Down Expand Up @@ -37,7 +38,7 @@ func (p *stateRmStepRunner) Run(ctx command.ProjectContext, extraArgs []string,
if err == nil {
if _, planPathErr := os.Stat(planPath); !os.IsNotExist(planPathErr) {
ctx.Log.Info("state rm successful, deleting planfile")
if removeErr := os.Remove(planPath); removeErr != nil {
if removeErr := utils.RemoveIgnoreNonExistent(planPath); removeErr != nil {
ctx.Log.Warn("failed to delete planfile after successful state rm: %s", removeErr)
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/events/pending_plan_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/pkg/errors"
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/utils"
)

//go:generate pegomock generate --package mocks -o mocks/mock_pending_plan_finder.go PendingPlanFinder
Expand Down Expand Up @@ -92,7 +93,7 @@ func (p *DefaultPendingPlanFinder) DeletePlans(pullDir string) error {
return err
}
for _, path := range absPaths {
if err := os.Remove(path); err != nil {
if err := utils.RemoveIgnoreNonExistent(path); err != nil {
return errors.Wrapf(err, "delete plan at %s", path)
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/events/working_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/runatlantis/atlantis/server/core/runtime"
"github.com/runatlantis/atlantis/server/events/models"
"github.com/runatlantis/atlantis/server/logging"
"github.com/runatlantis/atlantis/server/utils"
)

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

output, err := cmd.CombinedOutput()

if err != nil {
logger.Warn("getting remote update failed: %s", string(output))
return false
Expand Down Expand Up @@ -420,7 +420,7 @@ func (w *FileWorkspace) SetCheckForUpstreamChanges() {
func (w *FileWorkspace) DeletePlan(logger logging.SimpleLogging, r models.Repo, p models.PullRequest, workspace string, projectPath string, projectName string) error {
planPath := filepath.Join(w.cloneDir(r, p, workspace), projectPath, runtime.GetPlanFilename(workspace, projectName))
logger.Info("Deleting plan: " + planPath)
return os.Remove(planPath)
return utils.RemoveIgnoreNonExistent(planPath)
}

// getGitUntrackedFiles returns a list of Git untracked files in the working dir.
Expand Down
13 changes: 13 additions & 0 deletions server/utils/os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package utils

import "os"

// RemoveIgnoreNonExistent removes a file, ignoring if it doesn't exist.
func RemoveIgnoreNonExistent(file string) error {
err := os.Remove(file)
if err == nil || os.IsNotExist(err) {
return nil
}

return err
}
Loading