From 575150d686344840f32c706ec3879ebbe66b878c Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 01:19:44 +0000 Subject: [PATCH] fix(codegen): keep watch mode alive on missing input files Co-Authored-By: Virgil --- cmd/codegen/main.go | 11 +++++++++++ cmd/codegen/main_test.go | 28 ++++++++++++++++++++++++++++ docs/development.md | 2 ++ 3 files changed, 41 insertions(+) diff --git a/cmd/codegen/main.go b/cmd/codegen/main.go index 9681486..45ef2f7 100644 --- a/cmd/codegen/main.go +++ b/cmd/codegen/main.go @@ -88,6 +88,17 @@ func runDaemon(ctx context.Context, inputPath, outputPath string, emitTypes bool for { input, err := coreio.Local.Read(inputPath) if err != nil { + if os.IsNotExist(err) { + select { + case <-ctx.Done(): + if errors.Is(ctx.Err(), context.Canceled) { + return nil + } + return ctx.Err() + case <-ticker.C: + } + continue + } return log.E("codegen", "reading input file", err) } diff --git a/cmd/codegen/main_test.go b/cmd/codegen/main_test.go index e6262bb..d5c4837 100644 --- a/cmd/codegen/main_test.go +++ b/cmd/codegen/main_test.go @@ -149,6 +149,34 @@ func TestRunDaemon_RecoversFromInvalidJSON(t *testing.T) { require.NoError(t, <-done) } +func TestRunDaemon_RecoversFromMissingInputFile(t *testing.T) { + dir := t.TempDir() + inputPath := dir + "/slots.json" + outputPath := dir + "/bundle.js" + + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + done := make(chan error, 1) + go func() { + done <- runDaemon(ctx, inputPath, outputPath, false, 5*time.Millisecond) + }() + + time.Sleep(20 * time.Millisecond) + require.NoError(t, writeTestFile(inputPath, `{"H":"nav-bar","C":"main-content"}`)) + + require.Eventually(t, func() bool { + got, err := readTestFile(outputPath) + if err != nil { + return false + } + return strings.Contains(got, "NavBar") && strings.Contains(got, "MainContent") + }, time.Second, 10*time.Millisecond) + + cancel() + require.NoError(t, <-done) +} + func TestRunDaemon_MissingPaths(t *testing.T) { err := runDaemon(context.Background(), "", "", false, time.Millisecond) require.Error(t, err) diff --git a/docs/development.md b/docs/development.md index 46174bb..f99550d 100644 --- a/docs/development.md +++ b/docs/development.md @@ -151,6 +151,8 @@ To run the daemon mode, point it at an input JSON file and an output bundle path go run ./cmd/codegen/ -watch -input slots.json -output components.js ``` +Watch mode keeps polling through transient missing files and invalid JSON edits, then rewrites the output bundle once the input becomes valid again. + Add `-dts` to emit TypeScript declarations instead of JavaScript in either mode. To test the CLI: