From bd31f22a5d8da4e41cba89db1e0d1e1d973f54e9 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Sat, 15 Aug 2026 17:12:34 -0500 Subject: [PATCH 1/2] Use Buf to guard protobuf compatibility --- .github/workflows/ci.yaml | 7 ++ elasticgraph-proto_ingestion/README.md | 22 ++++ .../buf_breaking_change_detector.rb | 99 +++++++++++++++ .../schema_artifact_manager_extension.rb | 52 +++++++- .../buf_breaking_change_detector.rbs | 23 ++++ .../schema_artifact_manager_extension.rbs | 6 + .../schema_definition/rake_tasks_spec.rb | 109 ++++++++++++++++ .../buf_breaking_change_detector_spec.rb | 119 ++++++++++++++++++ .../readme_snippets/ruby_snippet_validator.rb | 11 ++ 9 files changed, 447 insertions(+), 1 deletion(-) create mode 100644 elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb create mode 100644 elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rbs create mode 100644 elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fd22bf8e9..bc7089d19 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -91,6 +91,13 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true # runs 'bundle install' and caches installed gems automatically + - name: Setup Buf + if: matrix.build_part == 'run_misc_checks' + uses: bufbuild/buf-action@8c6a16e16f12ba20b6470afa9c2ba9b5ba8c97c3 # v1.5.0 + with: + version: "1.72.0" + setup_only: true + - name: Setup Docker Compose uses: KengoTODA/actions-setup-docker-compose@b09fb2b902df45b91982bc46858188f2a714ee5f # v1.2.11 env: diff --git a/elasticgraph-proto_ingestion/README.md b/elasticgraph-proto_ingestion/README.md index 41ffe55bf..4f7e0df6c 100644 --- a/elasticgraph-proto_ingestion/README.md +++ b/elasticgraph-proto_ingestion/README.md @@ -84,6 +84,28 @@ end After running `bundle exec rake schema_artifacts:dump`, ElasticGraph will generate a `schema.proto` schema artifact, and will maintain a `proto_field_numbers.yaml` file alongside your schema definition. +### Protecting Protobuf Compatibility With Buf + +Install the [Buf CLI](https://buf.build/docs/installation/) anywhere that dumps schema artifacts. +Once `schema.proto` exists, ElasticGraph compiles the existing and proposed schemas with Buf whenever +the artifact changes. It then applies Buf's strict [`FILE` breaking-change +rules](https://buf.build/docs/breaking/rules/) so changes that break generated code, JSON, or the binary +wire format cannot be dumped accidentally. Imported schemas are compiled using the project's normal +Buf configuration and are excluded from the comparison itself, keeping this check scoped to the +generated `schema.proto`. + +A breaking change always fails the dump. Protobuf has no schema version to bump: consumers that +already deserialize these messages would misread them, and you cannot update every consumer at the +same moment. So there is no flag to accept a breaking change. + +To get past the error, make the change compatible: + +- Add a new field instead of a change to the type or the name of an existing field. +- Reserve the number of every field you remove. +- Keep enum value numbers stable, and reserve the numbers of values you remove. + +Compatible additions dump normally and need no extra step. + ## Schema Definition API ### Protobuf Syntax (`proto2` / `proto3`) diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb new file mode 100644 index 000000000..a859fa89a --- /dev/null +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb @@ -0,0 +1,99 @@ +# Copyright 2024 - 2026 Block, Inc. +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. +# +# frozen_string_literal: true + +require "elastic_graph/errors" +require "elastic_graph/proto_ingestion" +require "open3" +require "tempfile" + +module ElasticGraph + module ProtoIngestion + module SchemaDefinition + # Uses the Buf CLI to detect breaking changes between two protobuf schemas. + # + # @private + class BufBreakingChangeDetector + BREAKING_CONFIG_JSON = %({"version":"v2","breaking":{"use":["FILE"]}}) + + def initialize(buf_command: "buf", temporary_directory: ::Dir.pwd) + @buf_command = buf_command + @temporary_directory = temporary_directory + end + + # Returns Buf's diagnostics if the current schema breaks the prior schema, or `nil` if it + # is compatible. + def breaking_changes(current_schema:, against_schema:) + with_built_images(current_schema, against_schema) do |current_image_path, against_image_path, proto_path| + output, status = run_buf( + "breaking", + current_image_path, + "--against", + against_image_path, + "--exclude-imports", + "--config", + BREAKING_CONFIG_JSON + ) + + return nil if status.success? + return normalized_output(output, proto_path) if status.exitstatus == 100 + + raise Errors::SchemaError, buf_failure_message("compare the protobuf schemas", output) + end + rescue ::Errno::ENOENT + raise Errors::SchemaError, <<~EOS.strip + The Buf CLI is required to check `schema.proto` for breaking changes, but the `#{@buf_command}` command could not be found. + Install Buf from https://buf.build/docs/installation and run `bundle exec rake schema_artifacts:dump` again. + EOS + end + + private + + def with_built_images(current_schema, against_schema) + ::Tempfile.create(["elasticgraph-schema", ".proto"], @temporary_directory) do |proto_file| + ::Tempfile.create(["elasticgraph-current-schema", ".binpb"], @temporary_directory) do |current_image_file| + build_image(current_schema, "current", proto_file, current_image_file) + + ::Tempfile.create(["elasticgraph-against-schema", ".binpb"], @temporary_directory) do |against_image_file| + build_image(against_schema, "against", proto_file, against_image_file) + yield current_image_file.path, against_image_file.path, proto_file.path + end + end + end + end + + def build_image(schema, label, proto_file, image_file) + proto_file.rewind + proto_file.truncate(0) + proto_file.write(schema) + proto_file.flush + image_file.close + + output, status = run_buf("build", proto_file.path, "--output", image_file.path) + return if status.success? + + raise Errors::SchemaError, buf_failure_message("compile the #{label} protobuf schema", output) + end + + def run_buf(*arguments) + stdout, stderr, status = ::Open3.capture3(@buf_command, *arguments) + output = [stdout, stderr].reject(&:empty?).join("\n").strip + [output, status] + end + + def buf_failure_message(action, output) + details = output.empty? ? "Buf did not provide any diagnostics." : output + "Buf was unable to #{action}:\n\n#{details}" + end + + def normalized_output(output, proto_path) + output.gsub(proto_path, PROTO_SCHEMA_FILE).gsub(::File.basename(proto_path), PROTO_SCHEMA_FILE) + end + end + end + end +end diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb index a0600b951..5ced0e293 100644 --- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb @@ -8,6 +8,7 @@ require "elastic_graph/errors" require "elastic_graph/proto_ingestion" +require "elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector" module ElasticGraph module ProtoIngestion @@ -31,6 +32,24 @@ module SchemaArtifactManagerExtension "lost, and previously serialized protobuf messages could be misread." ].freeze + # Overrides `dump_artifacts` to reject any change that breaks protobuf wire compatibility. + # + # A protobuf schema has no version to bump. Every consumer that already deserializes these + # messages must keep reading them, and those consumers cannot be updated in lockstep. So a + # breaking change is always an error, and the only fix is to make the change compatible. + def dump_artifacts + proto_schema = protobuf_schema_definition_results.proto_schema + return super if proto_schema.empty? + + artifact = proto_schema_artifact + existing_schema = artifact.existing_dumped_contents + return super unless artifact.out_of_date? && existing_schema + + check_for_breaking_proto_changes(artifact.desired_contents, existing_schema) + + super + end + private # Overrides the base `artifacts_from_schema_def` method to add proto artifacts. @@ -41,10 +60,41 @@ def artifacts_from_schema_def base_artifacts + [ proto_field_numbers_artifact, - new_raw_artifact(PROTO_SCHEMA_FILE, proto_schema.chomp, comment_prefix: "//") + proto_schema_artifact ] end + def proto_schema_artifact + @proto_schema_artifact ||= new_raw_artifact( + PROTO_SCHEMA_FILE, + protobuf_schema_definition_results.proto_schema.chomp, + comment_prefix: "//" + ) + end + + def check_for_breaking_proto_changes(current_schema, against_schema) + changes = BufBreakingChangeDetector.new( + temporary_directory: ::File.dirname(proto_schema_artifact.file_name) + ).breaking_changes( + current_schema: current_schema, + against_schema: against_schema + ) + return unless changes + + abort <<~EOS.strip + Buf detected a breaking change to `schema.proto`: + + #{changes} + + Protobuf offers no way to version your way out of this. Consumers that already read + these messages would misread them after this change. Make the change compatible + instead: add a new field rather than retype or rename an existing one, and reserve the + number of every field you remove. + EOS + rescue Errors::SchemaError => e + abort e.message + end + # Builds the `proto_field_numbers.yaml` artifact. The file is part of the schema definition # rather than a proper schema artifact--it's an input to `schema.proto` generation--so it # lives alongside `path_to_schema` instead of in the schema artifacts directory. diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rbs new file mode 100644 index 000000000..999f848e3 --- /dev/null +++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rbs @@ -0,0 +1,23 @@ +module ElasticGraph + module ProtoIngestion + module SchemaDefinition + class BufBreakingChangeDetector + BREAKING_CONFIG_JSON: ::String + + @buf_command: ::String + @temporary_directory: ::String + + def initialize: (?buf_command: ::String, ?temporary_directory: ::String) -> void + def breaking_changes: (current_schema: ::String, against_schema: ::String) -> (::String | nil) + + private + + def with_built_images: (::String current_schema, ::String against_schema) { (::String, ::String, ::String) -> untyped } -> untyped + def build_image: (::String schema, ::String label, ::File proto_file, ::File image_file) -> void + def run_buf: (*::String arguments) -> [::String, ::Process::Status] + def buf_failure_message: (::String action, ::String output) -> ::String + def normalized_output: (::String, ::String) -> ::String + end + end + end +end diff --git a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rbs b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rbs index 120aa8962..9f07c9151 100644 --- a/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rbs +++ b/elasticgraph-proto_ingestion/sig/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rbs @@ -4,9 +4,15 @@ module ElasticGraph module SchemaArtifactManagerExtension : ::ElasticGraph::SchemaDefinition::SchemaArtifactManager PROTO_FIELD_NUMBERS_COMMENT_PREAMBLE_LINES: ::Array[::String] + @proto_schema_artifact: (::ElasticGraph::SchemaDefinition::SchemaArtifact[::String] | nil) + + def dump_artifacts: () -> void + private def artifacts_from_schema_def: () -> ::Array[::ElasticGraph::SchemaDefinition::SchemaArtifact[untyped]] + def proto_schema_artifact: () -> ::ElasticGraph::SchemaDefinition::SchemaArtifact[::String] + def check_for_breaking_proto_changes: (::String, ::String) -> void def proto_field_numbers_artifact: () -> ::ElasticGraph::SchemaDefinition::SchemaArtifact[::Hash[::String, untyped]] def proto_field_numbers_path: () -> ::String def protobuf_schema_definition_results: () -> (::ElasticGraph::SchemaDefinition::Results & ResultsExtension) diff --git a/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb b/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb index f4f8c540d..357daaf4c 100644 --- a/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb +++ b/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb @@ -38,6 +38,25 @@ module SchemaDefinition ) end + it "does not invoke Buf or dump a proto artifact when no indexed types are defined" do + expect(BufBreakingChangeDetector).not_to receive(:new) + write_proto_schema(table_defs: <<~EOS) + s.object_type "Point" do |t| + t.field "x", "Float" + end + + s.on_root_query_type do |t| + t.field "point", "Point" do |f| + f.resolve_with :object_without_lookahead + end + end + EOS + + run_rake_with_proto("schema_artifacts:dump") + + expect(read_artifact(PROTO_SCHEMA_FILE)).to be_nil + end + it "idempotently dumps proto artifacts" do write_proto_schema(table_defs: <<~EOS) s.object_type "Product" do |t| @@ -55,6 +74,8 @@ module SchemaDefinition end it "persists proto field-number mappings and reuses them on the next dump" do + stub_buf_breaking_changes(nil) + write_proto_schema(table_defs: <<~EOS) s.object_type "Product" do |t| t.field "id", "ID" @@ -97,6 +118,88 @@ module SchemaDefinition expect(read_artifact(PROTO_SCHEMA_FILE)).to include("string id = 1;", "string name = 2;") end + + it "dumps a compatible change that Buf reports as safe" do + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.index "products" + end + EOS + run_rake_with_proto("schema_artifacts:dump") + + detector = stub_buf_breaking_changes(nil) + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "name", "String" + t.index "products" + end + EOS + + expect { + run_rake_with_proto("schema_artifacts:dump") + }.to change { read_artifact(PROTO_SCHEMA_FILE) } + + expect(detector).to have_received(:breaking_changes).with( + current_schema: a_string_including("string name = 2;"), + against_schema: a_string_excluding("string name = 2;") + ) + end + + it "refuses to dump a breaking change reported by Buf" do + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.index "products" + end + EOS + run_rake_with_proto("schema_artifacts:dump") + + original_proto = read_artifact(PROTO_SCHEMA_FILE) + stub_buf_breaking_changes("schema.proto: Field changed type from string to int32.") + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "Int" + t.index "products" + end + EOS + + expect { + run_rake_with_proto("schema_artifacts:dump") + }.to abort_with a_string_including( + "Buf detected a breaking change", + "Field changed type from string to int32", + "Protobuf offers no way to version your way out of this", + "reserve the" + ) + expect(read_artifact(PROTO_SCHEMA_FILE)).to eq(original_proto) + end + + it "surfaces Buf failures without a stack trace" do + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.index "products" + end + EOS + run_rake_with_proto("schema_artifacts:dump") + + detector = instance_double(BufBreakingChangeDetector) + allow(detector).to receive(:breaking_changes).and_raise(Errors::SchemaError, "Buf could not compile an import.") + allow(BufBreakingChangeDetector).to receive(:new).and_return(detector) + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "name", "String" + t.index "products" + end + EOS + + expect { + run_rake_with_proto("schema_artifacts:dump") + }.to abort_with("Buf could not compile an import.") + end end describe "schema_artifacts:check" do @@ -130,6 +233,12 @@ def write_proto_schema(table_defs:) EOS end + def stub_buf_breaking_changes(changes) + detector = instance_double(BufBreakingChangeDetector, breaking_changes: changes) + allow(BufBreakingChangeDetector).to receive(:new).and_return(detector) + detector + end + def run_rake_with_proto(*args) run_rake(*args) do |output| ElasticGraph::SchemaDefinition::RakeTasks.new( diff --git a/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb new file mode 100644 index 000000000..1322d085d --- /dev/null +++ b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb @@ -0,0 +1,119 @@ +# Copyright 2024 - 2026 Block, Inc. +# +# Use of this source code is governed by an MIT-style +# license that can be found in the LICENSE file or at +# https://opensource.org/licenses/MIT. +# +# frozen_string_literal: true + +require "elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector" + +module ElasticGraph + module ProtoIngestion + module SchemaDefinition + RSpec.describe BufBreakingChangeDetector do + it "builds both schemas at one logical path and uses Buf's FILE rules to detect breaking changes" do + calls = stub_buf( + build_results: [["", successful_status], ["", successful_status]], + breaking_result: ["schema.proto: Field changed type.", failed_status(100)] + ) + + changes = described_class.new.breaking_changes( + current_schema: %(syntax = "proto3";), + against_schema: %(syntax = "proto2";) + ) + + expect(changes).to eq("schema.proto: Field changed type.") + expect(calls.map { |arguments, _| arguments.fetch(1) }).to eq(["build", "build", "breaking"]) + expect(calls.first.first.fetch(2)).to start_with(::Dir.pwd) + expect(calls.first.first.fetch(2)).to eq(calls.fetch(1).first.fetch(2)) + expect(calls.last.first).to include( + "--exclude-imports", + "--config", + described_class::BREAKING_CONFIG_JSON + ) + end + + it "returns nil when Buf finds no breaking changes" do + stub_buf( + build_results: [["", successful_status], ["", successful_status]], + breaking_result: ["", successful_status] + ) + + expect(described_class.new.breaking_changes(current_schema: "current", against_schema: "against")).to be_nil + end + + it "raises a clear error when Buf cannot compile a schema" do + stub_buf(build_results: [["invalid proto", failed_status(1)]]) + + expect { + described_class.new.breaking_changes(current_schema: "current", against_schema: "against") + }.to raise_error Errors::SchemaError, a_string_including( + "compile the current protobuf schema", + "invalid proto" + ) + end + + it "identifies which prior schema failed to compile even when Buf provides no diagnostics" do + stub_buf(build_results: [["", successful_status], ["", failed_status(1)]]) + + expect { + described_class.new.breaking_changes(current_schema: "current", against_schema: "against") + }.to raise_error Errors::SchemaError, a_string_including( + "compile the against protobuf schema", + "Buf did not provide any diagnostics." + ) + end + + it "raises a clear error when the breaking check itself fails" do + stub_buf( + build_results: [["", successful_status], ["", successful_status]], + breaking_result: ["bad config", failed_status(1)] + ) + + expect { + described_class.new.breaking_changes(current_schema: "current", against_schema: "against") + }.to raise_error Errors::SchemaError, a_string_including("compare the protobuf schemas", "bad config") + end + + it "raises an actionable error when Buf is not installed" do + allow(::Open3).to receive(:capture3).and_raise(::Errno::ENOENT) + + expect { + described_class.new.breaking_changes(current_schema: "current", against_schema: "against") + }.to raise_error Errors::SchemaError, a_string_including( + "Buf CLI is required", + "https://buf.build/docs/installation" + ) + end + + def stub_buf(build_results:, breaking_result: nil) + calls = [] + remaining_build_results = build_results.dup + + allow(::Open3).to receive(:capture3) do |*arguments, **options| + calls << [arguments, options] + stdout_and_status = if arguments.fetch(1) == "build" + remaining_build_results.shift + else + breaking_result + end + + output, status = stdout_and_status + [output, "", status] + end + + calls + end + + def successful_status + instance_double(::Process::Status, success?: true, exitstatus: 0) + end + + def failed_status(exitstatus) + instance_double(::Process::Status, success?: false, exitstatus: exitstatus) + end + end + end + end +end diff --git a/script/readme_snippets/ruby_snippet_validator.rb b/script/readme_snippets/ruby_snippet_validator.rb index da42d7ac3..ff61902a0 100644 --- a/script/readme_snippets/ruby_snippet_validator.rb +++ b/script/readme_snippets/ruby_snippet_validator.rb @@ -14,6 +14,10 @@ class RubySnippetValidator < SnippetValidator # Constants for Ruby snippet validation RACK_TIMEOUT_SECONDS = 10 + # Artifacts a previous snippet may have dumped into the shared temp project. + SCHEMA_ARTIFACTS_DIRECTORY = "config/schema/artifacts" + PROTO_FIELD_NUMBERS_GLOB = "config/**/proto_field_numbers.yaml" + # Rack config detection patterns RACK_SUCCESS_INDICATORS = [ "Listening on", @@ -91,6 +95,13 @@ def schema_definition?(snippet, file_path) end def dump_artifacts + # Each snippet is an independent example, not the next step of one evolving schema. The + # snippets share a temp project, so artifacts dumped by an earlier snippet must be discarded + # first. Otherwise a snippet gets compared against an unrelated schema--for example, the + # protobuf compatibility check reads the previous snippet's `schema.proto` as its baseline. + FileUtils.rm_rf(SCHEMA_ARTIFACTS_DIRECTORY) + FileUtils.rm_f(Dir.glob(PROTO_FIELD_NUMBERS_GLOB)) + output = `bundle exec rake schema_artifacts:dump 2>&1` [$?.success?, output] end From d54e53f8e8a98903a3d707727e648cae9c7eab23 Mon Sep 17 00:00:00 2001 From: Josh Wilson Date: Fri, 20 Nov 2026 21:42:15 -0600 Subject: [PATCH 2/2] Check protobuf compatibility with Buf's WIRE rules Buf's FILE rules also protect generated source code and JSON names, so they rejected removing a field, `renamed_from`, type renames, and `int32` to `int64` widening. With no escape hatch, a schema could never drop or rename a field once `schema.proto` existed. The indexer reads binary messages, so the WIRE rules cover what must stay compatible. Add specs that run the real Buf CLI to pin which changes pass and fail, install Buf for every CI job so they run, and update the README and error guidance. Co-authored-by: Josh Wilson Signed-off-by: Josh Wilson --- .github/workflows/ci.yaml | 2 +- elasticgraph-proto_ingestion/README.md | 34 +++++---- .../buf_breaking_change_detector.rb | 7 +- .../schema_artifact_manager_extension.rb | 4 +- .../schema_definition/rake_tasks_spec.rb | 57 ++++++++++++++- .../spec/spec_helper.rb | 11 +++ .../buf_breaking_change_detector_spec.rb | 70 ++++++++++++++++++- 7 files changed, 166 insertions(+), 19 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index bc7089d19..1f33c4f4c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -91,8 +91,8 @@ jobs: ruby-version: ${{ matrix.ruby }} bundler-cache: true # runs 'bundle install' and caches installed gems automatically + # Buf is needed both for README snippet validation and for the specs that exercise the real Buf CLI. - name: Setup Buf - if: matrix.build_part == 'run_misc_checks' uses: bufbuild/buf-action@8c6a16e16f12ba20b6470afa9c2ba9b5ba8c97c3 # v1.5.0 with: version: "1.72.0" diff --git a/elasticgraph-proto_ingestion/README.md b/elasticgraph-proto_ingestion/README.md index 4f7e0df6c..bf8ba24af 100644 --- a/elasticgraph-proto_ingestion/README.md +++ b/elasticgraph-proto_ingestion/README.md @@ -88,23 +88,31 @@ schema artifact, and will maintain a `proto_field_numbers.yaml` file alongside y Install the [Buf CLI](https://buf.build/docs/installation/) anywhere that dumps schema artifacts. Once `schema.proto` exists, ElasticGraph compiles the existing and proposed schemas with Buf whenever -the artifact changes. It then applies Buf's strict [`FILE` breaking-change -rules](https://buf.build/docs/breaking/rules/) so changes that break generated code, JSON, or the binary -wire format cannot be dumped accidentally. Imported schemas are compiled using the project's normal -Buf configuration and are excluded from the comparison itself, keeping this check scoped to the -generated `schema.proto`. +the artifact changes. It then applies Buf's [`WIRE` breaking-change rules](https://buf.build/docs/breaking/rules/), +which reject changes that would make the indexer misread binary messages that publishers have +already serialized. Imported schemas are compiled using the project's normal Buf configuration and +are excluded from the comparison itself, keeping this check scoped to the generated `schema.proto`. -A breaking change always fails the dump. Protobuf has no schema version to bump: consumers that -already deserialize these messages would misread them, and you cannot update every consumer at the -same moment. So there is no flag to accept a breaking change. +The `WIRE` rules allow routine schema evolution: + +- Adding fields, types, and enum values. +- Removing fields and enum values. ElasticGraph keeps their numbers in `proto_field_numbers.yaml` and + emits `reserved` statements for them, so their numbers are never reused. +- Renaming fields with `renamed_from`. The field keeps its number. +- Wire-compatible type changes, such as `Int` to `JsonSafeLong` (`int32` to `int64`). -To get past the error, make the change compatible: +They reject changes such as: -- Add a new field instead of a change to the type or the name of an existing field. -- Reserve the number of every field you remove. -- Keep enum value numbers stable, and reserve the numbers of values you remove. +- Changing a field to a wire-incompatible type, such as `String` to `Int` (`string` to `int32`). +- Changing a field between a single value and a list. +- Changing the protobuf package. +- Restoring a field or enum value that was removed. ElasticGraph would reassign its old number, + which removes the `reserved` statement that protects it. -Compatible additions dump normally and need no extra step. +A breaking change always fails the dump. Protobuf has no schema version to bump: consumers that +already deserialize these messages would misread them, and you cannot update every consumer at the +same moment. So there is no flag to accept a breaking change. Instead, add a field with a new name, +and use a new name rather than restoring a removed field or enum value. ## Schema Definition API diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb index a859fa89a..166bbf998 100644 --- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector.rb @@ -18,7 +18,12 @@ module SchemaDefinition # # @private class BufBreakingChangeDetector - BREAKING_CONFIG_JSON = %({"version":"v2","breaking":{"use":["FILE"]}}) + # ElasticGraph publishers and the indexer exchange binary protobuf messages, so Buf's `WIRE` + # rules check exactly what must stay compatible: field numbers, wire types, cardinality, and + # reservations. The stricter `FILE`, `PACKAGE`, and `WIRE_JSON` categories also protect generated + # source code or protobuf JSON names. They reject routine schema evolution, such as removing a + # field or using `renamed_from`, without protecting anything the indexer reads. + BREAKING_CONFIG_JSON = %({"version":"v2","breaking":{"use":["WIRE"]}}) def initialize(buf_command: "buf", temporary_directory: ::Dir.pwd) @buf_command = buf_command diff --git a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb index 5ced0e293..6ad847c76 100644 --- a/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb +++ b/elasticgraph-proto_ingestion/lib/elastic_graph/proto_ingestion/schema_definition/schema_artifact_manager_extension.rb @@ -88,8 +88,8 @@ def check_for_breaking_proto_changes(current_schema, against_schema) Protobuf offers no way to version your way out of this. Consumers that already read these messages would misread them after this change. Make the change compatible - instead: add a new field rather than retype or rename an existing one, and reserve the - number of every field you remove. + instead: add a field with a new name rather than change the type or list depth of an + existing field, and use a new name rather than restore a field that was removed. EOS rescue Errors::SchemaError => e abort e.message diff --git a/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb b/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb index 357daaf4c..a82c9ebaf 100644 --- a/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb +++ b/elasticgraph-proto_ingestion/spec/integration/elastic_graph/proto_ingestion/schema_definition/rake_tasks_spec.rb @@ -171,7 +171,7 @@ module SchemaDefinition "Buf detected a breaking change", "Field changed type from string to int32", "Protobuf offers no way to version your way out of this", - "reserve the" + "use a new name rather than restore a field" ) expect(read_artifact(PROTO_SCHEMA_FILE)).to eq(original_proto) end @@ -200,6 +200,61 @@ module SchemaDefinition run_rake_with_proto("schema_artifacts:dump") }.to abort_with("Buf could not compile an import.") end + + context "with the real Buf CLI", :requires_buf do + it "dumps a field removal and reserves the removed field's number" do + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "name", "String" + t.field "price", "Int" + t.index "products" + end + EOS + run_rake_with_proto("schema_artifacts:dump") + + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "price", "Int" + t.index "products" + end + EOS + + expect { + run_rake_with_proto("schema_artifacts:dump") + }.to change { read_artifact(PROTO_SCHEMA_FILE) } + .to(a_string_including("reserved 2; // Previously used by name.").and(excluding("string name = 2;"))) + end + + it "refuses to dump a wire-incompatible type change" do + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "price", "Int" + t.index "products" + end + EOS + run_rake_with_proto("schema_artifacts:dump") + original_proto = read_artifact(PROTO_SCHEMA_FILE) + + write_proto_schema(table_defs: <<~EOS) + s.object_type "Product" do |t| + t.field "id", "ID" + t.field "price", "String" + t.index "products" + end + EOS + + expect { + run_rake_with_proto("schema_artifacts:dump") + }.to abort_with a_string_including( + "Buf detected a breaking change", + 'Field "2" with name "price" on message "Product" changed type from "int32" to "string".' + ) + expect(read_artifact(PROTO_SCHEMA_FILE)).to eq(original_proto) + end + end end describe "schema_artifacts:check" do diff --git a/elasticgraph-proto_ingestion/spec/spec_helper.rb b/elasticgraph-proto_ingestion/spec/spec_helper.rb index 39286580a..cb35527d0 100644 --- a/elasticgraph-proto_ingestion/spec/spec_helper.rb +++ b/elasticgraph-proto_ingestion/spec/spec_helper.rb @@ -17,4 +17,15 @@ config.when_first_matching_example_defined(:proto_schema) do require "support/proto_schema_support" end + + # Examples tagged with `:requires_buf` run the real Buf CLI, and are skipped where it is not installed. + config.before(:example, :requires_buf) do + require "open3" + _, _, status = ::Open3.capture3("buf", "--version") + # simplecov:disable -- CI installs Buf, so the skips are only reached on machines without it. + skip "Buf CLI is not installed" unless status.success? + rescue ::Errno::ENOENT + skip "Buf CLI is not installed" + # simplecov:enable + end end diff --git a/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb index 1322d085d..6f4362833 100644 --- a/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb +++ b/elasticgraph-proto_ingestion/spec/unit/elastic_graph/proto_ingestion/schema_definition/buf_breaking_change_detector_spec.rb @@ -12,7 +12,7 @@ module ElasticGraph module ProtoIngestion module SchemaDefinition RSpec.describe BufBreakingChangeDetector do - it "builds both schemas at one logical path and uses Buf's FILE rules to detect breaking changes" do + it "builds both schemas at one logical path and uses Buf's WIRE rules to detect breaking changes" do calls = stub_buf( build_results: [["", successful_status], ["", successful_status]], breaking_result: ["schema.proto: Field changed type.", failed_status(100)] @@ -87,6 +87,74 @@ module SchemaDefinition ) end + context "with the real Buf CLI", :in_temp_dir, :requires_buf do + let(:original_schema) do + widget_schema(<<~EOS) + optional string id = 1; + optional string name = 2; + optional int32 count = 3; + EOS + end + + let(:schema_without_count) do + widget_schema(<<~EOS) + optional string id = 1; + optional string name = 2; + reserved 3; // Previously used by count. + EOS + end + + it "allows adding a field" do + expect(breaking_changes_from(original_schema, to: original_schema.sub("optional int32 count = 3;", "optional int32 count = 3;\n optional string color = 4;"))).to be_nil + end + + it "allows removing a field whose number is reserved" do + expect(breaking_changes_from(original_schema, to: schema_without_count)).to be_nil + end + + it "allows renaming a field that keeps its number" do + expect(breaking_changes_from(original_schema, to: original_schema.sub("int32 count = 3;", "int32 total = 3;"))).to be_nil + end + + it "allows wire-compatible type changes" do + expect(breaking_changes_from(original_schema, to: original_schema.sub("int32 count", "int64 count"))).to be_nil + end + + it "rejects wire-incompatible type changes" do + expect(breaking_changes_from(original_schema, to: original_schema.sub("int32 count", "string count"))).to include( + "schema.proto", + 'changed type from "int32" to "string"' + ) + end + + it "rejects changing a single-valued field into a list" do + expect(breaking_changes_from(original_schema, to: original_schema.sub("optional int32 count", "repeated int32 count"))).to include( + 'changed cardinality from "optional with explicit presence" to "repeated"' + ) + end + + it "rejects restoring a removed field, which would drop its reservation" do + expect(breaking_changes_from(schema_without_count, to: original_schema)).to include( + 'Previously present reserved range "[3]" on message "Widget" is missing values: [3] were removed.' + ) + end + + def breaking_changes_from(against_schema, to:) + described_class.new.breaking_changes(current_schema: to, against_schema: against_schema) + end + + def widget_schema(fields) + <<~EOS + syntax = "proto3"; + package elasticgraph; + + message Widget { + #{fields.lines.map { |line| " #{line}" }.join.chomp} + } + EOS + end + end + def stub_buf(build_results:, breaking_result: nil) calls = [] remaining_build_results = build_results.dup