From ce9996d4d5027343ff6dc0ecb40d6a266876b003 Mon Sep 17 00:00:00 2001 From: jeong-sik Date: Sat, 26 Sep 2026 08:30:16 +0900 Subject: [PATCH] Read the plugin request until end of input, not the first short read read_all stopped at the first input call that returned fewer than 1024 bytes. Stdlib.input may return fewer bytes than requested before the end of input; only 0 means end of file. On macOS a pipe hands the first read 512 bytes, so the CodeGeneratorRequest was cut and decoding failed with Premature_end_of_input. Read until input returns 0. Co-Authored-By: Claude Opus 5.5 (1M context) --- src/plugin/protoc_gen_ocaml.ml | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/plugin/protoc_gen_ocaml.ml b/src/plugin/protoc_gen_ocaml.ml index 3621546..d2b19c3 100644 --- a/src/plugin/protoc_gen_ocaml.ml +++ b/src/plugin/protoc_gen_ocaml.ml @@ -2,18 +2,21 @@ open StdLabels module Descriptor = Spec.Descriptor.Google.Protobuf module Plugin = Spec.Plugin.Google.Protobuf.Compiler +(* [input] may return fewer bytes than requested before the end of input; + only 0 means end of file. A pipe can hand over a short first chunk + (512 bytes on macOS), so stopping at the first short read would cut + the request that protoc sends. *) let read_all in_channel = - let rec inner buffer = - let b = Bytes.create 1024 in - match input in_channel b 0 1024 with - | 1024 -> - Buffer.add_bytes buffer b; - inner buffer + let buffer = Buffer.create 1024 in + let chunk = Bytes.create 1024 in + let rec inner () = + match input in_channel chunk 0 (Bytes.length chunk) with + | 0 -> Buffer.contents buffer | read -> - Buffer.add_subbytes buffer b 0 read; - Buffer.contents buffer + Buffer.add_subbytes buffer chunk 0 read; + inner () in - inner (Buffer.create 1024) + inner () (* Read from stdin *) let read () =