Skip to content

Commit 31ee9a2

Browse files
committed
chore: fix issues from code review
1 parent b17ecf1 commit 31ee9a2

2 files changed

Lines changed: 12 additions & 14 deletions

File tree

internal/analysis/check.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ func findUnusedTemplates(ts *template.Template, executedTemplates map[string][]T
102102
usedTemplates[name] = true
103103
}
104104

105-
// Find all templates referenced from used templates (transitive closure)
106-
changed := true
107-
for changed {
108-
changed = false
109-
for name := range usedTemplates {
110-
t := ts.Lookup(name)
111-
if t == nil || t.Tree == nil {
112-
continue
113-
}
114-
}
115-
}
116-
117105
// Find unused templates (skip templates that are empty after define blocks are stripped)
118106
var unused []string
119107
for name := range allNames {

internal/cli/commands.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"cmp"
66
_ "embed"
7+
"errors"
78
"fmt"
89
"go/token"
910
"io"
@@ -111,11 +112,16 @@ func generateCommand(workingDirectory string, args []string, getEnv func(string)
111112

112113
// Write new files
113114
newGeneratedFiles := make(map[string]bool)
114-
for _, file := range files {
115+
for i, file := range files {
115116
var sb bytes.Buffer
116117
writeCodeGenerationComment(&sb, configToArgs(config))
117118
sb.WriteString(file.Content)
118119
if err := os.WriteFile(file.Path, sb.Bytes(), 0o644); err != nil {
120+
for _, f := range files[:i] {
121+
if rmErr := os.Remove(f.Path); rmErr != nil {
122+
err = errors.Join(err, rmErr)
123+
}
124+
}
119125
return err
120126
}
121127
newGeneratedFiles[file.Path] = true
@@ -161,7 +167,7 @@ func findGeneratedFiles(workingDirectory, routesFunction string) (map[string]boo
161167
filePath := filepath.Join(workingDirectory, entry.Name())
162168
content, err := os.ReadFile(filePath)
163169
if err != nil {
164-
continue
170+
return nil, fmt.Errorf("failed to read %s: %w", filePath, err)
165171
}
166172

167173
// Check if file has muxt generation comment
@@ -178,6 +184,10 @@ func findGeneratedFiles(workingDirectory, routesFunction string) (map[string]boo
178184
// Parse the routes function name from the generation comment
179185
// Only include files that match the current routes function name
180186
generatedRoutesFunc := parseRoutesFunctionFromComment(firstLine)
187+
if generatedRoutesFunc == "" {
188+
log.Println("WARNING: ignored generated file with empty TemplateRoutes identifier", filePath)
189+
continue
190+
}
181191
if generatedRoutesFunc == routesFunction {
182192
generatedFiles[filePath] = true
183193
}

0 commit comments

Comments
 (0)