Skip to content

fix(cmd): preserve protobuf raw descriptor blocks during template copy#3805

Open
NepetaLemon wants to merge 3 commits into
go-kratos:mainfrom
NepetaLemon:fix/avoid-corrupting-protobuf-generated-files
Open

fix(cmd): preserve protobuf raw descriptor blocks during template copy#3805
NepetaLemon wants to merge 3 commits into
go-kratos:mainfrom
NepetaLemon:fix/avoid-corrupting-protobuf-generated-files

Conversation

@NepetaLemon
Copy link
Copy Markdown

Description (what this PR does / why we need it):

kratos new currently performs repository-wide string replacement when copying a template into a new project.

For .pb.go files generated by newer protoc-gen-go versions, the protobuf raw descriptor is emitted as a string literal block. Replacing the template module path inside that block can corrupt the embedded descriptor payload and cause generated projects to
panic during protobuf descriptor initialization.

This change preserves protobuf raw descriptor string blocks during template copy while keeping normal module-path replacement behavior for the rest of the file.

The change is backward-compatible with legacy generated files:

  • newer string-literal raw descriptors are preserved
  • legacy []byte raw descriptors continue to follow the existing replacement path

This PR also adds regression tests covering:

  • protected replacement for string-literal raw descriptors
  • normal replacement for non-generated content
  • unchanged behavior for legacy []byte descriptor output

Which issue(s) this PR fixes (resolves / be part of):

part of #

Other special notes for the reviewers:

Validated with:

  • go test ./internal/base ./internal/project under cmd/kratos
  • a real kratos new run against a local ftgo-layout template

Affected protoc-gen-go range verified locally:

  • v1.36.5 and earlier still emit []byte raw descriptors
  • v1.36.6 through at least v1.36.11 emit string-literal raw descriptors

@dosubot dosubot Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 16, 2026
@shenqidebaozi shenqidebaozi requested a review from Copilot March 24, 2026 11:44
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates kratos new template-copy behavior to avoid corrupting protobuf raw descriptor payloads in newer protoc-gen-go-generated .pb.go files where descriptors are emitted as concatenated string-literal blocks.

Changes:

  • Add protobuf raw-descriptor block detection and skip module-path replacements within those blocks while still replacing elsewhere.
  • Refactor replacement logic into replaceTemplateContent/applyReplacements.
  • Add regression tests for preserving string-literal raw descriptors and keeping legacy []byte behavior unchanged.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
cmd/kratos/internal/base/path.go Preserves matched protobuf raw descriptor string blocks during template copying while applying replacements to other content.
cmd/kratos/internal/base/path_test.go Adds tests validating preservation of string-literal raw descriptors, full replacement when no raw desc exists, and legacy []byte behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/kratos/internal/base/path.go Outdated
Comment thread cmd/kratos/internal/base/path_test.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants