Skip to content

feat(protogen): infer feature imports from ProtoFileOptions and sort options deterministically#400

Merged
wenchy merged 1 commit intomasterfrom
infer-imports-from-file-options
Apr 22, 2026
Merged

feat(protogen): infer feature imports from ProtoFileOptions and sort options deterministically#400
wenchy merged 1 commit intomasterfrom
infer-imports-from-file-options

Conversation

@Kybxd
Copy link
Copy Markdown
Collaborator

@Kybxd Kybxd commented Apr 22, 2026

Summary

When generating .proto files via bookExporter.export, language-specific
feature options (e.g. features.(pb.go).*, features.(pb.cpp).*,
features.(pb.java).*) used to require callers to also manually declare the
matching *_features.proto imports, and the emitted option lines came out
in Go's non-deterministic map iteration order.

This MR fixes both issues.

Changes

internal/protogen/exporter.go

  • Infer feature imports from ProtoFileOptions. While building the import
    set, scan each option key and add the corresponding well-known proto:
    • features.(pb.go)google/protobuf/go_features.proto
    • features.(pb.cpp)google/protobuf/cpp_features.proto
    • features.(pb.java)google/protobuf/java_features.proto
      Because imports are kept in a treeset keyed by path, duplicates are
      naturally deduplicated and output stays sorted by import path.
  • Emit option lines in alphabetical order of the key instead of relying
    on Go's randomized map iteration, producing stable, diff-friendly output.

internal/protogen/exporter_test.go

  • New table-driven Test_bookExporter_export covering:
    • proto3 baseline output.
    • edition = "2023" with features.utf8_validation.
    • edition = "2024" with features.strip_enum_prefix.
    • Import inference for each of pb.go / pb.cpp / pb.java in isolation,
      asserting that only the matching *_features.proto is imported.
    • All three language features together, asserting that all three imports are
      added and options appear in the file.
    • Alphabetical ordering of option keys when they are defined in a
      non-alphabetical order in the input map.
  • Tests use real options defined in protobuf's go_features.proto /
    cpp_features.proto / java_features.proto (api_level, string_type,
    utf8_validation, legacy_closed_enum, ...) rather than synthetic names.

.github/workflows/*.yml (lint / testing / release / publish-module-to-bsr)

  • Formatting-only changes produced automatically by the VSCode "GitHub
    Actions" extension on save
    (e.g. [master, main][ master, main ],
    minor spacing normalizations). No workflow semantics are changed.

Compatibility

  • Public API of bookExporter is unchanged.
  • For users that already imported *_features.proto manually, the import set
    deduplicates via treeset, so no duplicate import lines are produced.
  • Option output order changes from random to alphabetical, which may affect
    byte-for-byte comparisons of previously generated files but not their
    semantics.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.38%. Comparing base (e7a79d9) to head (aa6ff1b).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #400      +/-   ##
==========================================
+ Coverage   73.35%   73.38%   +0.03%     
==========================================
  Files          88       88              
  Lines       11284    11300      +16     
==========================================
+ Hits         8277     8293      +16     
  Misses       2440     2440              
  Partials      567      567              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wenchy wenchy merged commit 99d72fc into master Apr 22, 2026
7 checks passed
@wenchy wenchy deleted the infer-imports-from-file-options branch April 22, 2026 08:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants