Subject: [PATCH] Don't create nested target directories automatically

From: Andrew Chambers <[email-masked]>
Date: Fri, 15 Apr 2022 12:55:59 +1200

If a user mistypes a rule, goredo creates those directories automatically
leaving junk on the filesystem. This change means higher level rules must
create those directories explicitly if needed.
---
 run.go | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/run.go b/run.go
index 32e3800..1a657a3 100644
--- a/run.go
+++ b/run.go
@@ -123,13 +123,6 @@ func (e RunError) Error() string {
 	return fmt.Sprintf("%s: %s", e.Name(), e.Err)
 }
 
-func mkdirs(pth string) error {
-	if _, err := os.Stat(pth); err == nil {
-		return nil
-	}
-	return os.MkdirAll(pth, os.FileMode(0777))
-}
-
 func isModified(cwd, redoDir, tgt string) (bool, *Inode, string, error) {
 	fdDep, err := os.Open(path.Join(redoDir, tgt+DepSuffix))
 	if err != nil {
@@ -190,7 +183,11 @@ func syncDir(dir string) error {
 func runScript(tgtOrig string, errs chan error, traced bool) error {
 	cwd, tgt := cwdAndTgt(tgtOrig)
 	redoDir := path.Join(cwd, RedoDir)
-	if err := mkdirs(redoDir); err != nil {
+
+	if err := os.Mkdir(redoDir, os.FileMode(0777)); err != nil && !os.IsExist(err) {
+		if os.IsNotExist(err) {
+			return TgtError{tgtOrig, fmt.Errorf("target directory does not exist")}
+		}
 		return TgtError{tgtOrig, err}
 	}
 
-- 
2.33.1