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