Skip to content

Commit 2e10c1f

Browse files
authored
fix: cleanup of orphaned files must respect -C flag. (#1886)
## What this PR does / why we need it: When using `terramate -C <dir> generate`, Terramate is still cleaning up orphaned files in the entire repository. ## Which issue(s) this PR fixes: none ## Special notes for your reviewer: ## Does this PR introduce a user-facing change? ``` yes, fixes a bug. ```
2 parents bb27332 + b5aa451 commit 2e10c1f

File tree

3 files changed

+107
-12
lines changed

3 files changed

+107
-12
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
2222

2323
## Unreleased
2424

25+
### Fixed
26+
27+
- Fix the cleaning up of orphaned files in the `terramate generate` to respect the `-C <dir>` flag.
28+
2529
## v0.10.6
2630

2731
### Fixed

generate/generate.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func Do(
194194
stackReport := doStackGeneration(root, tree, vendorDir, vendorRequests)
195195
rootReport := doRootGeneration(root, tree)
196196
report := mergeReports(stackReport, rootReport)
197-
return cleanupOrphaned(root, report)
197+
return cleanupOrphaned(root, tree, report)
198198
}
199199

200200
func doStackGeneration(
@@ -1339,21 +1339,23 @@ func loadStackCodeCfgs(
13391339
return genfilesConfigs, nil
13401340
}
13411341

1342-
func cleanupOrphaned(root *config.Root, report Report) Report {
1342+
func cleanupOrphaned(root *config.Root, target *config.Tree, report Report) Report {
13431343
logger := log.With().
13441344
Str("action", "generate.cleanupOrphaned()").
1345+
Stringer("dir", target.Dir()).
13451346
Logger()
1346-
// If the root of the tree is a stack then there is nothing to do
1347-
// since there can't be any orphans (the root parent stack owns
1348-
// the entire project).
1349-
if root.Tree().IsStack() {
1350-
logger.Debug().Msg("project root is a stack, nothing to do")
1347+
1348+
defer report.sort()
1349+
1350+
// If the target tree is a stack then there is nothing to do
1351+
// as it was already generated at this point.
1352+
if target.IsStack() {
13511353
return report
13521354
}
13531355

13541356
logger.Debug().Msg("listing orphaned generated files")
13551357

1356-
orphanedGenFiles, err := ListStackGenFiles(root, root.HostDir())
1358+
orphanedGenFiles, err := ListStackGenFiles(root, target.HostDir())
13571359
if err != nil {
13581360
report.CleanupErr = err
13591361
return report
@@ -1363,8 +1365,8 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
13631365
deleteFailures := map[project.Path]*errors.List{}
13641366

13651367
for _, genfile := range orphanedGenFiles {
1366-
genfileAbspath := filepath.Join(root.HostDir(), genfile)
1367-
dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile)))
1368+
genfileAbspath := filepath.Join(target.HostDir(), genfile)
1369+
dir := project.PrjAbsPath(root.HostDir(), filepath.Dir(genfileAbspath))
13681370
if err := os.Remove(genfileAbspath); err != nil {
13691371
if deleteFailures[dir] == nil {
13701372
deleteFailures[dir] = errors.L()
@@ -1402,7 +1404,5 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
14021404
Deleted: deletedFiles,
14031405
})
14041406
}
1405-
1406-
report.sort()
14071407
return report
14081408
}

generate/generate_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,97 @@ func TestGenerateCleanup(t *testing.T) {
799799
},
800800
},
801801
},
802+
{
803+
name: "workdir is respected in cleanup",
804+
layout: []string{
805+
"s:stacks/stack-1",
806+
"s:stacks/stack-2",
807+
"s:stacks/stack-1/stack-1-a",
808+
"s:stacks/stack-1/stack-1-b",
809+
"s:stacks/stack-3", // only files here will be cleaned up.
810+
genfile("dir/orphan.hcl"),
811+
genfile("stacks/stack-1/a.hcl"),
812+
genfile("stacks/stack-1/subdir/b.hcl"),
813+
genfile("stacks/stack-1/subdir/dir/c.hcl"),
814+
genfile("stacks/stack-1/stack-1-a/e.hcl"),
815+
genfile("stacks/stack-1/stack-1-a/subdir/f.hcl"),
816+
genfile("stacks/stack-1/stack-1-a/subdir/dir/g.hcl"),
817+
genfile("stacks/stack-1/stack-1-b/h.hcl"),
818+
genfile("stacks/stack-2/d.hcl"),
819+
genfile("stacks/stack-3/x.hcl"),
820+
genfile("stacks/stack-3/subdir/y.hcl"),
821+
genfile("stacks/stack-3/subdir/dir/z.hcl"),
822+
},
823+
fromdir: "/stacks/stack-3",
824+
wantReport: generate.Report{
825+
Successes: []generate.Result{
826+
{
827+
Dir: project.NewPath("/stacks/stack-3"),
828+
Deleted: []string{
829+
"subdir/dir/z.hcl",
830+
"subdir/y.hcl",
831+
"x.hcl",
832+
},
833+
},
834+
},
835+
},
836+
want: []generatedFile{
837+
{
838+
dir: "/stacks/stack-1",
839+
files: map[string]fmt.Stringer{
840+
"a.hcl": Doc(),
841+
},
842+
},
843+
{
844+
dir: "/stacks/stack-1/stack-1-a",
845+
files: map[string]fmt.Stringer{
846+
"e.hcl": Doc(),
847+
},
848+
},
849+
{
850+
dir: "/stacks/stack-1/stack-1-a/subdir/dir",
851+
files: map[string]fmt.Stringer{
852+
"g.hcl": Doc(),
853+
},
854+
},
855+
{
856+
dir: "/stacks/stack-1/stack-1-a/subdir",
857+
files: map[string]fmt.Stringer{
858+
"f.hcl": Doc(),
859+
},
860+
},
861+
{
862+
dir: "/stacks/stack-1/stack-1-b",
863+
files: map[string]fmt.Stringer{
864+
"h.hcl": Doc(),
865+
},
866+
},
867+
{
868+
dir: "/stacks/stack-1/subdir",
869+
files: map[string]fmt.Stringer{
870+
"b.hcl": Doc(),
871+
},
872+
},
873+
{
874+
dir: "/stacks/stack-1/subdir/dir",
875+
files: map[string]fmt.Stringer{
876+
"c.hcl": Doc(),
877+
},
878+
},
879+
{
880+
dir: "/stacks/stack-2",
881+
files: map[string]fmt.Stringer{
882+
"d.hcl": Doc(),
883+
},
884+
},
885+
{
886+
dir: "/dir",
887+
files: map[string]fmt.Stringer{
888+
"orphan.hcl": Doc(),
889+
},
890+
},
891+
},
892+
},
802893
{
803894
name: "cleanup ignores dotdirs outside stacks",
804895
layout: []string{

0 commit comments

Comments
 (0)