Skip to content

Further work on Golang RPC, import manipulation, and formatting#7557

Merged
jkschneider merged 1 commit intomainfrom
worktree-golang
May 3, 2026
Merged

Further work on Golang RPC, import manipulation, and formatting#7557
jkschneider merged 1 commit intomainfrom
worktree-golang

Conversation

@jkschneider
Copy link
Copy Markdown
Member

Summary

Brings rewrite-go to parity with the JavaScript / Python / C# language ports across the RPC handler surface, recipe-author primitives, type attribution, test harness, and recipe library.

RPC handler surface

  • Parse / ParseProject (project-aware: module + goModContent; per-file closest-ancestor go.mod ownership; sibling go.sum populates ResolvedDependencies).
  • BatchVisit returns {modified, deleted, hasNewMessages, searchResultIds}; searchResultIds walker registered, hasNewMessages snapshot tracked.
  • Generate drives ScanningRecipe.Generate.
  • GetMarketplace emits the full descriptor (recipeList, dataTables, preconditions, maintainers, contributors, examples, option types).
  • PrepareRecipe returns per-instance UUID.
  • TraceGetObject for RPC tracing.
  • Server CLI flags: --log-file, --trace-rpc-messages, --metrics-csv, --recipe-install-dir, --data-tables-csv-dir.

Recipe-author surface

  • recipe.Recipe + recipe.Base; ScanningRecipe end-to-end.
  • recipe.DataTable[Row] generic + InMemoryDataTableStore + CsvDataTableStore.
  • visitor.GoVisitor.Cursor() / SetCursor(); visitor.BuildChain reconstructs cursors from RPC IDs.
  • Cursor message map: PutMessage, GetNearestMessage, PollNearestMessage, etc.
  • recipe.Service[T](sourceFile) registry + GoVisitor.DoAfterVisit composition.
  • Five services: ImportService, AutoFormatService, AnnotationService, WhitespaceValidationService, NamingService.
  • Five foundational recipes: AddImport, RemoveImport, RemoveUnusedImports, OrderImports, RenamePackage.

Type attribution

  • Same-package multi-file resolution via parser.ParsePackage.
  • Third-party module resolution via go.mod requires + vendor walker (real JavaTypeMethod attribution; honors replace directives).
  • tree.GoResolutionResult marker (Module, Requires, Replaces, Excludes, Retracts, ResolvedDependencies); both Go-side and Java-side RPC codecs.
  • parser.ProjectImporter 3-tier lazy resolver (sources → requires → stdlib).
  • Build-tag file inclusion: parser.MatchBuildContext recognizes //go:build, // +build, and filename suffixes.
  • Cross-package generics across the corpus.

RPC sender / receiver visitor-pattern refactor

  • Senders / receivers on both sides extend the language visitor (visitor.GoVisitor on Go, JavaVisitor<RpcSendQueue/ReceiveQueue> on Java) and override VisitX, mirroring rewrite-java.
  • Eliminated ~600 LOC of switch boilerplate.
  • tree.J got polymorphic GetID / GetPrefix / GetMarkers; mutators intentionally NOT on the interface (would break RecipeScheduler identity-based change detection).

Annotations

  • tree.Annotation (J-shaped) + LeadingAnnotations on VariableDeclarations, MethodDeclaration, TypeDecl on both sides.
  • Struct field tags AND //go: / //lint: directives uniformly modeled as J.Annotation.
  • AnnotationService surface mirrors rewrite-java.

Test harness

  • Java: Assertions.go(...), goMod(...), goProject(name, ...) matching mavenProject(...).
  • Go: test.RewriteRun + test.Golang(...), test.GoMod(...), test.GoSum(...), test.GoProject(name, ...).
  • propagateModuleResolution copies the parsed go.mod marker onto every sibling .go CU at project expansion.
  • ExpectType / ExpectPrimitiveType / ExpectMethodType helpers on both sides.
  • GoMod conformance corpus (10 cases) shared across Go + Java tests.
  • Printer parity corpus (13 fixtures across gofmt/ + generics/), opt-in via make parity, never in CI.

Documentation

  • Recipe-author guide at doc/recipe-authoring.md.
  • PLAN.md removed; contents now reflected in the codebase, doc, and shipped tests.

Test plan

  • cd rewrite-go && go test ./... — full Go suite green.
  • ./gradlew :rewrite-go:check :rewrite-go:integTest — Java unit + integ.
  • cd rewrite-go && make parity — printer corpus byte-equality.
  • Downstream: recipes-go test suite against the worktree replace path.

Full RPC handler surface, recipe-author primitives, type attribution,
test harness, and recipe library brought to parity with the JavaScript,
Python, and C# language ports.

RPC handler surface
- Parse / ParseProject (project-aware: module + goModContent; per-file
  closest-ancestor go.mod ownership; sibling go.sum populates
  ResolvedDependencies).
- BatchVisit returns {modified, deleted, hasNewMessages, searchResultIds};
  searchResultIds walker registered, hasNewMessages snapshot tracked.
- Generate drives ScanningRecipe.Generate.
- GetMarketplace emits full descriptor (recipeList, dataTables,
  preconditions, maintainers, contributors, examples, option types).
- PrepareRecipe returns per-instance UUID.
- TraceGetObject for RPC tracing.
- Server CLI flags: --log-file, --trace-rpc-messages, --metrics-csv,
  --recipe-install-dir, --data-tables-csv-dir.

Recipe-author surface
- recipe.Recipe + recipe.Base, ScanningRecipe end-to-end.
- recipe.DataTable[Row] generic + InMemoryDataTableStore +
  CsvDataTableStore.
- visitor.GoVisitor.Cursor() / SetCursor() for RPC seeding;
  visitor.BuildChain reconstructs cursors from RPC IDs.
- Cursor message map: PutMessage, GetNearestMessage,
  PollNearestMessage, etc.
- recipe.Service[T](sourceFile) registry + GoVisitor.DoAfterVisit
  composition.
- Five services: ImportService, AutoFormatService, AnnotationService,
  WhitespaceValidationService, NamingService.
- Five foundational recipes: AddImport, RemoveImport,
  RemoveUnusedImports, OrderImports, RenamePackage.

Type attribution
- Same-package multi-file resolution via parser.ParsePackage.
- Third-party module resolution via go.mod requires + vendor walker
  (parses <projectRoot>/vendor/<modulePath>/*.go for real
  JavaTypeMethod attribution; honors `replace` directives).
- tree.GoResolutionResult marker (Module, Requires, Replaces,
  Excludes, Retracts, ResolvedDependencies); both Go-side and
  Java-side RPC codecs.
- parser.ProjectImporter 3-tier lazy resolver
  (sources -> requires -> stdlib).
- Build-tag file inclusion: parser.MatchBuildContext recognizes
  //go:build, // +build, and filename suffixes; default
  build.Default with recipe-author override.
- Cross-package generics across the corpus.

RPC sender / receiver visitor-pattern refactor
- Senders / receivers on both sides extend the language visitor
  (visitor.GoVisitor on Go, JavaVisitor<RpcSendQueue/ReceiveQueue>
  on Java) and override VisitX, mirroring rewrite-java.
- Eliminated ~600 LOC of switch boilerplate.
- tree.J got polymorphic GetID / GetPrefix / GetMarkers; mutators
  intentionally NOT on the interface (would break RecipeScheduler
  identity-based change detection).

Annotations
- tree.Annotation (J-shaped) + LeadingAnnotations on
  VariableDeclarations, MethodDeclaration, TypeDecl on both Go and
  Java sides.
- Struct field tags AND //go:/lint: directives uniformly modeled as
  J.Annotation; printer reassembles backticks for tags and //<name>
  lines for directives.
- AnnotationService surface mirrors rewrite-java
  (AllAnnotations, IsAnnotatedWith, FindAnnotations,
  AddAnnotationVisitor, RemoveAnnotationVisitor).

Test harness
- Java: Assertions.go(...), goMod(...), goProject(name, ...) sibling
  pattern matching mavenProject(...).
- Go: test.RewriteRun + test.Golang(...), test.GoMod(...),
  test.GoSum(...), test.GoProject(name, ...).
- propagateModuleResolution copies the parsed go.mod marker onto
  every sibling .go CU at project expansion so cross-file recipes
  read module context per file.
- ExpectType / ExpectPrimitiveType / ExpectMethodType helpers on
  both sides.
- GoMod conformance corpus (10 cases) shared across Go + Java tests.
- Printer parity corpus (13 fixtures across gofmt/ + generics/),
  opt-in via `make parity`, never in CI.

Recipe-author guide at doc/recipe-authoring.md.
PLAN.md removed: contents are now reflected in the codebase, doc, and
shipped tests.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 3, 2026
@jkschneider jkschneider merged commit bbe3b20 into main May 3, 2026
1 check failed
@jkschneider jkschneider deleted the worktree-golang branch May 3, 2026 22:42
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant