Skip to content

Commit fa71086

Browse files
committed
feat(builder): add pre-clean step to proto generation to remove stale files
Introduce CleanXatuCBTProtos and CleanCBTAPIGenerated helpers that wipe generated proto, handler, server and OpenAPI artefacts before each regeneration. This prevents stale outputs from persisting when schemas or templates change, reducing hard-to-debug mismatches between source and generated code.
1 parent c94367f commit fa71086

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

pkg/builder/manager.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,32 @@ func (m *Manager) BuildLabFrontend(ctx context.Context) error {
224224
func (m *Manager) GenerateXatuCBTProtos(ctx context.Context) error {
225225
m.log.WithField("repo", "xatu-cbt").Info("generating protos")
226226

227+
// Clean generated proto files before regenerating to avoid stale files
228+
if err := m.CleanXatuCBTProtos(); err != nil {
229+
m.log.WithError(err).Warn("failed to clean xatu-cbt protos, continuing anyway")
230+
}
231+
227232
if err := m.runMake(ctx, m.cfg.Repos.XatuCBT, "proto"); err != nil {
228233
return fmt.Errorf("failed to generate xatu-cbt protos: %w", err)
229234
}
230235

231236
return nil
232237
}
233238

239+
// CleanXatuCBTProtos removes generated proto files from xatu-cbt.
240+
func (m *Manager) CleanXatuCBTProtos() error {
241+
m.log.WithField("repo", "xatu-cbt").Info("cleaning generated proto files")
242+
243+
// xatu-cbt generates protos to pkg/proto/clickhouse/
244+
protoDir := filepath.Join(m.cfg.Repos.XatuCBT, "pkg", "proto", "clickhouse")
245+
246+
if err := os.RemoveAll(protoDir); err != nil {
247+
return fmt.Errorf("failed to remove xatu-cbt proto directory: %w", err)
248+
}
249+
250+
return nil
251+
}
252+
234253
// GenerateProtos generates protobuf files for cbt-api.
235254
func (m *Manager) GenerateProtos(ctx context.Context) error {
236255
// Generate cbt-api protos (only for first network, they're network-agnostic)
@@ -242,6 +261,11 @@ func (m *Manager) GenerateProtos(ctx context.Context) error {
242261
"network": network.Name,
243262
}).Info("generating protos")
244263

264+
// Clean generated files before regenerating to avoid stale files
265+
if err := m.CleanCBTAPIGenerated(); err != nil {
266+
m.log.WithError(err).Warn("failed to clean cbt-api generated files, continuing anyway")
267+
}
268+
245269
// Use the generated config file
246270
configPath := filepath.Join(".xcli", "configs", fmt.Sprintf("cbt-api-%s.yaml", network.Name))
247271

@@ -262,6 +286,40 @@ func (m *Manager) GenerateProtos(ctx context.Context) error {
262286
return nil
263287
}
264288

289+
// CleanCBTAPIGenerated removes all generated files from cbt-api.
290+
// This includes proto files, handlers, server implementation, and OpenAPI specs.
291+
func (m *Manager) CleanCBTAPIGenerated() error {
292+
m.log.WithField("repo", "cbt-api").Info("cleaning generated files")
293+
294+
// Files and directories to remove (matches cbt-api Makefile clean target)
295+
toRemove := []string{
296+
// Proto generated directory
297+
filepath.Join(m.cfg.Repos.CBTAPI, "pkg", "proto", "clickhouse"),
298+
// Generated handler code
299+
filepath.Join(m.cfg.Repos.CBTAPI, "internal", "handlers", "generated.go"),
300+
// Generated server implementation
301+
filepath.Join(m.cfg.Repos.CBTAPI, "internal", "server", "implementation.go"),
302+
// Embedded OpenAPI spec
303+
filepath.Join(m.cfg.Repos.CBTAPI, "internal", "server", "openapi.yaml"),
304+
// Root OpenAPI spec
305+
filepath.Join(m.cfg.Repos.CBTAPI, "openapi.yaml"),
306+
// Proto descriptors
307+
filepath.Join(m.cfg.Repos.CBTAPI, ".descriptors.pb"),
308+
// Discovered tables cache
309+
filepath.Join(m.cfg.Repos.CBTAPI, ".tables.txt"),
310+
// Temp directory
311+
filepath.Join(m.cfg.Repos.CBTAPI, "tmp"),
312+
}
313+
314+
for _, path := range toRemove {
315+
if err := os.RemoveAll(path); err != nil {
316+
m.log.WithError(err).WithField("path", path).Warn("failed to remove generated file")
317+
}
318+
}
319+
320+
return nil
321+
}
322+
265323
// binaryExists checks if a binary file exists.
266324
func (m *Manager) binaryExists(path string) bool {
267325
info, err := os.Stat(path)

0 commit comments

Comments
 (0)