diff --git a/CHANGELOG.md b/CHANGELOG.md
index fcfb20ccd..cf21dc65e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -26,6 +26,10 @@ Given a version number `MAJOR.MINOR.PATCH`, we increment the:
- Add `terramate create --wants ... --wanted-by ...` flags for configuring the `stack.wants` and `stack.wanted_by` attributes, respectively.
+### Fixed
+
+- Fix the cleaning up of orphaned files in the `terramate generate` to respect the `-C
` flag.
+
## v0.10.6
### Fixed
diff --git a/generate/generate.go b/generate/generate.go
index 1e4c36aac..59ee56f1f 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -194,7 +194,7 @@ func Do(
stackReport := doStackGeneration(root, tree, vendorDir, vendorRequests)
rootReport := doRootGeneration(root, tree)
report := mergeReports(stackReport, rootReport)
- return cleanupOrphaned(root, report)
+ return cleanupOrphaned(root, tree, report)
}
func doStackGeneration(
@@ -1339,21 +1339,23 @@ func loadStackCodeCfgs(
return genfilesConfigs, nil
}
-func cleanupOrphaned(root *config.Root, report Report) Report {
+func cleanupOrphaned(root *config.Root, target *config.Tree, report Report) Report {
logger := log.With().
Str("action", "generate.cleanupOrphaned()").
+ Stringer("dir", target.Dir()).
Logger()
- // If the root of the tree is a stack then there is nothing to do
- // since there can't be any orphans (the root parent stack owns
- // the entire project).
- if root.Tree().IsStack() {
- logger.Debug().Msg("project root is a stack, nothing to do")
+
+ defer report.sort()
+
+ // If the target tree is a stack then there is nothing to do
+ // as it was already generated at this point.
+ if target.IsStack() {
return report
}
logger.Debug().Msg("listing orphaned generated files")
- orphanedGenFiles, err := ListStackGenFiles(root, root.HostDir())
+ orphanedGenFiles, err := ListStackGenFiles(root, target.HostDir())
if err != nil {
report.CleanupErr = err
return report
@@ -1363,8 +1365,8 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
deleteFailures := map[project.Path]*errors.List{}
for _, genfile := range orphanedGenFiles {
- genfileAbspath := filepath.Join(root.HostDir(), genfile)
- dir := project.NewPath("/" + filepath.ToSlash(filepath.Dir(genfile)))
+ genfileAbspath := filepath.Join(target.HostDir(), genfile)
+ dir := project.PrjAbsPath(root.HostDir(), filepath.Dir(genfileAbspath))
if err := os.Remove(genfileAbspath); err != nil {
if deleteFailures[dir] == nil {
deleteFailures[dir] = errors.L()
@@ -1402,7 +1404,5 @@ func cleanupOrphaned(root *config.Root, report Report) Report {
Deleted: deletedFiles,
})
}
-
- report.sort()
return report
}
diff --git a/generate/generate_test.go b/generate/generate_test.go
index 6fd43b3e9..65870d207 100644
--- a/generate/generate_test.go
+++ b/generate/generate_test.go
@@ -799,6 +799,97 @@ func TestGenerateCleanup(t *testing.T) {
},
},
},
+ {
+ name: "workdir is respected in cleanup",
+ layout: []string{
+ "s:stacks/stack-1",
+ "s:stacks/stack-2",
+ "s:stacks/stack-1/stack-1-a",
+ "s:stacks/stack-1/stack-1-b",
+ "s:stacks/stack-3", // only files here will be cleaned up.
+ genfile("dir/orphan.hcl"),
+ genfile("stacks/stack-1/a.hcl"),
+ genfile("stacks/stack-1/subdir/b.hcl"),
+ genfile("stacks/stack-1/subdir/dir/c.hcl"),
+ genfile("stacks/stack-1/stack-1-a/e.hcl"),
+ genfile("stacks/stack-1/stack-1-a/subdir/f.hcl"),
+ genfile("stacks/stack-1/stack-1-a/subdir/dir/g.hcl"),
+ genfile("stacks/stack-1/stack-1-b/h.hcl"),
+ genfile("stacks/stack-2/d.hcl"),
+ genfile("stacks/stack-3/x.hcl"),
+ genfile("stacks/stack-3/subdir/y.hcl"),
+ genfile("stacks/stack-3/subdir/dir/z.hcl"),
+ },
+ fromdir: "/stacks/stack-3",
+ wantReport: generate.Report{
+ Successes: []generate.Result{
+ {
+ Dir: project.NewPath("/stacks/stack-3"),
+ Deleted: []string{
+ "subdir/dir/z.hcl",
+ "subdir/y.hcl",
+ "x.hcl",
+ },
+ },
+ },
+ },
+ want: []generatedFile{
+ {
+ dir: "/stacks/stack-1",
+ files: map[string]fmt.Stringer{
+ "a.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/stack-1-a",
+ files: map[string]fmt.Stringer{
+ "e.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/stack-1-a/subdir/dir",
+ files: map[string]fmt.Stringer{
+ "g.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/stack-1-a/subdir",
+ files: map[string]fmt.Stringer{
+ "f.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/stack-1-b",
+ files: map[string]fmt.Stringer{
+ "h.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/subdir",
+ files: map[string]fmt.Stringer{
+ "b.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-1/subdir/dir",
+ files: map[string]fmt.Stringer{
+ "c.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/stacks/stack-2",
+ files: map[string]fmt.Stringer{
+ "d.hcl": Doc(),
+ },
+ },
+ {
+ dir: "/dir",
+ files: map[string]fmt.Stringer{
+ "orphan.hcl": Doc(),
+ },
+ },
+ },
+ },
{
name: "cleanup ignores dotdirs outside stacks",
layout: []string{