Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a4c1b66
generate-stable-protos: turn draft schema validation cases into fixtures
stephanos Nov 20, 2026
fa43751
generate-stable-protos: turn comment ownership cases into fixtures
stephanos Nov 20, 2026
1664ed5
generate-stable-protos: cover import path rewriting in the imports fi…
stephanos Nov 20, 2026
6859277
generate-stable-protos: check the generator is idempotent
stephanos Nov 21, 2026
ff2c0ca
generate-stable-protos: check random schemas with rapid
stephanos Nov 20, 2026
0e785ae
generate-stable-protos: reject RPC options that name draft fields
stephanos Nov 20, 2026
16ef18b
generate-stable-protos: take the draft option package from the schema…
stephanos Nov 21, 2026
a900e89
generate-stable-protos: make the source, stable and schema paths flags
stephanos Nov 21, 2026
4a2907b
generate-stable-protos: require an empty -out and create files exclus…
stephanos Nov 21, 2026
aee5a01
docs: specify draft annotations for every proto placement
stephanos Nov 21, 2026
3b525b8
generate-stable-protos: validate draft annotations on every placement
stephanos Nov 21, 2026
37a931b
generate-stable-protos: require proto3 in api_next
stephanos Nov 21, 2026
2f3ed6c
generate-stable-protos: drop the comments of pruned imports
stephanos Nov 21, 2026
738e12c
generate-stable-protos: project every draft placement
stephanos Nov 21, 2026
eeb6922
generate-stable-protos: reject draft references in retained option li…
stephanos Nov 21, 2026
096e031
generate-stable-protos: generate random schemas with every draft plac…
stephanos Nov 21, 2026
8fefb3b
generate-stable-protos: emit resolved draft feature gates
stephanos Nov 23, 2026
d91ac8e
api_next: move the draft example off workflow updates
stephanos Nov 23, 2026
af52922
generate-stable-protos: reject draft filtering inside retained collec…
stephanos Nov 23, 2026
48b0766
Document how to develop and release draft protos
stephanos Nov 24, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ Use `buf.next.yaml` to compile the preview tree.

## Contribution

Edit proto files under `temporal/api_next/`. Run `make stable-api` to regenerate `temporal/api/`, or run
`make` to also update the OpenAPI definitions. Do not edit `temporal/api/` by hand. Commit the regenerated
tree with your changes because CI fails if it is stale.

Use the `draft` field option or the `draft_message` message option from
[`temporal/api_next/protometa/v1/draft.proto`](temporal/api_next/protometa/v1/draft.proto) to mark draft
declarations. For example, `string user = 7 [(temporal.api.protometa.v1.draft) = "update-user-tagging"];`
is a draft field. The generator removes annotated declarations from the stable tree.
Edit `temporal/api_next/` for all schema changes. The generator writes the stable schema to
`temporal/api/`. Do not edit that folder by hand.

Use a [draft annotation](docs/draft-apis.md) to keep a declaration out of the stable schema while
you develop it. Remove the annotation when the declaration is ready to ship.

Run `make` to regenerate the stable protos, draft manifest, and OpenAPI definitions. To update
just `temporal/api/`, run `make stable-api`. Commit the source changes and generated files together.

Rust is also required because `make` installs and runs `nex-gen` when regenerating system Nexus WIT files.

Expand Down
90 changes: 90 additions & 0 deletions cmd/generate-stable-protos/collections_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"os"
"path/filepath"
"strings"
"testing"
)

func TestDraftCollections(t *testing.T) {
schema, err := os.ReadFile("../../temporal/api_next/protometa/v1/draft.proto")
if err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
proto string
want string
}{
{
name: "stable repeated message reaches draft through a cycle",
proto: `
message Root { repeated Item entries = 1; }
message Item { Nested nested = 1; }
message Nested {
Item cycle = 1;
string secret = 2 [(temporal.api.protometa.v1.draft) = "preview"];
}`,
want: "fixture.Root.entries: retained collection reaches draft declaration fixture.Nested.secret",
},
{
name: "stable map reaches an individually gated enum value",
proto: `
message Root { map<string, Mode> entries = 1; }
enum Mode {
MODE_UNSPECIFIED = 0;
MODE_PREVIEW = 1 [(temporal.api.protometa.v1.draft_enum_value) = "preview"];
}`,
want: "fixture.Root.entries: retained collection reaches draft declaration fixture.MODE_PREVIEW",
},
{
name: "gated list reaches a different gate",
proto: `
message Root { repeated Item entries = 1 [(temporal.api.protometa.v1.draft) = "preview"]; }
message Item {
string secret = 1 [(temporal.api.protometa.v1.draft) = "details"];
}`,
want: "fixture.Root.entries: retained collection reaches draft declaration fixture.Item.secret",
},
{
name: "list and map clear under one gate",
proto: `
message Root {
repeated Item entries = 1 [(temporal.api.protometa.v1.draft) = "preview"];
map<string, Item> by_name = 2 [(temporal.api.protometa.v1.draft) = "preview"];
}
message Item {
option (temporal.api.protometa.v1.draft_message) = "preview";
repeated string names = 1;
Item cycle = 2;
}`,
},
{
name: "gated collection can share a stable message with the same gate",
proto: `
message Root { repeated Item entries = 1 [(temporal.api.protometa.v1.draft) = "preview"]; }
message Item {
string secret = 1 [(temporal.api.protometa.v1.draft) = "preview"];
}`,
},
} {
t.Run(tc.name, func(t *testing.T) {
cfg := defaultConfig()
root := t.TempDir()
writeFile(t, filepath.Join(root, cfg.draftSchema), string(schema))
writeFile(t, filepath.Join(root, cfg.nextDir, "fixture.proto"), `syntax = "proto3";
package fixture;
import "temporal/api_next/protometa/v1/draft.proto";
`+tc.proto)
err := cfg.stableProtosGenerator(t.Context(), root, t.TempDir())
if tc.want == "" {
if err != nil {
t.Fatal(err)
}
} else if err == nil || !strings.Contains(err.Error(), tc.want) {
t.Fatalf("want error containing %q, got %v", tc.want, err)
}
})
}
}
117 changes: 117 additions & 0 deletions cmd/generate-stable-protos/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,128 @@ import (
"strings"

"github.com/bufbuild/protocompile/experimental/ast"
"github.com/bufbuild/protocompile/experimental/ast/edit"
"github.com/bufbuild/protocompile/experimental/fdp"
"github.com/bufbuild/protocompile/experimental/ir"
"github.com/bufbuild/protocompile/experimental/parser"
"github.com/bufbuild/protocompile/experimental/report"
"github.com/bufbuild/protocompile/experimental/seq"
"github.com/bufbuild/protocompile/experimental/source"
"github.com/bufbuild/protocompile/experimental/token"
)

// pruneImports removes unused imports and the external trivia attributed to them.
func pruneImports(f *ir.File) (*ast.File, error) {
unused := map[string]ast.DeclImport{}
for imp := range seq.Values(f.Imports()) {
if !imp.Used && !imp.Decl.IsZero() {
unused[imp.Path()] = imp.Decl
}
}
if len(unused) == 0 {
return f.AST(), nil
}
fd, err := fdp.DescriptorProto(f, fdp.IncludeSourceCodeInfo(true))
if err != nil {
return nil, fmt.Errorf("reading import source info: %w", err)
}
stream := f.AST().Stream()
var text []byte
// Replace comment bytes with whitespace so the import offsets remain valid after parsing.
mask := func(span source.Span) {
if text == nil {
text = []byte(stream.File.Text())
}
for i := span.Start; i < span.End; i++ {
if text[i] != '\n' && text[i] != '\r' {
text[i] = ' '
}
}
}
starts := map[int]bool{}
const dependencyTag = 3
for _, loc := range fd.GetSourceCodeInfo().GetLocation() {
path := loc.GetPath()
if len(path) != 2 || path[0] != dependencyTag {
continue
}
dependency := fd.GetDependency()[path[1]]
decl, drop := unused[dependency]
if !drop {
continue
}
delete(unused, dependency)
span := decl.Span()
starts[span.Start] = true
leading := strings.Fields(strings.Join(loc.GetLeadingDetachedComments(), " ") + " " + loc.GetLeadingComments())
trailing := strings.Fields(loc.GetTrailingComments())
before, _ := stream.Around(span.Start)
_, after := stream.Around(span.End)
if !maskImportComments(before, leading, true, mask) || !maskImportComments(after, trailing, false, mask) {
return nil, fmt.Errorf("locating comments of import %q", dependency)
}
}
if len(unused) != 0 {
return nil, fmt.Errorf("missing source locations for %d unused imports", len(unused))
}

file := f.AST()
if text != nil {
var diagnostics report.Report
file, _ = parser.Parse(f.Path(), source.NewFile(f.Path(), string(text)), &diagnostics)
if message, failed := errorDiagnostics(&diagnostics); failed {
return nil, fmt.Errorf("parsing source with pruned import comments: %s", message)
}
}
var dels []edit.Edit
for decl := range seq.Values(file.Decls()) {
if !decl.AsImport().IsZero() && starts[decl.Span().Start] {
dels = append(dels, edit.Edit{Kind: edit.KindDelete, Target: decl})
}
}
if err := edit.ApplyEdits(file, dels); err != nil {
return nil, err
}
return file, nil
}

// maskImportComments locates attributed comments in the trivia adjacent to an import.
func maskImportComments(start token.Token, words []string, backwards bool, mask func(source.Span)) bool {
if len(words) == 0 {
return true
}
if start.IsZero() {
return false
}
cursor := token.NewCursorAt(start)
next := cursor.NextSkippable
if backwards {
cursor.NextSkippable()
next = cursor.PrevSkippable
}
for tok := next(); !tok.IsZero() && tok.Kind().IsSkippable(); tok = next() {
if tok.Kind() != token.Comment {
continue
}
got := commentTokenWords(tok.Text())
if len(got) > len(words) {
return false
}
if backwards && slices.Equal(words[len(words)-len(got):], got) {
words = words[:len(words)-len(got)]
} else if !backwards && slices.Equal(words[:len(got)], got) {
words = words[len(got):]
} else {
return false
}
mask(tok.LeafSpan())
if len(words) == 0 {
return true
}
}
return false
}

// draftComments holds the comments protoc attributes to a file's removed draft
// declarations, and how often each comment word may still occur in the stable file.
type draftComments struct {
Expand Down
96 changes: 0 additions & 96 deletions cmd/generate-stable-protos/comments_test.go

This file was deleted.

15 changes: 15 additions & 0 deletions cmd/generate-stable-protos/corpus_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import "testing"

// TestRepoAPINextIdempotent checks that the stable tree generated from the repository's
// api_next tree is a fixed point of the generator.
func TestRepoAPINextIdempotent(t *testing.T) {
t.Parallel()
const root = "../.."
first := t.TempDir()
if err := testConfig.stableProtosGenerator(t.Context(), root, first); err != nil {
t.Fatal(err)
}
checkIdempotent(t.Context(), t, root, first, t.TempDir())
}
Loading